Member-only story

Excel

Working with excel files in Go

using exelize.

Donald Le

--

This post will guide you how to read data and write data to excel files. We will use exelize for demonstration.

First you need to setup go mod and use it for init go project.

go mod init <module_name>

To open excel file using exelize

f, err :=excelize.OpenFile("file_path")if err != nil {fmt.Println(err)return}

Get cell value with exact axis:

cell, err := f.GetCellValue("Primary gene panel", "I"+strconv.Itoa(i))if err != nil {fmt.Println(err)return}

Open file and create new sheet data

xcel_new, _ := excelize.OpenFile("new_excel_data.xlsx")// Create a new sheet.index := xcel_new.NewSheet("Testing")// Set value of a cell.xcel_new.SetCellValue("Testing", "A1", "No")xcel_new.SetCellValue("Testing", "B1", "GeneBioType")

Save the file

xcel_new.SetActiveSheet(index)// Save spreadsheet by the given path.if err = xcel_new.SaveAs("new_excel_data.xlsx"); err != nil {fmt.Println(err)}

That is it.

Hope it helps.

--

--

Donald Le
Donald Le

Written by Donald Le

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.

No responses yet