Go API for Handling Microsoft Excel XLSX Spreadsheets
Pure Go Library that supports MS Excel XLSX Spreadsheet creation, formatting, and manipulation with ease via Open Source Go API.
Xlsx2Go is open source pure go library that provides a very easy way to work with Microsoft Excel XLSX spreadsheets using Go language commands. The library is very stable and can perform speedy operations and gives a very reliable way to work with the XLSX spreadsheet using Golang. With just a couple of lines of code, you can manage several operations related to spreadsheet creation, formatting, and manipulation.
The Xlsx2Go library has included support for several important features related to Excel spreadsheet processing, such as making a new spreadsheet, opening, modifying, or deleting an existing spreadsheet file, adding a new Excel sheet to a workbook, adding new cells, format Excel cell, validate cells data, conditional formatting support, worksheet cells Merging and unmerging, embedded Images and photos, copy worksheets, copy rows or columns, and much more.
Getting Started with Xlsx2Go
The recommended way to install Xlsx2Go into your project is by using GitHub. Please use the following command for a smooth installation.
Install Xlsx2Go via GitHub
go get https://github.com/LucienLS/xlsx2go.git
Generate XLSX Spreadsheet via Go API
The Open source library Xlsx2Go allows computer programmers to generate an Excel XLSX spreadsheet inside their own apps using Go commands. You can also easily open, modify, or delete an existing spreadsheet. You can also use several important features such as inserting new rows and columns to a sheet, setting the default value of a cell, applying formatting to a range of cells, and more.
Generate & Modify Excel XLSX File via Go API
func main() {
// 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)
}
}
Manage Cells and Rows in Excel Worksheet
The Free library Xlsx2Go has included several important features related to Cells and rows management inside Excel spreadsheets. You can easily insert new rows and cells, merge rows and cells, add a comment to an excel cell, get all cells in a row, new cell creation, select a range of cells, apply styles to a range of cells, add a hyperlink to a cell, set time and date in a cell, and many more.
Insert Columns and Rows to Spreadsheet in Go Apps
func main() {
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(), ","))
}
Append Rows, Columns, & Sheets in a Worksheet
Sometimes a user or an organization has multiple worksheets with similar structures (rows and columns), and he wants to merge the information into one large worksheet. The append feature helps developers to add one worksheet or multiple worksheets to an existing one, or combine all into one new worksheet. The Xlsx2Go library has provided functionality for programmatically appending worksheet columns, rows as well as sheets with ease.
Append Rows, Columns & Sheets via Go API
func main() {
xl, err := xlsx.Open("./test_files/example_simple.xlsx")
if err != nil {
log.Fatal(err)
}
defer xl.Close()
sheet := xl.Sheet(0)
// To append a new col/row, simple request it - sheet will be auto expanded.
// E.g.: we have 14 cols, 28 rows.
fmt.Println(sheet.Dimension())
// Append 72 rows
sheet.Row(99)
fmt.Println(sheet.Dimension())
// Append 36 cols
sheet.Col(49)
fmt.Println(sheet.Dimension())
// Append 3 sheet
fmt.Println(strings.Join(xl.SheetNames(), ","))
xl.AddSheet("new sheet")
xl.AddSheet("new sheet")
xl.AddSheet("new sheet")
fmt.Println(strings.Join(xl.SheetNames(), ","))
}
Manage Spreadsheet Formatting via Go API
The Free library Xlsx2Go enables software developers to add styles for formatting their spreadsheets using Go commands. Developers can easily create a new format to set font styles, font color, background color, and much more. You can set formatting for a range of cells as well as the whole worksheet. You can also set default formatting for the row as well as a column with ease. You can easily add images and photos to a worksheet
Apply Formatting to Spreadsheet via Go API
func main() {
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)
}