日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Go + Excel 学习 Excelize rows.go

發布時間:2024/5/6 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Go + Excel 学习 Excelize rows.go 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Copyright 2016 - 2019 The excelize Authors.
版權所有2016-2019優秀作者。

All rights reserved.
保留所有權利。

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
此源代碼的使用由可在許可文件中找到的BSD樣式許可管理。

Package excelize providing a set of functions that allow you to write to and read from XLSX files.
包提供了一組函數,允許您對xlsx文件進行寫入和讀取。

Support reads and writes XLSX file generated by Microsoft Excel? 2007 and later.
支持讀取和寫入由Microsoft Excel?2007及更高版本生成的XLSX文件。

Support save file without losing original charts of XLSX.
支持保存文件而不丟失xlsx的原始圖表。

This library needs Go version 1.10 or later.
此庫需要go版本1.10或更高版本。

package excelizeimport ("encoding/xml""fmt""math""strconv" )

type Rows struct

Rows defines an iterator to a sheet
行定義工作表的迭代器

type Rows struct {err errorf *Filerows []xlsxRowcurRow int }

type ErrSheetNotExist struct

ErrSheetNotExist defines an error of sheet is not exist
errsheetnotexist定義工作表不存在的錯誤

type ErrSheetNotExist struct {SheetName string }

func (err ErrSheetNotExist) Error() string

func (err ErrSheetNotExist) Error() string {return fmt.Sprintf("Sheet %s is not exist", string(err.SheetName)) }

func (rows *Rows) Next() bool

Next will return true if find the next row element.
如果找到下一行元素,next將返回true。

func (rows *Rows) Next() bool {return rows.curRow < len(rows.rows) }

func (rows *Rows) Error() error

Error will return the error when the find next row element
查找下一行元素時,錯誤將返回Error

func (rows *Rows) Error() error {return rows.err }

func convertRowHeightToPixels(height float64) float64

convertRowHeightToPixels provides a function to convert the height of a cell from user’s units to pixels.
convertRowHeightToPixels 提供一個函數,用于將單元格的高度從用戶的單位轉換為像素。

If the height hasn’t been set by the user we use the default value. If the row is hidden it has a value of zero.
如果用戶沒有設置高度,我們使用默認值。如果行被隱藏,則其值為零。

func convertRowHeightToPixels(height float64) float64 {var pixels float64if height == 0 {return pixels}pixels = math.Ceil(4.0 / 3.0 * height)return pixels }

func (f *File) Rows(sheet string) (*Rows, error)

Rows return a rows iterator.
行返回行迭代器。

func (f *File) Rows(sheet string) (*Rows, error) {xlsx, err := f.workSheetReader(sheet)if err != nil {return nil, err}name, ok := f.sheetMap[trimSheetName(sheet)]if !ok {return nil, ErrSheetNotExist{sheet}}if xlsx != nil {data := f.readXML(name)f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpaceBytes(namespaceStrictToTransitional(data)))}return &Rows{f: f,rows: xlsx.SheetData.Row,}, nil }

func (f *File) GetRowHeight(sheet string, row int) (float64, error)

GetRowHeight provides a function to get row height by given worksheet name and row index.
GetRowHeight提供了一個函數,可以根據給定的工作表名稱和行索引獲取行高。

func (f *File) GetRowHeight(sheet string, row int) (float64, error) {if row < 1 {return defaultRowHeightPixels, newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return defaultRowHeightPixels, err}if row > len(xlsx.SheetData.Row) {return defaultRowHeightPixels, nil // it will be better to use 0, but we take care with BC}for _, v := range xlsx.SheetData.Row {if v.R == row && v.Ht != 0 {return v.Ht, nil}}// Optimisation for when the row heights haven't changed.return defaultRowHeightPixels, nil }

func (f *File) getRowHeight(sheet string, row int) int

getRowHeight provides a function to get row height in pixels by given sheet name and row index.
getRowHeight提供了一個函數,可以通過給定的工作表名稱和行索引以像素為單位獲取行高。

func (f *File) getRowHeight(sheet string, row int) int {xlsx, _ := f.workSheetReader(sheet)for _, v := range xlsx.SheetData.Row {if v.R == row+1 && v.Ht != 0 {return int(convertRowHeightToPixels(v.Ht))}}// Optimisation for when the row heights haven't changed.當行高沒有變化時進行優化。return int(defaultRowHeightPixels) }

func (f *File) SetRowHeight(sheet string, row int, height float64) error

SetRowHeight provides a function to set the height of a single row.
set row height提供了一個函數來設置單行的高度。

func (f *File) SetRowHeight(sheet string, row int, height float64) error {if row < 1 {return newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return err}prepareSheetXML(xlsx, 0, row)rowIdx := row - 1xlsx.SheetData.Row[rowIdx].Ht = heightxlsx.SheetData.Row[rowIdx].CustomHeight = truereturn nil }

func (f *File) GetRows(sheet string) ([][]string, error)

GetRows return all the rows in a sheet by given worksheet name (case sensitive).
getrows按給定的工作表名稱返回工作表中的所有行(區分大小寫)。

返回一個二維字符串數組和一個錯誤信息。

func (f *File) GetRows(sheet string) ([][]string, error) {rows, err := f.Rows(sheet)if err != nil {return nil, err}results := make([][]string, 0, 64)for rows.Next() {if rows.Error() != nil {break}row, err := rows.Columns()if err != nil {break}results = append(results, row)}return results, nil }

func (rows *Rows) Columns() ([]string, error)

Columns return the current row’s column values
列返回當前行的列值

func (rows *Rows) Columns() ([]string, error) {curRow := rows.rows[rows.curRow]rows.curRow++columns := make([]string, len(curRow.C))d := rows.f.sharedStringsReader()for _, colCell := range curRow.C {col, _, err := CellNameToCoordinates(colCell.R)if err != nil {return columns, err}val, _ := colCell.getValueFrom(rows.f, d)columns[col-1] = val}return columns, nil }

func (f *File) InsertRow(sheet string, row int) error

InsertRow provides a function to insert a new row after given Excel row number starting from 1.
InsertRow提供一個函數,用于在給定的Excel行號(從1開始)之后插入新行。

Use this method with caution, which will affect changes in references such as formulas, charts, and so on.
請謹慎使用此方法,這將影響公式、圖表等引用中的更改。

If there is any referenced value of the worksheet, it will cause a file error when you open it.
如果工作表中有任何引用值,則打開它時將導致文件錯誤。

The excelize only partially updates these references currently.
excelize當前僅部分更新這些引用。

func (f *File) InsertRow(sheet string, row int) error {if row < 1 {return newInvalidRowNumberError(row)}return f.adjustHelper(sheet, rows, row, 1) }

func (f *File) DuplicateRow(sheet string, row int) error

DuplicateRow inserts a copy of specified row (by its Excel row number) below.
duplicateRow在下面插入指定行的副本(按其excel行號)。

Use this method with caution, which will affect changes in references such as formulas, charts, and so on.
請謹慎使用此方法,這將影響公式、圖表等引用中的更改。

If there is any referenced value of the worksheet, it will cause a file error when you open it.
如果工作表中有任何引用值,則打開它時將導致文件錯誤。

The excelize only partially updates these references currently.
excelize當前僅部分更新這些引用。

func (f *File) DuplicateRow(sheet string, row int) error {return f.DuplicateRowTo(sheet, row, row+1) }

func (f *File) RemoveRow(sheet string, row int) error

RemoveRow provides a function to remove single row by given worksheet name and Excel row number.
RemoveRow提供了一個按給定工作表名稱和Excel行號刪除單行的函數。

Use this method with caution, which will affect changes in references such as formulas, charts, and so on.
請謹慎使用此方法,這將影響公式、圖表等引用中的更改。

If there is any referenced value of the worksheet, it will cause a file error when you open it.
如果工作表中有任何引用值,則打開它時將導致文件錯誤。

The excelize only partially updates these references currently.
excelize當前僅部分更新這些引用。

func (f *File) RemoveRow(sheet string, row int) error {if row < 1 {return newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return err}if row > len(xlsx.SheetData.Row) {return f.adjustHelper(sheet, rows, row, -1)}for rowIdx := range xlsx.SheetData.Row {if xlsx.SheetData.Row[rowIdx].R == row {xlsx.SheetData.Row = append(xlsx.SheetData.Row[:rowIdx],xlsx.SheetData.Row[rowIdx+1:]...)[:len(xlsx.SheetData.Row)-1]return f.adjustHelper(sheet, rows, row, -1)}}return nil }

func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error)

getValueFrom return a value from a column/row cell, this function is inteded to be used with for range on rows an argument with the xlsx opened file.
getValueFrom從列/行單元格返回一個值,此函數將與xlsx打開文件的參數一起用于行上的范圍。

func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) {switch xlsx.T {case "s":xlsxSI := 0xlsxSI, _ = strconv.Atoi(xlsx.V)return f.formattedValue(xlsx.S, d.SI[xlsxSI].String()), nilcase "str":return f.formattedValue(xlsx.S, xlsx.V), nilcase "inlineStr":return f.formattedValue(xlsx.S, xlsx.IS.String()), nildefault:return f.formattedValue(xlsx.S, xlsx.V), nil} }

func (f *File) SetRowVisible(sheet string, row int, visible bool) error

SetRowVisible provides a function to set visible of a single row by given worksheet name and Excel row number.
SetRowVisible 提供了一個函數,用于根據給定的工作表名稱和excel行號設置單行的可見。

func (f *File) SetRowVisible(sheet string, row int, visible bool) error {if row < 1 {return newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return err}prepareSheetXML(xlsx, 0, row)xlsx.SheetData.Row[row-1].Hidden = !visiblereturn nil }

func checkRow(xlsx *xlsxWorksheet) error

checkRow provides a function to check and fill each column element for all rows and make that is continuous in a worksheet of XML.
checkRow提供了一個函數,用于檢查和填充所有行的每個列元素,并使其在XML工作表中連續。

Noteice: this method could be very slow for large spreadsheets (more than 3000 rows one sheet).
注:對于大型電子表格(一張超過3000行),這種方法可能非常慢。

func checkRow(xlsx *xlsxWorksheet) error {for rowIdx := range xlsx.SheetData.Row {rowData := &xlsx.SheetData.Row[rowIdx]colCount := len(rowData.C)if colCount == 0 {continue}lastCol, _, err := CellNameToCoordinates(rowData.C[colCount-1].R)if err != nil {return err}if colCount < lastCol {oldList := rowData.Cnewlist := make([]xlsxC, 0, lastCol)rowData.C = xlsx.SheetData.Row[rowIdx].C[:0]for colIdx := 0; colIdx < lastCol; colIdx++ {cellName, err := CoordinatesToCellName(colIdx+1, rowIdx+1)if err != nil {return err}newlist = append(newlist, xlsxC{R: cellName})}rowData.C = newlistfor colIdx := range oldList {colData := &oldList[colIdx]colNum, _, err := CellNameToCoordinates(colData.R)if err != nil {return err}xlsx.SheetData.Row[rowIdx].C[colNum-1] = *colData}}}return nil }

func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error)

GetRowOutlineLevel provides a function to get outline level number of a single row by given worksheet name and Excel row number.
getRowOutlineLevel提供了一個函數,用于根據給定的工作表名稱和Excel行號獲取單行的大綱級別編號。

func (f *File) GetRowOutlineLevel(sheet string, row int) (uint8, error) {if row < 1 {return 0, newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return 0, err}if row > len(xlsx.SheetData.Row) {return 0, nil}return xlsx.SheetData.Row[row-1].OutlineLevel, nil }

func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error

SetRowOutlineLevel provides a function to set outline level number of a single row by given worksheet name and Excel row number.
SetRowOutlineLevel 提供了一個函數,用于根據給定的工作表名稱和Excel行號設置單行的大綱級別號。

func (f *File) SetRowOutlineLevel(sheet string, row int, level uint8) error {if row < 1 {return newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return err}prepareSheetXML(xlsx, 0, row)xlsx.SheetData.Row[row-1].OutlineLevel = levelreturn nil }

func (f *File) GetRowVisible(sheet string, row int) (bool, error)

GetRowVisible provides a function to get visible of a single row by given worksheet name and Excel row number.
GetRowVisible 提供了一個函數,可以通過給定的工作表名稱和excel行號獲取是否該行顯示。

func (f *File) GetRowVisible(sheet string, row int) (bool, error) {if row < 1 {return false, newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return false, err}if row > len(xlsx.SheetData.Row) {return false, nil}return !xlsx.SheetData.Row[row-1].Hidden, nil }

func (f *File) sharedStringsReader() *xlsxSST

sharedStringsReader provides a function to get the pointer to the structure after deserialization of xl/sharedStrings.xml.
sharedStringsReader提供了一個函數,用于在對xl/sharedStrings.xml進行反序列化之后獲取指向結構的指針。

func (f *File) sharedStringsReader() *xlsxSST {if f.SharedStrings == nil {var sharedStrings xlsxSSTss := f.readXML("xl/sharedStrings.xml")if len(ss) == 0 {ss = f.readXML("xl/SharedStrings.xml")}_ = xml.Unmarshal(namespaceStrictToTransitional(ss), &sharedStrings)f.SharedStrings = &sharedStrings}return f.SharedStrings }

func (f *File) DuplicateRowTo(sheet string, row, row2 int) error

DuplicateRowTo inserts a copy of specified row by it Excel number to specified row position moving down exists rows after target position.
DuplicateRowTo 按excel數字將指定行的副本插入到指定行位置在目標位置后下移已存在的行。

Use this method with caution, which will affect changes in references such as formulas, charts, and so on.
請謹慎使用此方法,這將影響公式、圖表等引用中的更改。

If there is any referenced value of the worksheet, it will cause a file error when you open it.
如果工作表中有任何引用值,則打開它時將導致文件錯誤。

The excelize only partially updates these references currently.
excelize當前僅部分更新這些引用。

func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {if row < 1 {return newInvalidRowNumberError(row)}xlsx, err := f.workSheetReader(sheet)if err != nil {return err}if row > len(xlsx.SheetData.Row) || row2 < 1 || row == row2 {return nil}var ok boolvar rowCopy xlsxRowfor i, r := range xlsx.SheetData.Row {if r.R == row {rowCopy = xlsx.SheetData.Row[i]ok = truebreak}}if !ok {return nil}if err := f.adjustHelper(sheet, rows, row2, 1); err != nil {return err}idx2 := -1for i, r := range xlsx.SheetData.Row {if r.R == row2 {idx2 = ibreak}}if idx2 == -1 && len(xlsx.SheetData.Row) >= row2 {return nil}rowCopy.C = append(make([]xlsxC, 0, len(rowCopy.C)), rowCopy.C...)f.ajustSingleRowDimensions(&rowCopy, row2)if idx2 != -1 {xlsx.SheetData.Row[idx2] = rowCopy} else {xlsx.SheetData.Row = append(xlsx.SheetData.Row, rowCopy)}return nil } 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Go + Excel 学习 Excelize rows.go的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。