xls

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 10 Imported by: 0

README

go-xls

CI Go Reference Go

Small Go library for CSV, legacy .xls (BIFF), and .xlsx export, ported from Mapbender’s ExportResponse.php (FOM\CoreBundle\Component\ExportResponse). It focuses on the same wire formats and HTTP headers used there: UTF‑16LE CSV with BOM and sep=, hand-written BIFF cells for .xls, and OpenXML .xlsx via excelize.

Install

go get github.com/eSlider/go-xls

Usage

package main

import (
	"net/http"
	"os"

	"github.com/eSlider/go-xls"
)

func main() {
	tab := xls.Table{
		Columns: []string{"name", "qty"},
		Rows: [][]string{
			{"apple", "3"},
			{"banana", "2"},
		},
	}

	// Legacy .xls (Mapbender genXLS-compatible layout on LE).
	bin, err := xls.GenXLS(tab, true)
	if err != nil {
		panic(err)
	}
	_ = os.WriteFile("export.xls", bin, 0o644)

	// UTF-16LE CSV with BOM + sep= line.
	csv, err := xls.GenCSV(tab, ",", `"`, "UTF-8", true)
	if err != nil {
		panic(err)
	}
	_ = os.WriteFile("export.csv", csv, 0o644)

	// .xlsx
	xlsx, err := xls.GenXLSX(tab, true)
	if err != nil {
		panic(err)
	}
	_ = os.WriteFile("export.xlsx", xlsx, 0o644)

	// HTTP attachment (Symfony-style headers from Mapbender).
	_ = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		body, _ := xls.GenXLS(tab, true)
		xls.WriteAttachment(w, "export.xls", xls.ContentTypeXLS, body, true)
	})
}

Behaviour notes

  • Table: columns are explicit and stable (Go has no PHP ordered-map iteration). If Columns is empty, synthetic names 0, 1, … are derived from the widest row, matching numeric-key tables in Mapbender.
  • XLS: string cells are encoded to ISO‑8859‑1 (unmappable runes become ?). Numbers use strconv.ParseFloat after strings.TrimSpace, similar to PHP is_numeric / trim usage.
  • CSV: only UTF-8 → UTF‑16LE is implemented for encodingFrom; other labels return an error until transcoding is extended.
  • XLSX: uses excelize; numeric-looking cells are written as floats.

Testing

go test -v -race ./...

License

MIT

Documentation

Index

Constants

View Source
const (
	TypeCSV  = "csv"
	TypeXLS  = "xls"
	TypeXLSX = "xlsx"
)

Export format constants (aligned with Mapbender FOM ExportResponse).

View Source
const (
	XLSIntType    = 0x203
	XLSStringType = 0x204
)

BIFF cell record types (Mapbender / legacy Excel writer).

View Source
const (
	ContentTypeCSV  = "text/csv;charset=UTF-16LE"
	ContentTypeXLS  = "application/vnd.ms-excel"
	ContentTypeXLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)

Content types aligned with Mapbender ExportResponse::setType.

Variables

This section is empty.

Functions

func DisableDownloadHeaders

func DisableDownloadHeaders(h http.Header)

DisableDownloadHeaders removes the download-related cache headers.

func EnableDownloadHeaders

func EnableDownloadHeaders(h http.Header)

EnableDownloadHeaders sets Cache-Control / Pragma / Expires like Mapbender enableDownload().

func GenCSV

func GenCSV(tab Table, delimiter, enclosure string, encodingFrom string, detectHead bool) ([]byte, error)

GenCSV builds UTF-16LE CSV bytes with BOM and a leading sep= line, matching Mapbender ExportResponse::setCsv. Row cells must be UTF-8; encodingFrom is reserved for future transcoding (only UTF-8 is supported today).

func GenXLS

func GenXLS(tab Table, detectHead bool) ([]byte, error)

GenXLS generates a legacy .xls (BIFF) workbook matching Mapbender ExportResponse::genXLS layout on little-endian hosts (PHP pack "s" / "d").

func GenXLSX

func GenXLSX(tab Table, detectHead bool) ([]byte, error)

GenXLSX builds an .xlsx workbook (OpenXML) from tabular data, analogous to Mapbender's SimpleXLSXGen::fromArray.

func TrimTrailingFinalNewline

func TrimTrailingFinalNewline(b []byte) []byte

TrimTrailingFinalNewline removes a single trailing newline from UTF-16LE CSV bytes (after BOM) if present. Mapbender stream ends without extra newline after last row in some paths; callers rarely need this.

func WriteAttachment

func WriteAttachment(w http.ResponseWriter, fileName, contentType string, body []byte, enableDownload bool)

WriteAttachment writes body with Content-Type, Content-Disposition, Content-Length, and optional download headers (Mapbender ExportResponse behavior).

Types

type Table

type Table struct {
	Columns []string
	Rows    [][]string
}

Table is column-ordered tabular data for export. Columns defines order and names; each Rows[i] may be shorter than len(Columns) (missing cells are empty). If Columns is nil or empty, synthetic names "0","1",… are derived from the widest row (Mapbender-style numeric keys).

Jump to

Keyboard shortcuts

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