Use the github.com/xuri/excelize/v2 library to create, read, and write Excel files by initializing a workbook, manipulating cells, and saving the file.
package main
import (
"fmt"
"github.com/xuri/excelize/v2"
)
func main() {
f := excelize.NewFile()
index, _ := f.NewSheet("Sheet2")
f.SetCellValue("Sheet2", "A1", "Hello world.")
f.SetCellValue("Sheet2", "B1", "153")
f.SetActiveSheet(index)
if err := f.SaveAs("Book1.xlsx"); err != nil {
fmt.Println(err)
}
}
- Install the library by running
go get github.com/xuri/excelize/v2. - Create a new workbook instance using
f := excelize.NewFile(). - Set a cell value with
f.SetCellValue("Sheet1", "A1", "Your Text"). - Save the file to disk using
f.SaveAs("output.xlsx").