This is a creation in Article, where the information may have evolved or changed.
Https://raw.githubusercontent.com/Luxurioust/excelize/master/excelize.png
Excelize is a Microsoft-based Office OpenXML standard that is written by Golang to manipulate office Excel document class libraries. You can use it to read, write to XLSX files. Compared to other open source class libraries, Excelize supports writing documents with pictures (tables), supports inserting pictures into Excel, and does not lose the chart style after saving.
Project Home Github.com/luxurioust/excelize
Installation
go get github.com/Luxurioust/excelize
Create XLSX
package mainimport ( "fmt" "os" "github.com/Luxurioust/excelize")func main() { xlsx := excelize.CreateFile() // Create a new sheet. xlsx.NewSheet(2, "Sheet2") // Set value of a cell. xlsx.SetCellValue("Sheet2", "A2", "Hello world.") xlsx.SetCellValue("Sheet1", "B2", 100) // Set active sheet of the workbook. xlsx.SetActiveSheet(2) // Save xlsx file by the given path. err := xlsx.WriteTo("./Workbook.xlsx") if err != nil { fmt.Println(err) os.Exit(1) }}
Read and write existing documents
package mainimport ( "fmt" "os" "strconv" "github.com/Luxurioust/excelize")func main() { xlsx, err := excelize.OpenFile("./Workbook.xlsx") if err != nil { fmt.Println(err) os.Exit(1) } // Get value from cell by given sheet index and axis. cell := xlsx.GetCellValue("Sheet1", "B2") fmt.Println(cell) // Get sheet index. index := xlsx.GetSheetIndex("Sheet2") // Get all the rows in a sheet. rows := xlsx.GetRows("sheet" + strconv.Itoa(index)) for _, row := range rows { for _, colCell := range row { fmt.Print(colCell, "\t") } fmt.Println() }}
Insert a chart into Excel
Package Mainimport ("FMT" "OS" "Github.com/luxurioust/excelize") func main () {categories: = Map[string]string {"A2": "Small", "A3": "Normal", "A4": "Large", "B1": "Apple", "C1": "Orange", "D1": "Pear"} Values: = map[string]int{"B 2 ": 2," C2 ": 3," D2 ": 3," B3 ": 5," C3 ": 2," D3 ": 4," B4 ": 6," C4 ": 7," D4 ": 8} xlsx: = Excelize. CreateFile () for k, V: = Range Categories {xlsx. Setcellvalue ("Sheet1", K, V)} for K, V: = range values {xlsx. Setcellvalue ("Sheet1", K, V)} xlsx. Addchart ("Sheet1", "E1", ' {"type": "Bar3d", "series": [{"Name": "=sheet1! $A $ $", "categories": "=sheet1! $B $: $D", " Values ":" =sheet1! $B $: $D $ "},{" name ":" =sheet1! $A $ "," categories ":" =sheet1! $B $: $D $ "," values ": =sheet1! $B $: $D $ "},{" name ":" =sheet1! $A $ $ "," categories ":" =sheet1! $B $: $D "," Values ":" =sheet1! $B $4: $D $4 "}]," title ": {" name ":" Fruit 3D line Chart "}} ')//Save xlsx file by the given path. ERR: = xlsx. WriteTo ("./workbook.xlsx") if err! = Nil {fmt. PRINTLN (ERR) Os. Exit (1)}}
insert a picture into Excel
Package Mainimport ("FMT" "Os" _ "Image/gif" _ "Image/jpeg" _ "Image/png" "Github.com/luxurioust/exceli Ze ") func main () {xlsx, err: = Excelize. OpenFile ("./workbook.xlsx") if err! = Nil {fmt. PRINTLN (ERR) OS. Exit (1)}//Insert a picture. Err = xlsx. AddPicture ("Sheet1", "A2", "./image1.gif", "" ") if err! = Nil {fmt. PRINTLN (ERR)}//Insert a picture to sheet with scaling. Err = xlsx. AddPicture ("Sheet1", "D2", "./image2.jpg", ' {"X_scale": 0.5, "Y_scale": 0.5} ') if err! = Nil {fmt. PRINTLN (ERR)}//Insert a picture of offset in the cell with printing support. Err = xlsx. AddPicture ("Sheet1", "H2", "./image3.gif", "{" X_offset ":", "Y_offset": Ten, "Print_obj": True, "Lock_aspect_ratio": False, "locked": false} ') if err! = Nil {fmt. PRINTLN (ERR)}//Save the xlsx file with the Origin path. Err = xlsx. Save () if err! = Nil {fmt. PRINTLN (ERR) OS. Exit (1)}}