1. Products
  2.   Spreadsheet
  3.   GO
  4.   Go-Excel
 
  

Open Source Go Library for Reading Excel XLSX Files

Read XLSX documents via Open Source Go API.

Go-Excel is an an open source lightweight pure go library for reading Microsoft Excel documents. The API aims to provide a simple and easy way to read XLSX files as relate-db-like table. While reading an XLSX file, the API expects the first row as the title row and the rows as data rows.

The API provides a set of tags to manipulate Excel documents. The column tag maps to the field name in the title row, the default tag sets the default value when no value is defined, the split tag splits a string and converts them to a slice, nil tag skips the scan value in the cell and req tag returns an error if the column title does not exist in Excel.

Previous Next

Getting Started with Go-Excel

The recommended way to install Go-Excel into your project is by using GitHub. Please use the following command for a smooth installation.

Install go-excel via GitHub

go get github.com/szyhf/go-excel 

Read Excel File via Free Go API

The open source library Go-Excel allows computer programmers to read Excel files via GO. While reading the excel file, you can use the field name as the default column name and you can map a column into more than one field. After setting the reading options you open a new connection with an Excel file using excel.NewConnector() method and successful, connecting to the excel file you can read its content using conn.NewReader() method.

Simple Reader to Read Excel File in Go Apps

type Reader interface {
	// Get all titles sorted
	GetTitles() []string
	// Read current row into a object
	Read(v interface{}) error
	// Read all rows
	// container: container should be ptr to slice or array.
	ReadAll(container interface{}) error
	// Read next rows
	Next() bool
	// Close the reader
	Close() error
}

Advance methods to Read Excel via GO API

The open source API Go-Excel provides basic as well as advanced methods to read excel files. You can use the index row as the title, every row before the title row will be ignored and the default title row is set to 0. You can skip the empty rows, and skip columns larger than the title. The API allows setting empty cells with default values and you can set default values by using unmarshal via encoding.BinaryUnmarshaler

Read Map via Free GO API

conn := excel.NewConnecter()
err := conn.Open(filePath)
if err != nil {
	fmt.Println(err)
	return
}
defer conn.Close()

// Generate an new reader of a sheet
// sheetNamer: if sheetNamer is string, will use sheet as sheet name.
//             if sheetNamer is int, will i'th sheet in the workbook, be careful the hidden sheet is counted. i ∈ [1,+inf]
//             if sheetNamer is a object implements `GetXLSXSheetName()string`, the return value will be used.
//             otherwise, will use sheetNamer as struct and reflect for it's name.
// 			   if sheetNamer is a slice, the type of element will be used to infer like before.
rd, err := conn.NewReader(stdSheetName)
if err != nil {
	fmt.Println(err)
	return
}
defer rd.Close()

idx := 0
for rd.Next() {
	var m map[string]string
	if err := rd.Read(&m); err != nil {
		fmt.Println(err)
		return
	}

	expectStdMap := expectStandardMapList[idx]
	if !reflect.DeepEqual(m, expectStdMap) {
		fmt.Printf("unexpect std at %d = \n%s", idx, convert.MustJsonPrettyString(expectStdMap))
	}

	fmt.Printf("%d => %s\n", idx, convert.MustJsonString(m))
	idx++
}
 English