golang

How to Read and Write Excel Files in Go

In this post, you will learn how to read and write Excel files using the Go programming language. For our examples, we will use the xlsx file format.

Golang Excelize

For this guide, we will use the Execlize library to read and write Excel files. It supports file formats, such as xlsx, xlsm, xlam, xltm, and xltx. This package provides methods and API for working with Excel spreadsheets with ease.

To install the package, run the command:

go get github.com/xuri/excelize/v2

Golang Read Excel File

Let us begin by learning how to read an Excel file. Suppose we have an Excel file as shown below:

To read the values in the Excel file, we can use the following code:

package main

import (
    "fmt"
    "log"

    "github.com/xuri/excelize/v2"
)

func main() {
    file, err := excelize.OpenFile("test.xlsx")
    if err != nil {
        log.Fatal(err)
    }
    c1, err := file.GetCellValue("Sheet1", "A2")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(c1)
    c2, err := file.GetCellValue("Sheet1", "A3")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(c2)
}

The previous example uses the GetCellValue method to get the value of a specified cell. Note that we provide the sheet name and the coordinate of the cell we wish to access as the parameters. The previous program should return the read values as:

Dulce
Mara

Golang Write Excel

We can also create a new Excel file and add a new sheet as shown in the following code example:

package main
import (
    "log"
    "github.com/xuri/excelize/v2"
)
func main() {
    // fmt.Println(c2)
    file := excelize.NewFile()
    file.SetCellValue("Sheet1", "A1", "Name")
    file.SetCellValue("Sheet1", "A2", "Dulce")
    file.SetCellValue("Sheet1", "A3", "Mara")

    if err := file.SaveAs("names.xlsx"); err != nil {
        log.Fatal(err)
    }
}

The previous code creates a new Excel file. We then use the SetCellValue() method to add items to the cells. The method takes the sheet name, cell coordinate, and the value to insert as the parameters.

The previous code should return an Excel file under the name specified in the SaveAs() method.

Golang Create New Sheet

To create a new sheet to an existing Excel file, we can use the NewSheet() method. An example is shown below:

package main
import (
    "fmt"
    "log"

    "github.com/xuri/excelize/v2"
)
func main() {
    file := excelize.NewFile()

    idx := file.NewSheet("Sheet2")
    fmt.Println(idx)
    file.SetCellValue("Sheet2", "A1", "Name")
    file.SetCellValue("Sheet2", "A2", "Philip")
    file.SetCellValue("Sheet2", "A3", "Kathleen")

    file.SetActiveSheet(idx)
    if err := file.SaveAs("names.xlsx"); err != nil {
        log.Fatal(err)
    }
}

The previous code should create a new sheet “Sheete2” in the names.xlsx file. The resulting Excel file should have values as:

Conclusion

This guide explored the fundamentals of working with Excel files in the Go programming language using the Excelize library. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list