xlsx

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2021 License: BSD-2-Clause Imports: 17 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses donate

XLSX

Microsoft Excel .xlsx read/write for golang

Basic Usage

Installation
go get github.com/leaker/xlsx
Create spreadsheet

Here is example usage that will create xlsx file.

package main

import (
    "fmt"

    "github.com/leaker/xlsx"
)

func main() {
    f := xlsx.NewFile()
    
    sheet := f.NewSheet("Sheet2")
	sheet.SetCellValue(xlsx.ColumnNumber("A"), 1, "Name")
	sheet.SetCellValue(xlsx.ColumnNumber("B"), 1, "Score")
	sheet.SetCellValue(xlsx.ColumnNumber("A"), 2, "Jason")
	sheet.SetCellValue(xlsx.ColumnNumber("B"), 2, 100)
    
    if err := f.SaveFile("Document1.xlsx"); err != nil {
        fmt.Println(err)
    }
}
Reading spreadsheet

The following constitutes the bare to read a spreadsheet document.

package main

import (
    "fmt"

    "github.com/leaker/xlsx"
)

func main() {
    f, err := xlsx.OpenFile("Document1.xlsx")
    if err != nil {
        fmt.Println(err)
        return
    }
    
    sheet := f.OpenSheet("Sheet2")
    A1 := sheet.GetCellString(1, 1)
    fmt.Println(A1)
    
    cell := sheet.Cell(xlsx.ColumnNumber("B"), 2)
    fmt.Println(cell.GetIntValue())
}

TODO:

  • Basic File Format
  • File: NewFile, OpenFile, SaveFile, Write
  • Sheet: NewSheet, OpenSheet ...

Documentation

Index

Constants

View Source
const (
	VariantTypeVariant = "variant"
	VariantTypeVTLPSTR = "lpstr"
)

VariantTypes

Variables

This section is empty.

Functions

func CellNameToCoordinates

func CellNameToCoordinates(cellName string) (int, int)

CellNameToCoordinates convert cell name to [col, row] coordinates

Example:

xlsx.CellNameToCoordinates("A1") // returns 1, 1
xlsx.CellNameToCoordinates("B5") // returns 2, 5

func ColumnName

func ColumnName(columnNumber int) (columnName string)

ColumnName convert the column number to column name

Example:

xlsx.ColumnName(51) // returns "AY"

func ColumnNumber

func ColumnNumber(columnName string) (columnNumber int)

ColumnNumber convert the column name to column number

Example:

xlsx.ColumnNumber("AY") // returns 51

func CoordinatesToCellName

func CoordinatesToCellName(col, row int) string

CoordinatesToCellName convert [col, row] coordinates to cell name

Example:

xlsx.CoordinatesToCellName(1, 1) // returns "A1"

func DeepCopy

func DeepCopy(dst, src interface{}) error

DeepCopy deepcopy object. can without same type

Example:

type A struct {
    Name string
    Value int
}

type B struct {
    Name string
    Value int
}

a := &A { Name: "Jason", 100}
var b B
xlsx.DeepCopy(&b, a)
fmt.Printf("%+v\n", b)

func TimeToExcelTime

func TimeToExcelTime(t time.Time) float64

TimeToExcelTime convert time.Time to excel time format

Types

type Cell

type Cell struct {
	// contains filtered or unexported fields
}

Cell cell operator

func (*Cell) Col

func (c *Cell) Col() int

Col cell col number

func (*Cell) GetIntValue

func (c *Cell) GetIntValue() int

GetIntValue get cell value with int type

func (*Cell) GetStringValue

func (c *Cell) GetStringValue() string

GetStringValue get cell value with string type

func (*Cell) Row

func (c *Cell) Row() int

Row cell row number

func (*Cell) SetBoolValue

func (c *Cell) SetBoolValue(value bool)

SetBoolValue set cell value for bool type

func (*Cell) SetDefaultValue

func (c *Cell) SetDefaultValue(value string)

SetDefaultValue set cell value without any type

func (*Cell) SetDurationValue

func (c *Cell) SetDurationValue(value time.Duration)

SetDurationValue set cell value for time.Duration type

func (*Cell) SetFloatValue

func (c *Cell) SetFloatValue(value float64)

SetFloatValue set cell for float64 type

func (*Cell) SetFloatValuePrec

func (c *Cell) SetFloatValuePrec(value float64, prec int, bitSize int)

SetFloatValuePrec set cell for float64 type with pres

func (*Cell) SetIntValue

func (c *Cell) SetIntValue(value int)

SetIntValue set cell for int type

func (*Cell) SetStringValue

func (c *Cell) SetStringValue(value string)

SetStringValue set cell value for string type

func (*Cell) SetTimeValue

func (c *Cell) SetTimeValue(value time.Time)

SetTimeValue set cell value for time.Time type

func (*Cell) SetValue

func (c *Cell) SetValue(value interface{})

SetValue provides to set the value of a cell Allow Types:

int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
float32
float64
string
[]byte
time.Duration
time.Time
bool
nil

Example:

cell.SetValue(100)
cell.SetValue("Hello")
cell.SetValue(3.14)

type File

type File struct {
	// contains filtered or unexported fields
}

File define for operation xlsx file

func NewFile

func NewFile() *File

NewFile create a default xlsx *File with default template

func OpenFile

func OpenFile(name string) (*File, error)

OpenFile open a xlsx *File

func (*File) NewSheet

func (f *File) NewSheet(name string) *Sheet

NewSheet create a new *Sheet with sheet name Example:

sheet := file.NewSheet("Sheet2")

func (*File) OpenSheet

func (f *File) OpenSheet(name string) *Sheet

OpenSheet open a exist *Sheet by name

Example:

sheet := file.OpenSheet("Sheet1")

return nil if sheet not exist

func (*File) SaveFile

func (f *File) SaveFile(name string) error

SaveFile save xlsx file

func (*File) Write

func (f *File) Write(w io.Writer) error

Write save to steam

type Sheet

type Sheet struct {
	// contains filtered or unexported fields
}

Sheet sheet operator

func (*Sheet) Cell

func (s *Sheet) Cell(col, row int) *Cell

Cell get Cell by cell name

func (*Sheet) GetCellInt

func (s *Sheet) GetCellInt(col, row int) int

GetCellInt get cell value of string

Example:

sheet.GetCellInt(3, 1) // C1 => 1000

func (*Sheet) GetCellString

func (s *Sheet) GetCellString(col, row int) string

GetCellString get cell value of string

Example:

sheet.GetCellString(1, 1) // A1 => "val"

func (*Sheet) SetCellValue

func (s *Sheet) SetCellValue(col, row int, value interface{})

SetCellValue set cell value

Example:

sheet.SetCellValue(1, 1, "val") // A1 => "val"
sheet.SetCellValue(2, 3, 98.01) // B3 => 98.01
sheet.SetCellValue(3, 1, 1000) // C1 => 1000
sheet.SetCellValue(4, 4, time.Now()) // D4 => "2021-03-11 05:19:16.483"

type StyleSheet

type StyleSheet struct {
	// contains filtered or unexported fields
}

StyleSheet style operator

type Vector

type Vector struct {
	// contains filtered or unexported fields
}

Vector for operation xml vt:vector node

func (*Vector) AppendString

func (v *Vector) AppendString(str string)

AppendString append lpstr to array

Example:

InputXML:
<vt:vector size="2" baseType="lpstr">
    <vt:lpstr>Sheet1</vt:lpstr>
</vt:vector>

vector.AppendString("Sheet2")

OutputXML:
<vt:vector size="2" baseType="lpstr">
    <vt:lpstr>Sheet1</vt:lpstr>
    <vt:lpstr>Sheet2</vt:lpstr>
</vt:vector>

func (*Vector) GetIntPairs

func (v *Vector) GetIntPairs() (pairs map[string]int)

GetIntPairs get variant type pairs

Example:

InputXML:
<vt:vector size="2" baseType="variant">
    <vt:variant>
        <vt:lpstr>Worksheets</vt:lpstr>
    </vt:variant>
    <vt:variant>
        <vt:i4>1</vt:i4>
    </vt:variant>
</vt:vector>

vector.GetIntPairs() // => map[Worksheets 1]

func (*Vector) GetStringArray

func (v *Vector) GetStringArray() []string

GetStringArray get lpstr array

Example:

InputXML:
<vt:vector size="2" baseType="lpstr">
    <vt:lpstr>Sheet1</vt:lpstr>
    <vt:lpstr>Sheet2</vt:lpstr>
</vt:vector>

vector.GetStringArray() // => [Sheet1 Sheet2]

func (*Vector) SetIntPairs

func (v *Vector) SetIntPairs(pairs map[string]int)

SetIntPairs set pairs for variant type

Example:

variantPairs := map[string]int {
    "Worksheets": 1,
}
vector.SetIntPairs(variantPairs)

OutputXML:
<vt:vector size="2" baseType="variant">
    <vt:variant>
        <vt:lpstr>Worksheets</vt:lpstr>
    </vt:variant>
    <vt:variant>
        <vt:i4>1</vt:i4>
    </vt:variant>
</vt:vector>

func (*Vector) SetStringArray

func (v *Vector) SetStringArray(strArray []string)

SetStringArray set lpstr array

Example:

vector.SetStringArray([]string{"Sheet1", "Sheet2"})

OutputXML:
<vt:vector size="2" baseType="lpstr">
    <vt:lpstr>Sheet1</vt:lpstr>
    <vt:lpstr>Sheet2</vt:lpstr>
</vt:vector>

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL