1. Introduction to Go-Excelize
Excelize is a basic library for operating Office Excel documents written in Go, based on ECMA-376, ISO/IEC 29500 international standard. You can use it to read and write spreadsheet documents created by Microsoft Excel™ 2007 and above.
Supports various document formats such as XLAM/XLSM/XLSX/XLTM/XLTX, and is highly compatible with documents with complex components such as styles, pictures (tables), pivot tables, slicers, etc., and provides a streaming read and write API for processing workbooks containing large-scale data. It can be applied to various reporting platforms, cloud computing, edge computing and other systems.
The Go language required to be used for use in this library is 1.15 or higher.
2. Close()
func (f *File) Close() error
What this API does is to close the workbook and clean up the system disk cache that may occur when opening the document.
Let’s take a look at the specific code below:
func (f *File) Close() error { var err error if != nil { if err := (); err != nil { return err } } (func(k, v interface{}) bool { if err = (v.(string)); err != nil { return false } return true }) return err }
First determine whether sharedStringTemp is empty, this is a variable of type *. It should be to close an important cache file.
Next is to traverse temporary files.(v.(string))
These temporary files and folders will be deleted. The above steps clean up the system disk cache that may occur when opening the document.
3. NewSheet()
func (f *File) NewSheet(sheet string) int
The purpose of this API is to create a new worksheet based on the given worksheet name and return the index of the worksheet in the workbook. Note that when creating a new workbook, a default worksheet named Sheet1 will be included.
func (f *File) NewSheet(name string) int { // Check if the worksheet already exists index := (name) if index != -1 { return index } (name) ++ wb := () sheetID := 0 for _, v := range { if > sheetID { sheetID = } } sheetID++ // Update docProps/ () // Update [Content_Types].xml ("/xl/worksheets/sheet"+(sheetID)+".xml", ContentTypeSpreadSheetMLWorksheet) // Create new sheet /xl/worksheets/sheet% (sheetID, name) // Update rID := ((), SourceRelationshipWorkSheet, ("/xl/worksheets/sheet%", sheetID), "") // Update (name, sheetID, rID) return (name) }
Note that worksheet names are case-insensitive.
First check whether the payroll of the given name exists. If GetSheetIndex does not exist, it will return -1, otherwise return the worksheet index.
func (f *File) GetSheetIndex(name string) int { for index, sheet := range () { if (sheet, trimSheetName(name)) { return index } } return -1 }
If GetSheetIndex returns -1, that is, the worksheet exists, then DeleteSheet is called to delete the worksheet. Then build awb := ()
Workbook reader.
for _, v := range { if > sheetID { sheetID = } }
The meaning of this code should be to find the maximum SheetID of the worksheet and assign it to the sheetID.
Then create or update docProps/, [Content_Types].xml, /xl/worksheets/sheet%, , these files.
// Update docProps/ () // Update [Content_Types].xml ("/xl/worksheets/sheet"+(sheetID)+".xml", ContentTypeSpreadSheetMLWorksheet) // Create new sheet /xl/worksheets/sheet% (sheetID, name) // Update rID := ((), SourceRelationshipWorkSheet, ("/xl/worksheets/sheet%", sheetID), "") // Update (name, sheetID, rID)
Finally return to this Sheet.
The above is the detailed content of the Go Excelize API source code reading Close and NewSheet method example analysis. For more information about Go-Excelize API, please follow my other related articles!