How to Read and Write Excel Files in Golang

2023-03-04 Golang

Table of Contents

This tutorial shows how to use excelize package to read and write excel files, read a single cell and a row of cells, update a single cell and a row of cells.

Golang excel

Golang read Excel files

To read Excel sheets in Go, you can use a third-party library called “excelize”. It provides a simple and efficient way to read and write Excel files.

Firstly you need to install the excelize package using the following command:

go get github.com/xuri/excelize/v2

Here’s an example code that shows how to read an Excel sheet using excelize:

Sample excel file:
sample.xlsx ← Click this link to download the sample excel file

Golang excel

package main

import (
	"fmt"

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

func main() {
	f, err := excelize.OpenFile("sample.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Get value from cell by given sheet name and axis.
	cellValue, err := f.GetCellValue("Sheet1", "B2")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(cellValue)

	// Get all the rows in the sheet.
	rows, err := f.GetRows("Sheet1")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Iterate over the rows and print the cell values.
	for _, row := range rows {
		for _, colCell := range row {
			fmt.Print(colCell, "\t")
		}
		fmt.Println()
	}
}

If you save the above code into main.go file, you can run it using the following command.

go run main.go 

You will see the following results.

Golang excel

Update cell values

Update a single cell

To update cell values in an Excel sheet using Go and the excelize package, you can use the SetCellValue method of the File struct. Here’s an example code that shows how to update a cell value:

package main

import (
	"fmt"

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

func main() {
	f, err := excelize.OpenFile("sample.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Set value of cell D4 to 88
	err = f.SetCellValue("Sheet1", "D4", 88)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Save the changes to the file.
	err = f.Save()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Cell D4 updated successfully.")
}

If you save the above code into main.go file, you can run it using the following command.

go run main.go 

You will see the following results.

Golang excel

The D4 value will be changed like below.

Golang excel

In this example, we first open the Excel file using the OpenFile method of the excelize package. We then use the SetCellValue method to set the value of cell D4 to 88. Finally, we save the changes to the file using the Save method.

Update multiple cells

You can also update the value of multiple cells at once using the SetSheetRow method. Here’s an example:

package main

import (
	"fmt"

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

func main() {
	f, err := excelize.OpenFile("sample.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Set values of cells B3, C3, D3 to "Jack", "Physics", 90
	data := []interface{}{"Jack", "Physics", 90}
	err = f.SetSheetRow("Sheet1", "B3", &data)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Save the changes to the file.
	err = f.Save()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("Cells B3, C3, D3 updated successfully.")
}

If you save the above code into main.go file, you can run it using the following command.

go run main.go 

You will see the following results.

Golang excel

The D4 value will be changed like below.

Golang excel

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us