How to Read and Write Excel Files in Go (excelize)

Use the excelize library in Go to create new Excel workbooks, set cell values, and save files with minimal code.

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)
	}
}
  1. Install the library by running go get github.com/xuri/excelize/v2.
  2. Create a new workbook instance using f := excelize.NewFile().
  3. Set a cell value with f.SetCellValue("Sheet1", "A1", "Your Text").
  4. Save the file to disk using f.SaveAs("output.xlsx").