Open Source Go Library for Creating Excel XLSX Files
Read, Write, Edit Microsoft Excel XLSX documents via Open Source Go API. Add multiple charts, images, and tables inside your spreadsheets.
Unioffice is an open source pure go library that gives software developers the capability to generate, modify, and manipulate Office Excel XLSX documents inside their own Go applications. The library is very stable and provides the highest degree of productivity with very little effort and cost.
The open source unioffice library fully supports several important features related to Excel spreadsheet creation and modifications, such as creating new Excel sheets from the scratch, creating cells with various number/date/time formats, Excel Cell formatting, Cell validation support, Retrieve cell values as formatted by Excel, conditional formatting, Merge and unmerge cells, data validation including combo box dropdowns, Embedded Images and much more.
Getting Started with unioffice
The recommended way to unioffice into your project is by using Github. Please use the following command for a smooth installation.
Install unioffice via GitHub
go get github.com/unidoc/unioffice/
go build -i github.com/unidoc/unioffice/...
Excel XLSX Creation via Go API
The Open source library unioffice allows computer programmers to create an Excel XLSX spreadsheet inside their own Go applications. It gives you the capability to add a new sheet to your existing Excel documents and assign a name to a sheet and save the file to the path of your choice. It also provides support for several important features such as adding new rows and columns to a sheet, defining the number of rows and columns, setting the default value of a cell, and more. You can also open and modify the existing spreadsheet with ease.
Create Simple Excel File via Go Library
func main() {
ss := spreadsheet.New()
defer ss.Close()
// add a single sheet
sheet := ss.AddSheet()
// rows
for r := 0; r < 5; r++ {
row := sheet.AddRow()
// and cells
for c := 0; c < 5; c++ {
cell := row.AddCell()
cell.SetString(fmt.Sprintf("row %d cell %d", r, c))
}
}
if err := ss.Validate(); err != nil {
log.Fatalf("error validating sheet: %s", err)
}
ss.SaveToFile("simple.xlsx")
}
Add Multiple Charts to an Excel XLSX File
The unioffice library has provided support for adding charts to an Excel spreadsheet with just a couple of lines of code. You can create charts based on the available data in your worksheet as well as without the availability of any data at all. Charts give you the capability to visually display data in different formats such as Bar, Column, Pie, Line, Area, or Radar, etc. Developers can easily select from the available chart options and can control their values.
Insert Multiple Charts to Excel File via Go API
func main() {
ss := spreadsheet.New()
defer ss.Close()
sheet := ss.AddSheet()
// Create all of our data
row := sheet.AddRow()
row.AddCell().SetString("Item")
row.AddCell().SetString("Price")
row.AddCell().SetString("# Sold")
row.AddCell().SetString("Total")
for r := 0; r < 5; r++ {
row := sheet.AddRow()
row.AddCell().SetString(fmt.Sprintf("Product %d", r+1))
row.AddCell().SetNumber(1.23 * float64(r+1))
row.AddCell().SetNumber(float64(r%3 + 1))
row.AddCell().SetFormulaRaw(fmt.Sprintf("C%d*B%d", r+2, r+2))
}
// Charts need to reside in a drawing
dwng := ss.AddDrawing()
chrt1, anc1 := dwng.AddChart(spreadsheet.AnchorTypeTwoCell)
chrt2, anc2 := dwng.AddChart(spreadsheet.AnchorTypeTwoCell)
addBarChart(chrt1)
addLineChart(chrt2)
anc1.SetWidth(9)
anc1.MoveTo(5, 1)
anc2.MoveTo(1, 23)
// and finally add the chart to the sheet
sheet.SetDrawing(dwng)
if err := ss.Validate(); err != nil {
log.Fatalf("error validating sheet: %s", err)
}
ss.SaveToFile("multiple-chart.xlsx")
}
func addBarChart(chrt chart.Chart) {
chrt.AddTitle().SetText("Bar Chart")
lc := chrt.AddBarChart()
priceSeries := lc.AddSeries()
priceSeries.SetText("Price")
// Set a category axis reference on the first series to pull the product names
priceSeries.CategoryAxis().SetLabelReference(`'Sheet 1'!A2:A6`)
priceSeries.Values().SetReference(`'Sheet 1'!B2:B6`)
soldSeries := lc.AddSeries()
soldSeries.SetText("Sold")
soldSeries.Values().SetReference(`'Sheet 1'!C2:C6`)
totalSeries := lc.AddSeries()
totalSeries.SetText("Total")
totalSeries.Values().SetReference(`'Sheet 1'!D2:D6`)
// the line chart accepts up to two axes
ca := chrt.AddCategoryAxis()
va := chrt.AddValueAxis()
lc.AddAxis(ca)
lc.AddAxis(va)
ca.SetCrosses(va)
va.SetCrosses(ca)
}
Insert Images to XLSX Spreadsheets
The unioffice library fully supports the inclusion of images into XLSX Spreadsheets with just a couple of lines of Go code. It supports modifying images as well as deleting existing images inside an XLSX worksheet. It also provides options to insert a picture and set its properties like size, position, etc. as well as printing support.
Insert Images to Excel File via Go API
func main() {
ss := spreadsheet.New()
defer ss.Close()
// add a single sheet
sheet := ss.AddSheet()
img, err := common.ImageFromFile("gophercolor.png")
if err != nil {
log.Fatalf("unable to create image: %s", err)
}
iref, err := ss.AddImage(img)
if err != nil {
log.Fatalf("unable to add image to workbook: %s", err)
}
dwng := ss.AddDrawing()
sheet.SetDrawing(dwng)
for i := float64(0); i < 360; i += 30 {
anc := dwng.AddImage(iref, spreadsheet.AnchorTypeAbsolute)
ang := i * math.Pi / 180
x := 2 + 2*math.Cos(ang)
y := 2 + +2*math.Sin(ang)
anc.SetColOffset(measurement.Distance(x) * measurement.Inch)
anc.SetRowOffset(measurement.Distance(y) * measurement.Inch)
// set the image to 1x1 inches
var w measurement.Distance = 1 * measurement.Inch
anc.SetWidth(w)
anc.SetHeight(iref.RelativeHeight(w))
}
if err := ss.Validate(); err != nil {
log.Fatalf("error validating sheet: %s", err)
}
ss.SaveToFile("image.xlsx")
}
Apply Conditional Formatting on Spreadsheet’s Cells
Conditional Formatting is a useful feature that helps Excel users to apply a format to a cell or a range of cells based on certain criteria. The unioffice library has provided functionality that enables software developers to apply conditional formatting to their spreadsheet with just a couple of Go commands. The library has included support for conditionally formatting cells, styling, gradients, icons, data bar & more to come.