### Install Excelize Library (Legacy) Source: https://github.com/xuri/excelize/blob/master/README.md Installs the Excelize library. This method might be used for older Go projects not utilizing Go Modules or for initial setup. ```Bash go get github.com/xuri/excelize ``` -------------------------------- ### Install Excelize Library (Go Modules) Source: https://github.com/xuri/excelize/blob/master/README.md Installs the Excelize library using Go Modules, specifically targeting version 2. This is the recommended installation method for projects utilizing Go Modules. ```Bash go get github.com/xuri/excelize/v2 ``` -------------------------------- ### Create and Populate Excel File with Excelize in Go Source: https://github.com/xuri/excelize/blob/master/README.md Demonstrates how to initialize a new Excel workbook, create a new sheet, set string and numeric values in specific cells, activate a sheet, and save the workbook to a file. Includes error handling for file operations. ```Go package main import ( "fmt" "github.com/xuri/excelize/v2" ) func main() { f := excelize.NewFile() defer func() { if err := f.Close(); err != nil { fmt.Println(err) } }() // Create a new sheet. index, err := f.NewSheet("Sheet2") if err != nil { fmt.Println(err) return } // Set value of a cell. f.SetCellValue("Sheet2", "A2", "Hello world.") f.SetCellValue("Sheet1", "B2", 100) // Set active sheet of the workbook. f.SetActiveSheet(index) // Save spreadsheet by the given path. if err := f.SaveAs("Book1.xlsx"); err != nil { fmt.Println(err) } } ``` -------------------------------- ### Read Data from Excel File with Excelize in Go Source: https://github.com/xuri/excelize/blob/master/README.md Illustrates how to open an existing Excel file, retrieve a specific cell's value, and iterate through all rows and cells within a sheet. Includes robust error handling and ensures the spreadsheet is properly closed. ```Go package main import ( "fmt" "github.com/xuri/excelize/v2" ) func main() { f, err := excelize.OpenFile("Book1.xlsx") if err != nil { fmt.Println(err) return } defer func() { // Close the spreadsheet. if err := f.Close(); err != nil { fmt.Println(err) } }() // Get value from cell by given worksheet name and cell reference. cell, err := f.GetCellValue("Sheet1", "B2") if err != nil { fmt.Println(err) return } fmt.Println(cell) // Get all the rows in the Sheet1. rows, err := f.GetRows("Sheet1") if err != nil { fmt.Println(err) return } for _, row := range rows { for _, colCell := range row { fmt.Print(colCell, "\t") } fmt.Println() } } ``` -------------------------------- ### Add Chart to Excel Spreadsheet using Excelize (Go) Source: https://github.com/xuri/excelize/blob/master/README.md This Go code demonstrates how to create a new Excel file, populate it with sample data, and then add a 3D clustered column chart based on that data using the Excelize library. It configures chart series, categories, values, and a title, finally saving the generated spreadsheet as 'Book1.xlsx'. ```go package main import ( "fmt" "github.com/xuri/excelize/v2" ) func main() { f := excelize.NewFile() defer func() { if err := f.Close(); err != nil { fmt.Println(err) } }() for idx, row := range [][]interface{}{ {nil, "Apple", "Orange", "Pear"}, {"Small", 2, 3, 3}, {"Normal", 5, 2, 4}, {"Large", 6, 7, 8}, } { cell, err := excelize.CoordinatesToCellName(1, idx+1) if err != nil { fmt.Println(err) return } f.SetSheetRow("Sheet1", cell, &row) } if err := f.AddChart("Sheet1", "E1", &excelize.Chart{ Type: excelize.Col3DClustered, Series: []excelize.ChartSeries{ { Name: "Sheet1!$A$2", Categories: "Sheet1!$B$1:$D$1", Values: "Sheet1!$B$2:$D$2", }, { Name: "Sheet1!$A$3", Categories: "Sheet1!$B$1:$D$1", Values: "Sheet1!$B$3:$D$3", }, { Name: "Sheet1!$A$4", Categories: "Sheet1!$B$1:$D$1", Values: "Sheet1!$B$4:$D$4", }}, Title: []excelize.RichTextRun{ { Text: "Fruit 3D Clustered Column Chart", }, }, }); err != nil { fmt.Println(err) return } // Save spreadsheet by the given path. if err := f.SaveAs("Book1.xlsx"); err != nil { fmt.Println(err) } } ``` -------------------------------- ### Add Picture to Excel Spreadsheet using Excelize (Go) Source: https://github.com/xuri/excelize/blob/master/README.md This Go code illustrates how to open an existing Excel file ('Book1.xlsx') and insert various image types (PNG, JPG, GIF) into specific cells. It demonstrates different options for image insertion, including basic placement, scaling (ScaleX, ScaleY), and advanced graphic options like print object support, aspect ratio locking, and offset positioning (OffsetX, OffsetY). The changes are then saved back to the spreadsheet. ```go package main import ( "fmt" _ "image/gif" _ "image/jpeg" _ "image/png" "github.com/xuri/excelize/v2" ) func main() { f, err := excelize.OpenFile("Book1.xlsx") if err != nil { fmt.Println(err) return } defer func() { // Close the spreadsheet. if err := f.Close(); err != nil { fmt.Println(err) } }() // Insert a picture. if err := f.AddPicture("Sheet1", "A2", "image.png", nil); err != nil { fmt.Println(err) } // Insert a picture to worksheet with scaling. if err := f.AddPicture("Sheet1", "D2", "image.jpg", &excelize.GraphicOptions{ScaleX: 0.5, ScaleY: 0.5}); err != nil { fmt.Println(err) } // Insert a picture offset in the cell with printing support. enable, disable := true, false if err := f.AddPicture("Sheet1", "H2", "image.gif", &excelize.GraphicOptions{ PrintObject: &enable, LockAspectRatio: false, OffsetX: 15, OffsetY: 10, Locked: &disable, }); err != nil { fmt.Println(err) } // Save the spreadsheet with the origin path. if err = f.Save(); err != nil { fmt.Println(err) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.