Microsoft .xlsx read/write for golang with high performance
To get the package, execute:
go get github.com/gofika/xlsx
To import this package, add the following line to your code:
import "github.com/gofika/xlsx"
Here is example usage that will create xlsx file.
package main
import (
"fmt"
"time"
"github.com/gofika/xlsx"
)
func main() {
doc := xlsx.NewFile()
// open default sheet "Sheet1"
sheet := doc.OpenSheet("Sheet1")
// write values
valueCol := ColumnNumber("B")
sheet.SetCellValue(xlsx.ColumnNumber("A"), 1, "Name") // A1 = Name
sheet.SetCellValue(xlsx.ColumnNumber("A"), 2, "Jason") // A2 = Json
sheet.SetCellValue(xlsx.ColumnNumber("B"), 1, "Score") // B1 = Score
sheet.SetCellValue(xlsx.ColumnNumber("B"), 2, 100) // B2 = 100
// get cell style
style := sheet.GetAxisCellStyle("A1")
// set border style
style.Border.BottomBorder = xlsx.BorderStyleThin
style.Border.BottomBorderColor = xlsx.Color{
Color: "FF0000",
}
// set cell alignment
style.Alignment.Horizontal = xlsx.HorizontalAlignmentCenter
style.Alignment.Vertical = xlsx.VerticalAlignmentCenter
// set font style
style.Font.Bold = true
// set cell style
sheet.SetAxisCellStyle("A1", style)
sheet.SetAxisCellStyle("B1", style)
// time value
sheet.SetAxisCellValue("C1", "Date") // C1 = Date
sheet.SetAxisCellValue("C2", time.Date(1980, 9, 8, 23, 40, 10, 40, time.UTC)) // C2 = 1980-09-08 23:40
// duration value
sheet.SetAxisCellValue("D1", "Duration") // D1 = Duration
sheet.SetAxisCellValue("D2", 30*time.Second) // D2 = 00:00:30
// time value with custom format
sheet.AxisCell("E1").SetStringValue("LastTime") // D1 = LastTime
sheet.AxisCell("E2").
SetTimeValue(time.Now()).
SetNumberFormat("yyyy-mm-dd hh:mm:ss") // D2 = 2022-08-23 20:08:08 (your current time)
// set formula
sheet.AxisCell("F1").SetIntValue(100)
sheet.AxisCell("F2").SetIntValue(200)
sheet.AxisCell("F3").SetFormula("SUM(F1:F2)")
// SetColumnStyle example
fStyle := sheet.GetColumnStyle(xlsx.ColumnNumber("F"))
fStyle.Alignment.Horizontal = xlsx.HorizontalAlignmentLeft
fStyle.Alignment.Vertical = xlsx.VerticalAlignmentCenter
sheet.SetColumnStyle(xlsx.ColumnNumber("F"), fStyle)
// set cell border
sheet.SetAxisCellBorder("F3", xlsx.BorderStyleThin, xlsx.Color{Color: "0000FF"}, true, true, true, true)
// save to file
if err := doc.SaveFile("Document1.xlsx"); err != nil {
panic(err)
}
}
The following constitutes the bare to read a spreadsheet document.
package main
import (
"fmt"
"github.com/gofika/xlsx"
)
func main() {
// open exists document
doc, err := xlsx.OpenFile("Document1.xlsx")
if err != nil {
panic(err)
return
}
// open exists sheet
sheet := doc.OpenSheet("Sheet2")
// read cell string
a1String := sheet.Cell(1, 1).GetStringValue()
fmt.Println(a1String)
// cell object read
cell := sheet.AxisCell("B2")
fmt.Println(cell.GetIntValue())
}
Write document as a stream.
package main
import (
"io"
"os"
"github.com/gofika/xlsx"
)
func main() {
// open file to write
f, err := os.OpenFile("Document1.xlsx", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
doc := xlsx.NewFile()
// do something with doc
// ...
// write to file or any io.Writer as stream
doc.Save(f)
}
You can specify default configurations when calling xlsx.NewFile.
package main
import (
"io"
"os"
"github.com/gofika/xlsx"
)
func main() {
// set document: default font name, default font size, default sheet name
doc := xlsx.NewFile(xlsx.WithDefaultFontName("Arial"), xlsx.WithDefaultFontSize(12), xlsx.WithDefaultSheetName("Tab1"))
// do something with doc
// ...
}
- Basic File Format
- File: NewFile, OpenFile, SaveFile, Save, Sheets
- Sheet:
- NewSheet, OpenSheet
- Name, SetCellValue, Cell, AxisCell, SetAxisCellValue, SetColumnWidth, GetColumnWidth, MergeCell, SetColumnStyle, GetColumnStyle, MaxRow
- Cell:
- Row, Col
- SetValue, SetIntValue, SetFloatValue, SetFloatValuePrec, SetStringValue, SetBoolValue, SetDefaultValue, SetTimeValue, SetDateValue, SetDurationValue, SetStyle, SetCellBorder, SetFormula
- GetIntValue, GetStringValue, GetFloatValue, GetBoolValue, GetTimeValue, GetDurationValue, GetStyle, GetFormula
- SetNumberFormat