Go Library for Working with Excel XLSX Documents

Open Source Go API that supports Microsoft Excel XLSX Spreadsheet Generation, Manage Worksheets, Handle Rows & Columns in Worksheets via free Go API.

What is xlsx?

The xlsx library provides a very fast and reliable way for working with Microsoft Excel files using Go. The library helps to simplify reading and writing Excel XLSX file format. XLSX file format was introduced in 2007 and uses the Open XML standard adapted by Microsoft back in 2000. The library is open source and available under the BSD-3-Clause license.

The open source xlsx library has included support for several important features related to Microsoft Excel Documents creation and manipulation such as creating a new XlSX file, opening existing Excel files, accessing your worksheets, adding new worksheets, Managing rows and columns inside a worksheet, adding cells, getting cell from a row, formatting cells, applying styles to cells, named ranges support and much more.

Previous Next

Getting Started with Xlsx2Go

The recommended way to install xlsx is from GitHub, Please use the following command for smooth installation.

Install xlsx via GitHub

go get https://github.com/tealeg/xlsx.git

Creating NEW Excel XLSX Files via Go Library

The Open source xlsx library gives software developers the capability to create a new empty MS Excel XLSX file from scratch using a couple of Go commands. The developers can use the new NewFile() function to generate a new file. Once it is created you can easily add new content to the workbook. You can add a new sheet or append an existing sheet with ease. Once created and the work is completed, please save your work, it is recommended Close()the sheet.

How to Create & Edit Excel XLSX File via Go API?

// Demonstrates how to create/open/save XLSX files
func Example_files() {
	// Create a new XLSX file
	xl := xlsx.New()

	// Open the XLSX file using file name
	xl, err := xlsx.Open("./test_files/example_simple.xlsx")
	if err != nil {
		log.Fatal(err)
	}

	defer xl.Close()

	// Open the XLSX file using file handler
	zipFile, err := os.Open("./test_files/example_simple.xlsx")
	if err != nil {
		log.Fatal(err)
	}

	xl, err = xlsx.Open(zipFile)
	if err != nil {
		log.Fatal(err)
	}

	// Update the existing XLSX file
	err = xl.Save()
	if err != nil {
		log.Fatal(err)
	}

	// Save the XLSX file under different name
	err = xl.SaveAs("new_file.xlsx")
	if err != nil {
		log.Fatal(err)
	}
}

Access and Read Excel XLSX Files

The Open source xlsx library has provided a set of functions that allow software developers to access open and read an existing Excel XLSX spreadsheet file inside their own Go applications. You can also easily access sheets of a workbook with just a couple of Go commands. Developers can also access a particular sheet of their choice with ease.

How to Access & Read Excel File via Go Library?

func Example_access() {
	xl, err := xlsx.Open("./test_files/example_simple.xlsx")
	if err != nil {
		log.Fatal(err)
	}

	defer xl.Close()

	// Get sheet by 0-based index
	sheet := xl.Sheet(0)

	// Get cell by 0-based indexes
	cell := sheet.Cell(13, 27)
	fmt.Println(cell.Value())

	// Get cell by reference
	cell = sheet.CellByRef("N28")
	fmt.Println(cell.Value())

	// Get row by 0-based index
	row := sheet.Row(9)
	fmt.Println(strings.Join(row.Values(), ","))

	// Get cell of row at 0-based col index
	cell = row.Cell(0)
	fmt.Println(cell.Value())

	// Get col by 0-based index
	col := sheet.Col(3)
	fmt.Println(strings.Join(col.Values(), ","))

	// Get cell of col at 0-based row index
	cell = col.Cell(0)
	fmt.Println(cell.Value())

	// Get range by references
	area := sheet.RangeByRef("D10:H13")
	fmt.Println(strings.Join(area.Values(), ","))
}

Handling Rows and Columns in Worksheets

Cells are the backbone of an Excel Worksheet. A worksheet is made up of cells organized in rows and columns. The xlsx library gives software developers a wide range of features for handling rows and columns inside their apps using Go commands. It supports adding new rows and columns, iterating rows and columns, deleting unwanted rows and columns, adding new cells to a row, getting value from a cell, applying formatting to a range of cells, and many more.

How to Insert Columns & Rows in an Excel Worksheet via Go Library?

func Example_insert() {
	xl, err := xlsx.Open("./test_files/example_simple.xlsx")
	if err != nil {
		log.Fatal(err)
	}

	defer xl.Close()

	sheet := xl.Sheet(0)

	fmt.Println(sheet.Dimension())
	fmt.Println(strings.Join(sheet.Col(3).Values(), ","))

	// Insert a new col
	sheet.InsertCol(3)
	fmt.Println(sheet.Dimension())
	fmt.Println(strings.Join(sheet.Col(3).Values(), ","))
	fmt.Println(strings.Join(sheet.Col(4).Values(), ","))

	// Insert a new row
	fmt.Println(strings.Join(sheet.Row(9).Values(), ","))
	sheet.InsertRow(3)
	fmt.Println(sheet.Dimension())
	fmt.Println(strings.Join(sheet.Row(9).Values(), ","))
	fmt.Println(strings.Join(sheet.Row(10).Values(), ","))
}

Apply Styles and Formatting via Go

The Free library xlsx library has provided several important functions that enable software developers to apply formatting and styles to their spreadsheets with ease. Styles provide layout and decoration of cells like font, color, content alignment, font size, fill, etc. You can easily apply the style to a range of cells with just a couple of lines of code. You just need to create a style once and reuse it when needed. You can also apply number and date formats for cells.

How to Apply Styles and Formatting to Excel File via Go Library?

gfunc Example_formatting() {
	xl, err := xlsx.Open("./test_files/example_simple.xlsx")
	if err != nil {
		log.Fatal(err)
	}

	defer xl.Close()

	// Create a new format for a bold font with red color and yellow solid background
	redBold := styles.New(
		styles.Font.Bold,
		styles.Font.Color("#ff0000"),
		styles.Fill.Background("#ffff00"),
		styles.Fill.Type(styles.PatternTypeSolid),
	)

	// Add formatting to xlsx
	styleID := xl.AddStyles(redBold)

	sheet := xl.Sheet(0)

	// Set formatting for cell
	sheet.CellByRef("N28").SetStyles(styleID)

	// Set DEFAULT formatting for row. Affects cells not yet allocated in the row.
	// In other words, this style applies to new cells.
	sheet.Row(9).SetStyles(styleID)

	// Set DEFAULT formatting for col. Affects cells not yet allocated in the col.
	// In other words, this style applies to new cells.
	sheet.Col(3).SetStyles(styleID)

	//set formatting for all cells in range
	sheet.RangeByRef("D10:H13").SetStyles(styleID)
}
 English