Categorygithub.com/gofika/xlsx
modulepackage
0.0.0-20240604014234-67646b266939
Repository: https://github.com/gofika/xlsx.git
Documentation: pkg.go.dev

# README

codecov Build Status go.dev Go Report Card Licenses donate

xlsx

Microsoft .xlsx read/write for golang with high performance

Basic Usage

Installation

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"

Create spreadsheet

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)
    }
}

Reading spreadsheet

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 spreadsheet as stream

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)
}

NewFile with options

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
    // ...
}

Implemented:

  • 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

# Packages

No description provided by the author

# Functions

CellNameToCoordinates convert cell name to [col, row] coordinates Example: xlsx.CellNameToCoordinates("A1") // returns 1, 1 xlsx.CellNameToCoordinates("B5") // returns 2, 5.
ColumnName convert the column number to column name Example: xlsx.ColumnName(51) // returns "AY".
ColumnNumber convert the column name to column number Example: xlsx.ColumnNumber("AY") // returns 51.
ColumnRange parse column range Example: xlsx.ColumnRange("A:Z") // returns 1, 26 xlsx.ColumnRange("Z:A") // returns 1, 26.
CoordinatesToCellName convert [col, row] coordinates to cell name Example: xlsx.CoordinatesToCellName(1, 1) // returns "A1".
ExcelTimeToTime convert excel time format to time.Time.
No description provided by the author
NewFile create a xlsx File.
OpenFile open a xlsx file for operator.
OpenFileReader open a stream for operator.
TimeToExcelTime convert time.Time to excel time format.
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
The maximum number of built-in number formats.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ASCII character set.
Arabic character set.
Baltic character set.
Chinese character set used mostly in Hong Kong SAR and Taiwan.
System default character set.
Eastern European character set.
Chinese character set used in mainland China.
Greek character set.
Another common spelling of the Korean character set.
Korean character set.
Hebrew character set.
Korean character set.
Characters used by Macintosh.
Extended ASCII character set used with disk operating system (DOS) and some Microsoft Windows fonts.
Russian character set.
Japanese character set.
Symbol character set.
Thai character set.
Turkish character set.
Vietnamese character set.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
A major font of a theme, generally used for headings.
A minor font of a theme, generally used to body and paragraphs.
Not a part of theme scheme.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
VariantTypes.
VariantTypes.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

Cell cell operator.
File define for operation xlsx file.
No description provided by the author
Sheet sheet operator.

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.drawing.colorscheme?view=openxml-2.8.1.
No description provided by the author