xlsreader

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference Version

xls-reader

A Go library for reading binary Excel XLS worksheets (BIFF8) as strings, compatible with files created by xls-writer. It uses only the Go standard library and has no dependency on the writer module.

Usage

package main

import (
    "fmt"
    "log"

    "github.com/sergrom/xls-reader"
)

func main() {
    book, err := xlsreader.Open("cities.xls")
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("Author:", book.Properties.Creator)
    for _, sheet := range book.Sheets {
        fmt.Println("Sheet:", sheet.Name)
        for _, row := range sheet.Rows() {
            fmt.Println(row)
        }
    }
}

Workbook API

API Description
Open(filename) (*Workbook, error) Reads all worksheets and properties, then closes the file.
Parse(io.Reader) (*Workbook, error) Reads all worksheets and properties without closing the reader.
Workbook.Properties Optional document metadata.
Workbook.Sheets Worksheets in their original tab order, including hidden sheets.
Workbook.SheetByName(name) (*Sheet, bool) Finds a sheet by its case-insensitive name.
Sheet.Name Worksheet name.
Sheet.Index Original zero-based tab index.
Sheet.Visibility SheetVisible, SheetHidden, or SheetVeryHidden.
Sheet.Rows() [][]string Returns already decoded rows without copying.

Open and Parse parse every worksheet before returning, so all read errors are reported immediately. They return no partial workbook on failure. There is no Close method: the returned workbook owns no open file or reader. Rows() performs no I/O. Treat its returned slices as read-only; modifying them changes the sheet's data. Objects are not safe for concurrent mutation.

sheet, ok := book.SheetByName("Cities")
if ok {
    rows := sheet.Rows()
    fmt.Println(rows)
}

Properties contains Title, Subject, Creator, Keywords, Description, LastModifiedBy, CreatedAt, and ModifiedAt. Metadata is read from the optional OLE SummaryInformation stream. Missing strings are empty; missing dates are zero time.Time values (check IsZero()). Dates are returned in UTC and describe the document's saved metadata, not the local file's filesystem timestamps.

Metadata supports UTF-16, UTF-8, Windows-1251, Windows-1252, ISO-8859-1, and ASCII. Unknown property types or code pages leave the corresponding field empty. Malformed metadata returns an error from Open/Parse.

Supported files

  • CFB/OLE version 3 containers with 512-byte sectors, including FAT, mini FAT, and DIFAT chains for larger files.
  • BIFF8 workbook globals and worksheet records used by xls-writer.
  • Multiple worksheets, shared and inline text, blank cells, and rich text (text is retained; formatting and phonetic metadata are ignored).
  • Numbers (NUMBER, RK, MULRK), booleans, Excel errors, and cached formula results, including continued string results.
  • Shared string tables, compressed Unicode, UTF-16, and strings spanning CONTINUE records, including supplementary characters such as emoji.
  • Up to 65,536 rows and 256 columns per sheet, and 32,767 UTF-16 code units per cell.

Cell positions are preserved. Gaps before a stored cell become empty strings. Each row ends at its last recognized cell, so rows can have different lengths. Entirely empty rows are nil slices. Trailing empty rows are retained from the worksheet's DIMENSIONS record. An empty sheet returns an empty slice. Column widths do not add cells to the returned rows.

Workbook globals and the shared string table are parsed once per call. Open and Parse decode all sheets.

Scope and errors

Values are converted without applying Excel display formats:

Stored value Returned string
Text Original text.
Number Decimal representation, e.g. 42, -12.5, 0.0000001.
Boolean TRUE or FALSE.
Excel error Its name, e.g. #DIV/0!, #REF!, #N/A.
Formula Its saved result, converted using the same rules.
Missing, unrecognized, or non-finite value Empty string.

Formulas are not evaluated. A saved result can be stale; a formula without a readable cached result remains blank. Numeric precision is limited to the value stored in the XLS file. Dates and times are returned as their numeric Excel serial values; currency, percentages, custom number formats, and leading zeros from display formats are not reproduced. Textual leading zeros are preserved.

Unknown BIFF records are ignored without ErrUnsupported. The reader never guesses cell coordinates from unknown record payloads: missing positions before recognized cells remain empty. An unknown trailing cell encoding cannot extend a row by itself. Recognized but unconverted cells (such as legacy RSTRING) retain their position with an empty value. Embedded chart substreams are skipped.

This is not a complete Excel implementation. XLSX, older BIFF versions, CFB version 4, encrypted workbooks, and non-worksheet tabs still return ErrUnsupported. Corrupt or truncated recognized records still return ErrInvalidFile; they are not silently treated as unsupported values. Cell formatting and printing settings are not exposed. Sheet names, visibility, and supported document properties are available through the workbook API.

Use errors.Is to distinguish:

Error Meaning
ErrInvalidFile Truncated or malformed structures, invalid offsets, cyclic chains, or invalid string references.
ErrUnsupported Unsupported file format, encryption, or sheet kind (not cell value types).
ErrLimit Input exceeds MaxFileSize (128 MiB).

File-system and reader errors are wrapped, preserving errors.Is/errors.As. Input bytes are buffered in memory; decoded strings and rows require additional memory. The workbook retains all decoded sheets. The size limit bounds input buffering, not total memory consumption.

Development

Go 1.20 or later is required.

go test ./...
go test -race ./...
go vet ./...
go test -run='^$' -fuzz='^FuzzBIFF$' -fuzztime=15s
go test -run='^$' -fuzz='^FuzzParse$' -fuzztime=15s
go test -run='^$' -fuzz='^FuzzProperties$' -fuzztime=15s

Tests include writer/reader round trips, multiple sheets, empty and sparse rows, Unicode continuations, numeric/boolean/error cells, cached formulas, rich text, row/column limits, DIFAT files, ignored records, truncated files, broken sector chains, sheet visibility, metadata encodings, and UTC timestamps. A snapshot of xls-writer is copied into internal/testwriter solely for generating fixtures; production code does not import it. Small hand-built BIFF fixtures test parsing independently of that writer, including changes of encoding within a continued string. A checked-in fixture generated by xlwt also tests values produced by an independent XLS writer; Python is not required to run the Go tests.

Format references:

License

MIT.

Documentation

Overview

Package xlsreader reads BIFF8 XLS worksheets as strings. It supports common cell types and ignores values it cannot convert. Formulas are not evaluated.

Index

Constants

View Source
const MaxFileSize = 128 << 20

MaxFileSize bounds input buffering. Decoded strings and rows require additional memory. Files are read into memory; this is not a streaming worksheet reader.

Variables

View Source
var (
	ErrInvalidFile = errors.New("xlsreader: invalid XLS file")
	ErrUnsupported = errors.New("xlsreader: unsupported XLS feature")
	ErrLimit       = errors.New("xlsreader: size limit exceeded")
)

Functions

This section is empty.

Types

type Properties added in v1.0.1

type Properties struct {
	Title          string
	Subject        string
	Creator        string
	Keywords       string
	Description    string
	LastModifiedBy string
	CreatedAt      time.Time
	ModifiedAt     time.Time
}

Properties contains optional SummaryInformation metadata. Missing strings are empty; missing or zero FILETIME dates are zero time.Time values. Timestamps are returned in UTC.

type Sheet added in v1.0.1

type Sheet struct {
	Name       string
	Index      int
	Visibility SheetVisibility
	// contains filtered or unexported fields
}

Sheet describes a worksheet. Index is its original zero-based tab index. Name, Index and Visibility are snapshots of metadata from the XLS file.

func (*Sheet) Rows added in v1.0.1

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

Rows returns the parsed rows without copying. Treat the returned slices as read-only; changing them changes this Sheet. No I/O occurs and no errors are deferred until this call. Missing cells are empty strings; entirely empty rows are nil slices. Rows end at their last recognized cell, and trailing empty rows from DIMENSIONS are preserved. Numbers, booleans, Excel errors and cached formula results are converted to strings. Unsupported values remain blank. Number formats are not applied: dates are returned as numeric serial values.

type SheetVisibility added in v1.0.1

type SheetVisibility uint8

SheetVisibility describes the visibility stored in the worksheet tab record.

const (
	SheetVisible SheetVisibility = iota
	SheetHidden
	SheetVeryHidden
)

SheetVeryHidden cannot be made visible through Excel's ordinary Unhide menu.

type Workbook added in v1.0.1

type Workbook struct {
	Properties Properties
	Sheets     []*Sheet
}

Workbook holds document properties and all worksheets in tab order. Open and Parse eagerly read all worksheets. Mutations are not synchronized.

func Open added in v1.0.1

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

Open reads all worksheets and optional document properties, then closes the file. It returns no partial Workbook on failure.

func Parse added in v1.0.1

func Parse(r io.Reader) (*Workbook, error)

Parse reads a workbook without closing r. All worksheets and supported properties are decoded before returning. Input buffering is bounded by MaxFileSize; decoded worksheets require additional memory.

func (*Workbook) SheetByName added in v1.0.1

func (w *Workbook) SheetByName(name string) (*Sheet, bool)

SheetByName finds the first worksheet whose name matches case-insensitively.

Directories

Path Synopsis
internal
testwriter
Package xlswriter creates BIFF8 XLS workbooks containing text cells.
Package xlswriter creates BIFF8 XLS workbooks containing text cells.

Jump to

Keyboard shortcuts

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