xls

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 12 Imported by: 0

README

xls

A Go library for reading Microsoft Excel 97–2003 binary format (.xls / BIFF8) files.

Go Reference

Installation

go get github.com/nkiri/xls

Usage

Open a file
wb, err := xls.Open("book.xls")
if err != nil {
    log.Fatal(err)
}
Access sheets
// By index (0-based)
sh := wb.Sheet(0)

// By name
sh = wb.SheetByName("Sheet1")

fmt.Println(sh.Name)         // sheet name
fmt.Println(wb.SheetCount()) // number of sheets
Read cells
// Get all cell values as [][]string
rows := sh.Strings()
for _, row := range rows {
    fmt.Println(row)
}

// Access rows and cells individually
for i := 0; i < sh.RowCount(); i++ {
    row := sh.Row(i)
    if row == nil {
        continue
    }
    for _, cell := range row.Cells() {
        fmt.Printf("[%d,%d] %s\n", cell.Row, cell.Col, cell.Value())
    }
}
Type-specific cell values
cell := sh.Row(0).Cell(0)

switch cell.Type {
case xls.CellTypeString:
    fmt.Println(cell.String())
case xls.CellTypeNumber:
    fmt.Println(cell.Float())
case xls.CellTypeBool:
    fmt.Println(cell.Bool())
case xls.CellTypeDate:
    fmt.Println(cell.Time())
}

Supported features

Feature Status
Reading (BIFF8 / Excel 97–2003)
String, number, boolean, date cells
Shared String Table (SST)
Formula cells (cached value)
1904 date system
Writing Not implemented

Example

example/xlsdump is a sample program that prints all cell data from an XLS file in a table format.

go run ./example/xlsdump path/to/file.xls

Documentation

https://pkg.go.dev/github.com/nkiri/xls

License

MIT

Documentation

Overview

Package xls provides support for reading and writing Microsoft Excel 97-2003 binary format (.xls) files, also known as BIFF8 (Binary Interchange File Format).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	Row, Col int
	Type     CellType

	Style *Style
	// contains filtered or unexported fields
}

Cell represents a single cell in a worksheet.

func (*Cell) Bool

func (c *Cell) Bool() bool

Bool returns the cell value as a bool.

func (*Cell) Float

func (c *Cell) Float() float64

Float returns the cell value as a float64.

func (*Cell) String

func (c *Cell) String() string

String returns the cell value as a string.

func (*Cell) Time

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

Time returns the cell value as a time.Time.

func (*Cell) Value

func (c *Cell) Value() string

Value returns the cell content as a human-readable string regardless of type:

  • Empty → ""
  • String → the string value
  • Number → decimal representation (no trailing zeros)
  • Bool → "TRUE" or "FALSE"
  • Date → "2006-01-02" (date-only) or "2006-01-02T15:04:05Z" when the time component is non-zero
  • Error → "#NULL!", "#DIV/0!", "#VALUE!", "#REF!", "#NAME?", "#NUM!", "#N/A", or "#ERR!<code>" for unknown codes
  • Formula → the cached calculation result, formatted by the same rules as the concrete types above (string/number/bool/date/error)

type CellType

type CellType int

CellType represents the data type of a cell value.

const (
	CellTypeEmpty   CellType = iota
	CellTypeString           // String (label or SST entry)
	CellTypeNumber           // Floating-point number
	CellTypeBool             // Boolean
	CellTypeError            // Formula error
	CellTypeFormula          // Formula result
	CellTypeDate             // Date/time stored as a number
)

type Font

type Font struct {
	Name      string
	Size      float64 // in points
	Bold      bool
	Italic    bool
	Underline bool
	Color     uint32 // RGB
}

Font holds font information.

type NumberFormat

type NumberFormat struct {
	Index  int
	Format string
}

NumberFormat holds a number format string.

type Row

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

Row represents a single row in a worksheet.

func (*Row) Cell

func (r *Row) Cell(col int) *Cell

Cell returns the cell at the given 0-based column index, or nil if empty.

func (*Row) CellCount

func (r *Row) CellCount() int

CellCount returns the number of cells in the row.

type Sheet

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

Sheet represents a single worksheet within a workbook.

func (*Sheet) Name

func (s *Sheet) Name() string

Name returns the sheet name.

func (*Sheet) Row

func (s *Sheet) Row(index int) *Row

Row returns the row at the given 0-based index, or nil if it does not exist.

func (*Sheet) RowCount

func (s *Sheet) RowCount() int

RowCount returns the number of rows in the sheet.

func (*Sheet) Strings

func (s *Sheet) Strings() [][]string

Strings returns all cell values as a 2-D slice of strings.

Each element is the result of Cell.Value(): numbers, booleans, dates, and errors are converted to their human-readable form; empty/nil cells become empty strings. Trailing empty columns within a row are preserved up to the row's last non-empty column, and trailing empty rows at the bottom of the sheet are omitted.

type Style

type Style struct {
	FontIndex   int
	FormatIndex int // Number format index
	XFIndex     int // Extended format index
}

Style holds formatting attributes for a cell.

type Workbook

type Workbook struct {
	Sheets []*Sheet
}

Workbook represents an XLS workbook.

func Open

func Open(path string) (*Workbook, error)

Open opens an XLS file from the given path and returns a Workbook.

func Read

func Read(r io.ReadSeeker) (*Workbook, error)

Read parses an XLS workbook from r. It supports BIFF8 format (Excel 97–2003, .xls).

func (*Workbook) Save

func (wb *Workbook) Save(path string) error

Save writes the workbook to the file at path, creating or truncating it.

func (*Workbook) Sheet

func (wb *Workbook) Sheet(index int) *Sheet

Sheet returns the sheet at the given index (0-based).

func (*Workbook) SheetByName

func (wb *Workbook) SheetByName(name string) *Sheet

SheetByName returns the first sheet with the given name, or nil if not found.

func (*Workbook) SheetCount

func (wb *Workbook) SheetCount() int

SheetCount returns the number of sheets in the workbook.

func (*Workbook) SheetList added in v0.0.2

func (wb *Workbook) SheetList() []string

SheetList returns the names of all sheets in the workbook.

func (*Workbook) Write

func (wb *Workbook) Write(w io.Writer) error

Write serialises the workbook in XLS format to w.

Directories

Path Synopsis
example
xlsdump command
xlsdump は XLS ファイルを読み込み、全シートのセルデータを [][]string として取得して標準出力に表示するサンプルプログラムです。
xlsdump は XLS ファイルを読み込み、全シートのセルデータを [][]string として取得して標準出力に表示するサンプルプログラムです。
xlsread command
xlsread reads an XLS file and prints each sheet as a [][]string.
xlsread reads an XLS file and prints each sheet as a [][]string.
internal
biff
Package biff implements reading and writing of Binary Interchange File Format (BIFF8) record streams, as used inside the "Workbook" stream of an XLS file.
Package biff implements reading and writing of Binary Interchange File Format (BIFF8) record streams, as used inside the "Workbook" stream of an XLS file.
cfb
Package cfb implements reading and writing of Microsoft Compound File Binary (CFB) files, also known as OLE2 / Structured Storage.
Package cfb implements reading and writing of Microsoft Compound File Binary (CFB) files, also known as OLE2 / Structured Storage.
codepage
Package codepage provides helpers for decoding legacy Windows code-page encoded strings found in older BIFF records.
Package codepage provides helpers for decoding legacy Windows code-page encoded strings found in older BIFF records.
formula
Package formula handles encoding and decoding of BIFF8 formula token streams.
Package formula handles encoding and decoding of BIFF8 formula token streams.

Jump to

Keyboard shortcuts

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