xlsreader

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 10 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

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() {
    rows, err := xlsreader.ReadXLS("cities.xls", 0)
    if err != nil {
        log.Fatal(err)
    }

    for _, row := range rows {
        fmt.Println(row)
    }
}

The second argument is the zero-based sheet index. For an existing reader:

rows, err := xlsreader.Read(reader, 0)

Read does not close the supplied io.Reader. ReadXLS opens and closes its file. Both return [][]string and an error. No partial rows are returned on failure.

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.

Only the selected worksheet's cell records are parsed. Workbook globals and its shared string table are parsed for every read.

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. Formatting, sheet names, document properties, and printing settings are not exposed in the returned data.

Use errors.Is to distinguish:

Error Meaning
ErrSheetIndex Negative or out-of-range sheet index.
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 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='^FuzzRead$' -fuzztime=15s
go test -run='^$' -fuzz='^FuzzBIFF$' -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, and broken sector chains. 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")
	ErrSheetIndex  = errors.New("xlsreader: sheet index out of range")
	ErrLimit       = errors.New("xlsreader: size limit exceeded")
)

Functions

func Read

func Read(r io.Reader, sheetIndex int) ([][]string, error)

Read reads a zero-based sheet without closing r. Text, numbers, booleans, Excel errors and cached formula results are converted to strings. Unsupported values are left blank and unknown BIFF records are ignored. Row/column positions and trailing blank rows from DIMENSIONS are preserved; rows extend through their last recognized cell. Entirely empty rows are nil slices. Number formats are not applied: dates are returned as numeric serial values. Encryption and unsupported file formats still return ErrUnsupported; malformed data still returns ErrInvalidFile. Only the selected sheet's cells are parsed.

func ReadXLS

func ReadXLS(filename string, sheetIndex int) ([][]string, error)

ReadXLS opens filename, reads a zero-based sheet, and closes the file.

Types

This section is empty.

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