GithubHelp home page GithubHelp logo

gofika / xlsx Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 0.0 176 KB

Microsoft .xlsx read/write for golang

License: MIT License

Go 100.00%
excel formula go golang microsoft office ooxml openoffice openxml spreadsheet visualization xlsx

xlsx's Introduction

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

xlsx's People

Contributors

leaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

xlsx's Issues

Streaming API

Is it possible to write in a streaming mode? It would be ideal to be able to write rows in a streaming mode. Then you could also be able to configure the in memory buffer threshold.

doc := xlsx.NewFile(xlsx.StreamMode, xlsx.RowBufferSize(100))

defer doc.Close()

pageSize := 100
total := db.GetItemCount()
pages := total / pageSize

if  total % pageSize > 0 {
    pages++
}

sheet := doc.AddSheet("items")
defer sheet.Close()

sheet.AddRow()
    .SetCellValue(xlsx.ColumnNumber("A"),  1, "Name")
    .SetCellValue(xlsx.ColumnNumber("B"),  1, "Quantity")
    .SetCellValue(xlsx.ColumnNumber("C"),  1, "Price")

for page = 0; page < pages; i++ {
    items := db.GetItems(page * pageSize, pageSize)
    for item, _ := range items {
        sheet.AddRow()
            .SetCellValue(xlsx.ColumnNumber("A"),  1, item.Name)
            .SetCellValue(xlsx.ColumnNumber("B"),  1, item.Quantity)
            .SetCellValue(xlsx.ColumnNumber("C"),  1, item.Price)
    }
}

doc.SaveAs("items.xlsx")

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.