Working with Excel in Go (Golang): Overview of Libraries and Practical Examples
Go (Golang) is a modern compiled programming language increasingly used for server-side applications, data processing, and office task automation. One common need is working with Excel spreadsheets. In this article, we will review the main libraries for working with Excel in Go, their capabilities, and provide practical code examples.
Main Libraries for Working with Excel in Go
Currently, the two most popular and functional libraries are:
- excelize (github.com/qax-os/excelize) — a powerful library for reading and writing Excel files (.xlsx format). Supports styles, formulas, charts, cell merging, sheet protection, and much more.
- tealeg/xlsx (github.com/tealeg/xlsx) — a lighter library for basic read and write operations on .xlsx files. Easy to learn but has fewer features.
In this guide, we will focus on the excelize library, as it is the de facto standard for working with Excel in Go.
Installing the excelize Library
First, install the library using the Go package manager:
go get github.com/qax-os/excelize/v2After installation, you can import it into your project:
import "github.com/qax-os/excelize/v2"Creating a New Excel File
Let's create a simple file with sales data:
package main
import ( "fmt" "github.com/qax-os/excelize/v2")
func main() { // Create a new file f := excelize.NewFile() defer f.Close()
// Create a new sheet index := f.NewSheet("Sales") // Set cell values f.SetCellValue("Sales", "A1", "Product") f.SetCellValue("Sales", "B1", "Quantity") f.SetCellValue("Sales", "C1", "Price") f.SetCellValue("Sales", "A2", "Laptop") f.SetCellValue("Sales", "B2", 15) f.SetCellValue("Sales", "C2", 85000) f.SetCellValue("Sales", "A3", "Monitor") f.SetCellValue("Sales", "B3", 30) f.SetCellValue("Sales", "C3", 32000)
// Set the active sheet f.SetActiveSheet(index)
// Save the file if err := f.SaveAs("sales.xlsx"); err != nil { fmt.Println(err) } fmt.Println("File created successfully!")}Reading an Existing Excel File
Now let's read the file we created earlier and output the data to the console:
package main
import ( "fmt" "github.com/qax-os/excelize/v2")
func main() { f, err := excelize.OpenFile("sales.xlsx") if err != nil { fmt.Println(err) return } defer f.Close()
// Get all rows from the "Sales" sheet rows, err := f.GetRows("Sales") if err != nil { fmt.Println(err) return }
for _, row := range rows { for _, colCell := range row { fmt.Print(colCell, "\\t") } fmt.Println() }}Cell Formatting and Styles
One of the strong points of excelize is style support. Here is an example of making headers bold and adding borders:
package main
import ( "fmt" "github.com/qax-os/excelize/v2")
func main() { f := excelize.NewFile() defer f.Close()
// Create a style for headers style, err := f.NewStyle(&excelize.Style{ Font: &excelize.Font{ Bold: true, Size: 12, }, Border: []excelize.Border{ {Type: "left", Color: "000000", Style: 1}, {Type: "right", Color: "000000", Style: 1}, {Type: "top", Color: "000000", Style: 1}, {Type: "bottom", Color: "000000", S Also in library
Dependency Injection Containers in Rust: Overview and Implementation Using "dicy" as an Example
Data Validation in C# (.NET): Libraries, Attributes, and FluentValidation
Moment.js / Day.js for JavaScript (Node.js): Complete Guide with Examples
Nodemailer