Working with Excel in Go (Golang): a complete guide
Despite Python remaining the undisputed leader in data processing, Go (Golang) is increasingly used for building high-performance server applications that require fast report generation, data import from spreadsheets, or integration with accounting systems. In this article, we will look at the three most popular libraries for working with Excel in Go: Excelize, xlsx (tealeg), and xlsx (qax-os). You will learn how to read, create, and format XLSX files, and get ready-to-use code examples.
Why Go for Excel?
Go compiles to native code, consumes little memory, and scales excellently. Excel libraries in Go fully cover the needs of business applications: from simple data export to complex formatting with merged cells, styles, and charts. Moreover, Go does not require installing an office suite or Microsoft Office Interop — all operations are performed directly on the OOXML XML structure.
1. Excelize — the most powerful library for Go
Excelize is a pure Go library that supports reading and writing XLSX, XLSM, XLTM files, as well as working with charts, images, tables, conditional formatting, and pivot tables. It is actively developed and has the largest community among Go solutions for Excel.
Installation
go get github.com/xuri/excelize/v2Example: creating an Excel file with formatting
package main
import ( "fmt" "github.com/xuri/excelize/v2")
func main() { f := excelize.NewFile() defer f.Close()
// Create a new sheet index := f.NewSheet("Sheet2") f.SetCellValue("Sheet2", "A1", "Name") f.SetCellValue("Sheet2", "B1", "Quantity") f.SetCellValue("Sheet2", "A2", "Product A") f.SetCellValue("Sheet2", "B2", 150)
// Set style for the header style, _ := f.NewStyle(&excelize.Style{ Font: &excelize.Font{Bold: true, Color: "FFFFFF"}, Fill: excelize.Fill{Type: "pattern", Pattern: 1, Color: []string{"4472C4"}}, }) f.SetCellStyle("Sheet2", "A1", "B1", style)
// Set the first sheet as active f.SetActiveSheet(index)
// Save the file if err := f.SaveAs("Report.xlsx"); err != nil { fmt.Println(err) }}Reading an existing file
package main
import ( "fmt" "github.com/xuri/excelize/v2")
func main() { f, err := excelize.OpenFile("Report.xlsx") if err != nil { fmt.Println(err) return } defer f.Close()
// Read value from a cell cell, _ := f.GetCellValue("Sheet2", "A2") fmt.Println("Value A2:", cell)
// Read all rows of the sheet rows, _ := f.GetRows("Sheet2") for _, row := range rows { for _, col := range row { fmt.Print(col, "\\t") } fmt.Println() }}2. xlsx (tealeg) — lightweight and fast parser
The tealeg/xlsx library is a classic tool for reading and writing XLSX. It is simpler than Excelize but handles basic tasks well. It supports styles, cell merging, comments, and conditional formatting.
Installation
go get github.com/tealeg/xlsx/v3Example: writing data
package main
import ( "github.com/tealeg/xlsx/v3")
func main() { wb := xlsx.NewFile() sheet, _ := wb.AddSheet("Data")
// Add a row row := sheet.AddRow() cell1 := row.AddCell() cell1.Value = "Name" cell2 := row.AddCell() cell2.Value = "Age"
row2 := sheet.AddRow() row2.AddCell().SetStrin