xls

package module
v0.0.0-...-65f2094 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

README

XLS

CircleCI codecov GoDoc

Installation

go get github.com/rinor/xls

Reference

Contributors

Pure Golang xls library writen by Rongshu Tech(chinese). Add Formula & Format support by [chen.s.g] (http://www.imohe.com)

Thanks for contributions from Tamás Gulácsi, sergeilem.

English User please mailto Liu Ming

This is a xls library writen in pure Golang. Almostly it is translated from the libxls library in c.

The master brunch has just the reading function without the format.

new_formater branch is for better format for date and number ,but just under test, you can try it in development environment. If you have some problem about the output format, tell me the problem, I will try to fix it.

Basic Usage

  • Use Open function for open file

  • Use OpenWithCloser function for open file and use the return value closer for close file

  • Use OpenReader function for open xls from a reader, you should close related file in your own code

  • Follow the example in GODOC

Documentation

Index

Examples

Constants

View Source
const (
	TYPE_STRING2    = iota + 1
	TYPE_STRING     // 2
	TYPE_FORMULA    // 3
	TYPE_NUMERIC    // 4
	TYPE_BOOL       // 5
	TYPE_NULL       // 6
	TYPE_INLINE     // 7
	TYPE_ERROR      // 8
	TYPE_DATETIME   // 9
	TYPE_PERCENTAGE // 10
	TYPE_CURRENCY   // 11
)

Data types

Variables

View Source
var ErrIsInt = errors.New("is int")

Functions

func ByteToUint16

func ByteToUint16(b []byte) uint16

ByteToUint16 Read 16-bit unsigned integer

func ByteToUint32

func ByteToUint32(b []byte) uint32

ByteToUint32 Read 32-bit unsigned integer

Types

type BlankCol

type BlankCol struct {
	Col
	Xf uint16
}

BlankCol represents a blank cell

func (*BlankCol) Debug

func (c *BlankCol) Debug(wb *WorkBook)

func (*BlankCol) String

func (c *BlankCol) String(wb *WorkBook) []string

type CellRange

type CellRange struct {
	FirstRowB uint16
	LastRowB  uint16
	FristColB uint16
	LastColB  uint16
}

CellRange is range type of multi cells in multi rows

func (*CellRange) FirstCol

func (c *CellRange) FirstCol() uint16

FirstCol return first column index

func (*CellRange) FirstRow

func (c *CellRange) FirstRow() uint16

FirstRow return first row index

func (*CellRange) LastCol

func (c *CellRange) LastCol() uint16

LastCol return last column index

func (*CellRange) LastRow

func (c *CellRange) LastRow() uint16

LastRow return last row index

type Col

type Col struct {
	RowB      uint16
	FirstColB uint16
}

func (*Col) Debug

func (c *Col) Debug(wb *WorkBook)

func (*Col) FirstCol

func (c *Col) FirstCol() uint16

func (*Col) LastCol

func (c *Col) LastCol() uint16

func (*Col) Row

func (c *Col) Row() uint16

func (*Col) String

func (c *Col) String(wb *WorkBook) []string

type Coler

type Coler interface {
	Row() uint16
}

type ExtSheetInfo

type ExtSheetInfo struct {
	ExternalBookIndex uint16
	FirstSheetIndex   uint16
	LastSheetIndex    uint16
}

ExtSheetInfo external sheet references provided for named cells

type Font

type Font struct {
	Info *FontInfo
	Name string
}

Font represents the font

type FontInfo

type FontInfo struct {
	Height     uint16
	Flag       uint16
	Color      uint16
	Bold       uint16
	Escapement uint16
	Underline  byte
	Family     byte
	Charset    byte
	Notused    byte
	NameB      byte
}

FontInfo represents the font info

type Format

type Format struct {
	Head struct {
		Index uint16
		Size  uint16
	}
	Raw []string
	// contains filtered or unexported fields
}

Format value interface

func (*Format) Prepare

func (f *Format) Prepare()

Prepare format meta data

func (*Format) String

func (f *Format) String(v float64) string

String format content to spec string see http://www.openoffice.org/sc/excelfileformat.pdf Page #174

type FormulaCol

type FormulaCol struct {
	Code   uint16
	Btl    uint16
	Btc    uint16
	Bts    []byte
	Header *formulaColHeader
	// contains filtered or unexported fields
}

FormulaCol represents formula cell

func (*FormulaCol) Debug

func (c *FormulaCol) Debug(wb *WorkBook)

func (*FormulaCol) FirstCol

func (c *FormulaCol) FirstCol() uint16

func (*FormulaCol) IsRef

func (c *FormulaCol) IsRef() bool

IsRef return true if cell is reference to other cell

func (*FormulaCol) LastCol

func (c *FormulaCol) LastCol() uint16

func (*FormulaCol) Row

func (c *FormulaCol) Row() uint16

func (*FormulaCol) String

func (c *FormulaCol) String(wb *WorkBook) []string
type HyperLink struct {
	CellRange
	Description      string
	TextMark         string
	TargetFrame      string
	Url              string
	ShortedFilePath  string
	ExtendedFilePath string
	IsUrl            bool
}

HyperLink represents a hyperlink's content

func (*HyperLink) Debug

func (h *HyperLink) Debug(wb *WorkBook)

Debug prints the needed file dump

func (*HyperLink) String

func (h *HyperLink) String(wb *WorkBook) []string

String gets the hyperlink string, use the public variable Url to get the original Url

type LabelsstCol

type LabelsstCol struct {
	Col
	Xf  uint16
	Sst uint32
}

func (*LabelsstCol) Debug

func (c *LabelsstCol) Debug(wb *WorkBook)

func (*LabelsstCol) String

func (c *LabelsstCol) String(wb *WorkBook) []string

type MulBlankCol

type MulBlankCol struct {
	Col
	Xfs      []uint16
	LastColB uint16
}

func (*MulBlankCol) Debug

func (c *MulBlankCol) Debug(wb *WorkBook)

func (*MulBlankCol) LastCol

func (c *MulBlankCol) LastCol() uint16

func (*MulBlankCol) String

func (c *MulBlankCol) String(wb *WorkBook) []string

type MulrkCol

type MulrkCol struct {
	Col
	Xfrks    []XfRk
	LastColB uint16
}

func (*MulrkCol) Debug

func (c *MulrkCol) Debug(wb *WorkBook)

func (*MulrkCol) LastCol

func (c *MulrkCol) LastCol() uint16

func (*MulrkCol) String

func (c *MulrkCol) String(wb *WorkBook) []string

type NumberCol

type NumberCol struct {
	Col
	Index uint16
	Float float64
}

func (*NumberCol) Debug

func (c *NumberCol) Debug(wb *WorkBook)

func (*NumberCol) String

func (c *NumberCol) String(wb *WorkBook) []string

type RK

type RK uint32

func (RK) Debug

func (rk RK) Debug(wb *WorkBook)

func (RK) String

func (rk RK) String(wb *WorkBook) string

type Ranger

type Ranger interface {
	FirstRow() uint16
	LastRow() uint16
}

Ranger is range type of multi rows

type RkCol

type RkCol struct {
	Col
	Xfrk XfRk
}

func (*RkCol) Debug

func (c *RkCol) Debug(wb *WorkBook)

func (*RkCol) String

func (c *RkCol) String(wb *WorkBook) []string

type Row

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

Row the data of one row

func (*Row) Col

func (r *Row) Col(i int) string

Col Get the Nth Column on the Row.

func (*Row) Column

func (r *Row) Column(i int) cell

Column will return the index i cell of the row

func (*Row) FirstCol

func (r *Row) FirstCol() int

FirstCol Get the number of First Col of the Row.

func (*Row) LastCol

func (r *Row) LastCol() int

LastCol Get the number of Last Col of the Row.

type SstInfo

type SstInfo struct {
	Total uint32
	Count uint32
}

SstInfo represents sst struct info

type WorkBook

type WorkBook struct {
	Is5ver   bool
	Type     uint16
	Codepage uint16
	Xfs      []XF
	Fonts    []Font
	Formats  map[uint16]*Format

	Author string
	// contains filtered or unexported fields
}

WorkBook represents xls workbook type

func Open

func Open(file string, charset string) (*WorkBook, error)

Open open one xls file with some charset

Example
if xlFile, err := Open("./testdata/Table.xls", "utf-8"); err == nil {
	fmt.Println(xlFile.Author)
}
Output:

func OpenReader

func OpenReader(reader io.ReadSeeker, charset string) (wb *WorkBook, err error)

OpenReader open xls file from reader

func OpenWithBuffer

func OpenWithBuffer(file string, charset string) (*WorkBook, error)

OpenWithBuffer open one xls file with memory buffer

func OpenWithCloser

func OpenWithCloser(file string, charset string) (*WorkBook, io.Closer, error)

OpenWithCloser open one xls file and return the closer

func (*WorkBook) Format

func (w *WorkBook) Format(xf uint16, v float64) (string, bool)

Format formats value to string

func (*WorkBook) GetSheet

func (w *WorkBook) GetSheet(num int) *WorkSheet

GetSheet gets one sheet by its number

Example

Output: read the content of first two cols in each row

if xlFile, err := Open("./testdata/Table.xls", "utf-8"); err == nil {
	if sheet1 := xlFile.GetSheet(0); sheet1 != nil {
		fmt.Print("Total Lines ", sheet1.MaxRow, sheet1.Name)
		col1 := sheet1.Row(0).Col(0)
		col2 := sheet1.Row(0).Col(0)
		for i := 0; i <= (int(sheet1.MaxRow)); i++ {
			row1 := sheet1.Row(i)
			col1 = row1.Col(0)
			col2 = row1.Col(1)
			fmt.Print("\n", col1, ",", col2)
		}
	}
}
Output:

func (*WorkBook) NumSheets

func (w *WorkBook) NumSheets() int

NumSheets get the number of all sheets, look into example

Example
if xlFile, err := Open("./testdata/Table.xls", "utf-8"); err == nil {
	for i := 0; i < xlFile.NumSheets(); i++ {
		sheet := xlFile.GetSheet(i)
		fmt.Println(sheet.Name)
	}
}
Output:

func (*WorkBook) ReadAllCells

func (w *WorkBook) ReadAllCells(max int) (res [][]string)

ReadAllCells helper function to read all cells from file Notice: the max value is the limit of the max capacity of lines. Warning: the helper function will need big memory if file is large.

type WorkSheet

type WorkSheet struct {
	Name string

	//NOTICE: this is the max row number of the sheet, so it should be count -1
	MaxRow uint16
	// contains filtered or unexported fields
}

WorkSheet in one WorkBook

func (*WorkSheet) Row

func (w *WorkSheet) Row(i int) *Row

Row returns the row according the index, if the row is not exist, it will return nil

type XF

type XF interface {
	FormatNo() uint16
}

type Xf5

type Xf5 struct {
	Font      uint16
	Format    uint16
	Type      uint16
	Align     uint16
	Color     uint16
	Fill      uint16
	Border    uint16
	Linestyle uint16
}

func (*Xf5) FormatNo

func (x *Xf5) FormatNo() uint16

type Xf8

type Xf8 struct {
	Font        uint16
	Format      uint16
	Type        uint16
	Align       byte
	Rotation    byte
	Ident       byte
	Usedattr    byte
	Linestyle   uint32
	Linecolor   uint32
	Groundcolor uint16
}

func (*Xf8) FormatNo

func (x *Xf8) FormatNo() uint16

type XfRk

type XfRk struct {
	Index uint16
	Rk    RK
}

func (*XfRk) Debug

func (xf *XfRk) Debug(wb *WorkBook)

func (*XfRk) String

func (xf *XfRk) String(wb *WorkBook) string

Jump to

Keyboard shortcuts

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