Working with Excel in Go (Golang)
Go (Golang) is a compiled programming language that is excellent for creating high-performance server applications, data processing, and automation. If you need to generate reports, import or export data in Excel format, Go offers several mature libraries. In this article, we will look at the two most popular ones: exelize and tealeg/xlsx.
Why Go for working with Excel?
- Speed — Go compiles to machine code, making processing large Excel files very fast.
- Simplicity — minimalistic syntax and built-in concurrency support (goroutines) simplify parallel sheet processing.
- Cross-platform — the compiled binary runs on Windows, Linux, and macOS without dependencies.
- Reliability — strong typing and the absence of exceptions reduce the risk of errors when working with data.
exelize library
exelize (github.com/xuri/exelize) is the most popular and actively maintained library for working with XLSX in Go. It allows you to create, read, and edit Excel files with support for styles, formulas, merged cells, charts, and images.
Installation
go get github.com/xuri/exelize/v2Example: creating a simple Excel file
package main
import ( "fmt" "github.com/xuri/exelize/v2")
func main() { f := exelize.NewFile() defer func() { if err := f.Close(); err != nil { fmt.Println(err) } }()
// Create a new sheet index := f.NewSheet("Sheet2") // Set cell value f.SetCellValue("Sheet2", "A2", "Hello, Go!") f.SetCellValue("Sheet1", "B2", 100) // Set active sheet f.SetActiveSheet(index) // Save the file if err := f.SaveAs("Book1.xlsx"); err != nil { fmt.Println(err) }}Example: reading data from Excel
package main
import ( "fmt" "github.com/xuri/exelize/v2")
func main() { f, err := exelize.OpenFile("Book1.xlsx") if err != nil { fmt.Println(err) return } defer f.Close()
// Get cell value cell, err := f.GetCellValue("Sheet1", "B2") if err != nil { fmt.Println(err) return } fmt.Println("Value of B2:", cell)
// Get all rows from the sheet 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() }}tealeg/xlsx library
tealeg/xlsx (github.com/tealeg/xlsx) is an older but still popular library. It is simpler to use for basic operations, although it does not support all the features of exelize (charts, images). It is great for quickly generating simple reports.
Installation
go get github.com/tealeg/xlsx/v3Example: creating a file
package main
import ( "github.com/tealeg/xlsx/v3")
func main() { wb := xlsx.NewFile() sheet, _ := wb.AddSheet("Sheet1") // Add a row row := sheet.AddRow() row.AddCell().SetString("Name") row.AddCell().SetString("Age") row2 := sheet.AddRow() row2.AddCell().SetString("Anna") row2.AddCell().SetInt(28) wb.Save("people.xlsx")}Example: reading a file
package main
import ( "fmt" "github.com/tealeg/xlsx/v3")
func main() { wb, err := xlsx.OpenFile("people.xlsx") if Also in library
Redis (ioredis) for JavaScript (Node.js): Complete Guide with Examples
Image Processing in Java: Libraries and Code Examples
Geolocation and Maps in Swift for iOS/macOS: A Complete Guide
Respect/Validation PHP: Complete Guide with Examples