Documentation
¶
Overview ¶
Package xlsx lets you create and read XLSX files incrementally.
The writer buffers rows only until CommitRows is called and then streams them to temporary files on disk. The reader streams worksheet rows with xml.Decoder and keeps only one row at a time in memory. Both directions are designed for large workbooks.
Writing ¶
workbook := xlsx.NewWorkbook("report.xlsx")
worksheet := workbook.AddWorksheet(&xlsx.WorksheetOptions{Name: "Sales"})
row, err := worksheet.AddRow()
if err != nil {
log.Fatal(err)
}
cell, err := row.AddCell()
if err != nil {
log.Fatal(err)
}
cell.SetString("Revenue")
if err := worksheet.CommitRows(); err != nil {
log.Fatal(err)
}
if err := workbook.Commit(); err != nil {
log.Fatal(err)
}
Reading ¶
reader, err := xlsx.Open("report.xlsx")
if err != nil {
log.Fatal(err)
}
defer reader.Close()
sheet, err := reader.OpenSheet("Sales")
if err != nil {
log.Fatal(err)
}
defer sheet.Close()
for sheet.Next() {
cell, ok := sheet.Row().CellAt(0)
if !ok {
continue
}
value, err := cell.String()
if err != nil {
log.Fatal(err)
}
fmt.Println(value)
}
if err := sheet.Err(); err != nil {
log.Fatal(err)
}
Options and temporary files ¶
NewWorkbookWithOptions accepts a temp directory, buffer size, compression, file modes, context cancellation, shared strings and safety limits. The library creates a private subdirectory inside the configured temp directory and never deletes the parent directory.
NewWriter writes the final package to an io.Writer, which is useful for HTTP responses and object storage.
NewStreamWriter is a zero-temp streaming mode: worksheet XML and ZIP entries are written directly to the io.Writer as rows are committed. Call Workbook.Flush to push buffered bytes to an http.Flusher and Workbook.Commit to finish the ZIP central directory. Only one worksheet may be open at a time in streaming mode.
Values ¶
Cell.Value accepts strings, booleans, all integer and unsigned integer types, floats, time.Time, []byte and database/sql/driver.Valuer implementations. Use Cell.SetFormula for formulas, Cell.SetHyperlink for links and Cell.SetStyle with an ID returned by Workbook.AddStyle.
Validation ¶
Validate checks the package structure, content types, relationships, worksheet references, styles and shared strings without loading the whole workbook into memory.
Index ¶
- Variables
- func Validate(filePath string, opts ...ValidateOption) error
- func ValidateReaderAt(r io.ReaderAt, size int64, opts ...ValidateOption) error
- type Cell
- func (c *Cell) Identifier() string
- func (c *Cell) Index() int
- func (c *Cell) Row() *Row
- func (c *Cell) SetBool(value bool) *Cell
- func (c *Cell) SetFloat(value float64) *Cell
- func (c *Cell) SetFormula(formula string) *Cell
- func (c *Cell) SetHyperlink(target string) *Cell
- func (c *Cell) SetInt(value int64) *Cell
- func (c *Cell) SetString(value string) *Cell
- func (c *Cell) SetStyle(styleID int) *Cell
- func (c *Cell) SetTime(value time.Time) *Cell
- type CellData
- type CellOptions
- type CellType
- type Compression
- type Options
- type ReadOption
- func WithReadContext(ctx context.Context) ReadOption
- func WithReadLimits(maxUncompressedSize int64, maxEntries int) ReadOption
- func WithReadMaxCellLength(length int) ReadOption
- func WithReadMaxColumns(columns int) ReadOption
- func WithReadMaxRows(rows int) ReadOption
- func WithReadTempDir(dir string) ReadOption
- func WithSharedStringsMode(mode SharedStringsMode) ReadOption
- type ReadOptions
- type Reader
- type Row
- func (r *Row) AddCell() (*Cell, error)
- func (r *Row) AddCellWithKey(key string) (*Cell, error)
- func (r *Row) Append(values ...interface{}) error
- func (r *Row) Index() int
- func (r *Row) SetHeight(height float64) error
- func (r *Row) WriteBool(value bool) (*Cell, error)
- func (r *Row) WriteFloat(value float64) (*Cell, error)
- func (r *Row) WriteInt(value int64) (*Cell, error)
- func (r *Row) WriteString(value string) (*Cell, error)
- func (r *Row) WriteTime(value time.Time) (*Cell, error)
- type RowData
- type SharedStringsMode
- type SheetInfo
- type SheetReader
- type Style
- type ValidateOption
- type Workbook
- func (wb *Workbook) AddStyle(style Style) (int, error)
- func (wb *Workbook) AddWorksheet(opts *WorksheetOptions) *Worksheet
- func (wb *Workbook) Cleanup() error
- func (wb *Workbook) Close() error
- func (wb *Workbook) Commit() error
- func (wb *Workbook) Context() context.Context
- func (wb *Workbook) Flush() error
- func (wb *Workbook) HasPendingWorksheets() bool
- func (wb *Workbook) TempDir() string
- func (wb *Workbook) Worksheets() []*Worksheet
- type Worksheet
- func (ws *Worksheet) AddRow() (*Row, error)
- func (ws *Worksheet) Commit() error
- func (ws *Worksheet) CommitRows() error
- func (ws *Worksheet) DefineColumns(columns []*WorksheetColumn) error
- func (ws *Worksheet) FileName() string
- func (ws *Worksheet) FreezePanes(rows, columns int) error
- func (ws *Worksheet) ID() int
- func (ws *Worksheet) MergeCells(ranges ...string) error
- func (ws *Worksheet) Name() string
- func (ws *Worksheet) SetAutoFilter(ref string) error
- type WorksheetColumn
- type WorksheetOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrWorkbookCommitted is returned when an operation is attempted on a // workbook that has already been committed. ErrWorkbookCommitted = errors.New("workbook already committed") // ErrWorksheetCommitted is returned when an operation is attempted on a // worksheet that has already been committed. ErrWorksheetCommitted = errors.New("worksheet already committed") // ErrNoWorksheets is returned when a workbook without worksheets is // committed. ErrNoWorksheets = errors.New("a workbook needs at least one worksheet") // ErrNoRows is returned when CommitRows is called without pending rows. ErrNoRows = errors.New("there are no rows to commit") // ErrPendingRows is returned when a worksheet is committed while it still // has rows waiting to be written. ErrPendingRows = errors.New("worksheet has pending rows") // ErrNoColumns is returned when a keyed cell is added to a worksheet that // has no columns defined. ErrNoColumns = errors.New("no columns were defined on the worksheet") // ErrColumnsDefined is returned when an unkeyed cell is added to a // worksheet that uses keyed columns. ErrColumnsDefined = errors.New("columns were defined on the worksheet") // ErrUnsupportedType is returned when a cell value cannot be represented // in an XLSX file. ErrUnsupportedType = errors.New("unsupported cell value type") // ErrInvalidSheetName is returned when a worksheet name is not a valid // Excel sheet name. ErrInvalidSheetName = errors.New("invalid worksheet name") // ErrDuplicateSheetName is returned when two worksheets share a name. ErrDuplicateSheetName = errors.New("duplicate worksheet name") // ErrInvalidColumn is returned when a column definition is invalid. ErrInvalidColumn = errors.New("invalid worksheet column") // ErrInvalidStyle is returned when a style or style ID is invalid. ErrInvalidStyle = errors.New("invalid cell style") // ErrInvalidFormula is returned when a formula is invalid. ErrInvalidFormula = errors.New("invalid cell formula") // ErrInvalidHyperlink is returned when a hyperlink is invalid. ErrInvalidHyperlink = errors.New("invalid cell hyperlink") // ErrInvalidRange is returned when an A1 range is invalid. ErrInvalidRange = errors.New("invalid A1 range") // ErrInvalidRow is returned when a row property is invalid. ErrInvalidRow = errors.New("invalid row") // ErrCellTooLong is returned when a text value exceeds the cell limit. ErrCellTooLong = errors.New("cell value is too long") // ErrTooManySheets is returned when a workbook exceeds its sheet limit. ErrTooManySheets = errors.New("too many worksheets") // ErrReadLimit is returned when a reader hits a configured safety limit. ErrReadLimit = errors.New("reader limit exceeded") // ErrCorruptFile is returned when an XLSX package is malformed. ErrCorruptFile = errors.New("corrupt XLSX file") // ErrEncrypted is returned for password-protected workbooks. ErrEncrypted = errors.New("encrypted workbooks are not supported") // ErrUnsupportedFormat is returned for formats such as XLSB. ErrUnsupportedFormat = errors.New("unsupported spreadsheet format") // ErrSheetNotFound is returned when a requested sheet does not exist. ErrSheetNotFound = errors.New("worksheet not found") // ErrSheetClosed is returned when reading from a closed worksheet. ErrSheetClosed = errors.New("worksheet reader is closed") // ErrInvalidCellReference is returned for malformed A1 references. ErrInvalidCellReference = errors.New("invalid cell reference") // ErrWrongCellType is returned when a typed cell accessor is used on the // wrong cell type. ErrWrongCellType = errors.New("wrong cell type") // ErrStreamingOrder is returned when a streaming writer is asked to // interleave multiple open worksheets. ErrStreamingOrder = errors.New("streaming writer requires one worksheet at a time") )
Sentinel errors returned by the package. They can be inspected with errors.Is.
Functions ¶
func Validate ¶ added in v0.2.0
func Validate(filePath string, opts ...ValidateOption) error
Validate checks that a file is a structurally valid XLSX package and that its worksheets can be streamed by Reader. It does not recalculate formulas.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/zignd/xlsx"
)
func exampleWorkbook() (string, func()) {
dir, err := os.MkdirTemp("", "xlsx-reader-example-*")
if err != nil {
panic(err)
}
path := filepath.Join(dir, "people.xlsx")
workbook := xlsx.NewWorkbook(path)
worksheet := workbook.AddWorksheet(&xlsx.WorksheetOptions{Name: "People"})
for _, person := range [][]interface{}{
{"Ada", 36},
{"Grace", 85},
} {
row, err := worksheet.AddRow()
if err != nil {
panic(err)
}
if err := row.Append(person...); err != nil {
panic(err)
}
}
if err := worksheet.CommitRows(); err != nil {
panic(err)
}
if err := workbook.Commit(); err != nil {
panic(err)
}
return path, func() { _ = os.RemoveAll(dir) }
}
func main() {
path, cleanup := exampleWorkbook()
defer cleanup()
if err := xlsx.Validate(path); err != nil {
panic(err)
}
fmt.Println("valid workbook")
}
Output: valid workbook
func ValidateReaderAt ¶ added in v0.2.0
func ValidateReaderAt(r io.ReaderAt, size int64, opts ...ValidateOption) error
ValidateReaderAt validates an XLSX package from an io.ReaderAt.
Types ¶
type Cell ¶
type Cell struct {
// Key is the column key used to create the cell, if any.
Key string
// Value is the cell value. It is written when the owning row is committed.
Value interface{}
// Formula is an optional formula without the leading "=".
Formula string
// Hyperlink is an optional external or internal hyperlink target.
Hyperlink string
// StyleID is the cell style ID returned by Workbook.AddStyle. Zero means
// the default style; dates automatically use the date style.
StyleID int
// contains filtered or unexported fields
}
Cell represents a cell in a worksheet. A row has a collection of cells.
func (*Cell) Identifier ¶
Identifier returns the A1-style reference of the cell (for example "B3").
func (*Cell) SetFormula ¶ added in v0.2.0
SetFormula sets a formula, removing an optional leading "=".
func (*Cell) SetHyperlink ¶ added in v0.2.0
SetHyperlink sets an external URL or an internal reference such as "#Sheet2!A1".
type CellData ¶ added in v0.2.0
type CellData struct {
Ref string
Column int
Row int
Type CellType
Raw string
Number float64
BoolValue bool
TimeValue time.Time
Formula string
StyleID int
HasValue bool
}
CellData is a cell returned by a SheetReader.
type CellOptions ¶
type CellOptions struct {
// Key is the optional column key.
Key *string
// Value is the initial cell value.
Value interface{}
}
CellOptions has options used when creating a new cell.
type CellType ¶ added in v0.2.0
type CellType uint8
CellType describes the type of a cell returned by the reader.
type Compression ¶ added in v0.2.0
type Compression uint8
Compression controls how the final ZIP package is compressed.
const ( // CompressionDefault uses the standard DEFLATE compression level. CompressionDefault Compression = iota // CompressionStore writes ZIP entries without compression. CompressionStore // CompressionBestSpeed favours speed over size. CompressionBestSpeed // CompressionBestCompression favours size over speed. CompressionBestCompression )
type Options ¶ added in v0.2.0
type Options struct {
// TempDir is the parent directory for temporary worksheet files. The
// library creates a private subdirectory inside it.
TempDir string
// BufferSize is the size of the per-worksheet write buffer. Zero uses the
// package default.
BufferSize int
// TempDirMode is the permission mode for temporary directories.
TempDirMode os.FileMode
// FileMode is the permission mode for the final .xlsx file.
FileMode os.FileMode
// Compression selects the ZIP compression strategy.
Compression Compression
// KeepTempFiles leaves the temporary workbook directory in place after
// Commit. It is useful for debugging.
KeepTempFiles bool
// Context cancels long-running writes. Nil means context.Background().
Context context.Context
SharedStrings bool
// FlushEveryRows controls how often NewStreamWriter flushes compressed
// bytes to the underlying writer. Zero means only on Commit/Flush.
FlushEveryRows int
// MaxCellLength limits text values. Zero uses Excel's 32,767-character
// limit.
MaxCellLength int
// MaxRows limits rows per worksheet. Zero uses Excel's limit.
MaxRows int
// MaxColumns limits columns per worksheet. Zero uses Excel's limit.
MaxColumns int
// MaxSheets limits the number of worksheets. Zero uses 1,024.
MaxSheets int
}
Options configures a workbook writer.
func DefaultOptions ¶ added in v0.2.0
func DefaultOptions() Options
DefaultOptions returns the default writer configuration.
type ReadOption ¶ added in v0.2.0
type ReadOption func(*ReadOptions)
ReadOption customizes ReadOptions.
func WithReadContext ¶ added in v0.2.0
func WithReadContext(ctx context.Context) ReadOption
WithReadContext sets the reader context.
func WithReadLimits ¶ added in v0.2.0
func WithReadLimits(maxUncompressedSize int64, maxEntries int) ReadOption
WithReadLimits overrides the reader safety limits.
func WithReadMaxCellLength ¶ added in v0.2.0
func WithReadMaxCellLength(length int) ReadOption
WithReadMaxCellLength limits text values.
func WithReadMaxColumns ¶ added in v0.2.0
func WithReadMaxColumns(columns int) ReadOption
WithReadMaxColumns limits columns per worksheet.
func WithReadMaxRows ¶ added in v0.2.0
func WithReadMaxRows(rows int) ReadOption
WithReadMaxRows limits rows per worksheet.
func WithReadTempDir ¶ added in v0.2.0
func WithReadTempDir(dir string) ReadOption
WithReadTempDir sets the parent directory for disk-backed shared strings.
func WithSharedStringsMode ¶ added in v0.2.0
func WithSharedStringsMode(mode SharedStringsMode) ReadOption
WithSharedStringsMode sets the shared-string storage mode.
type ReadOptions ¶ added in v0.2.0
type ReadOptions struct {
// Context cancels long-running reads. Nil means context.Background().
Context context.Context
SharedStringsMode SharedStringsMode
SharedStringsMemoryLimit int64
// TempDir is the parent directory for disk-backed shared strings.
TempDir string
// MaxUncompressedSize limits the total uncompressed package size.
MaxUncompressedSize int64
// MaxEntries limits the number of ZIP entries.
MaxEntries int
// MaxRows limits rows per worksheet.
MaxRows int
// MaxColumns limits columns per worksheet.
MaxColumns int
// MaxCellLength limits text values.
MaxCellLength int
}
ReadOptions configures a streaming XLSX reader.
func DefaultReadOptions ¶ added in v0.2.0
func DefaultReadOptions() ReadOptions
DefaultReadOptions returns the default reader configuration.
type Reader ¶ added in v0.2.0
type Reader struct {
// contains filtered or unexported fields
}
Reader is a streaming XLSX reader.
func Open ¶ added in v0.2.0
func Open(path string, opts ...ReadOption) (*Reader, error)
Open opens an XLSX file for streaming reads.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/zignd/xlsx"
)
func exampleWorkbook() (string, func()) {
dir, err := os.MkdirTemp("", "xlsx-reader-example-*")
if err != nil {
panic(err)
}
path := filepath.Join(dir, "people.xlsx")
workbook := xlsx.NewWorkbook(path)
worksheet := workbook.AddWorksheet(&xlsx.WorksheetOptions{Name: "People"})
for _, person := range [][]interface{}{
{"Ada", 36},
{"Grace", 85},
} {
row, err := worksheet.AddRow()
if err != nil {
panic(err)
}
if err := row.Append(person...); err != nil {
panic(err)
}
}
if err := worksheet.CommitRows(); err != nil {
panic(err)
}
if err := workbook.Commit(); err != nil {
panic(err)
}
return path, func() { _ = os.RemoveAll(dir) }
}
func main() {
path, cleanup := exampleWorkbook()
defer cleanup()
reader, err := xlsx.Open(path)
if err != nil {
panic(err)
}
defer reader.Close()
sheet, err := reader.OpenSheet("People")
if err != nil {
panic(err)
}
defer sheet.Close()
for sheet.Next() {
name, _ := sheet.Row().CellAt(0)
age, _ := sheet.Row().CellAt(1)
nameValue, _ := name.String()
ageValue, _ := age.Int64()
fmt.Printf("%s is %d\n", nameValue, ageValue)
}
if err := sheet.Err(); err != nil {
panic(err)
}
}
Output: Ada is 36 Grace is 85
func OpenReaderAt ¶ added in v0.2.0
OpenReaderAt opens an XLSX package from an io.ReaderAt.
type Row ¶
type Row struct {
// Height is the optional row height in points. Zero means the default.
Height float64
// contains filtered or unexported fields
}
Row represents a row in a worksheet. A worksheet has a collection of rows.
func (*Row) AddCell ¶
AddCell adds a new cell to the row. It can only be used on worksheets where columns were not defined.
func (*Row) AddCellWithKey ¶
AddCellWithKey is just like AddCell but adds a key to the cell. It should be used when columns were defined on the worksheet.
func (*Row) Append ¶ added in v0.2.0
Append adds a series of values to the row. On keyed worksheets the values are mapped to columns in definition order.
func (*Row) SetHeight ¶ added in v0.2.0
SetHeight sets the row height in points. Excel accepts 0 (default) through 409.
func (*Row) WriteFloat ¶ added in v0.2.0
WriteFloat appends a floating-point cell.
func (*Row) WriteString ¶ added in v0.2.0
WriteString appends a string cell. It can only be used on worksheets without keyed columns; use AddCellWithKey for keyed worksheets.
type RowData ¶ added in v0.2.0
RowData is one row returned by a SheetReader.
type SharedStringsMode ¶ added in v0.2.0
type SharedStringsMode uint8
SharedStringsMode controls how shared strings are read.
const ( // disk. SharedStringsAuto SharedStringsMode = iota SharedStringsMemory // on demand. SharedStringsDisk )
type SheetReader ¶ added in v0.2.0
type SheetReader struct {
// contains filtered or unexported fields
}
SheetReader streams rows from a worksheet.
func (*SheetReader) Close ¶ added in v0.2.0
func (s *SheetReader) Close() error
Close releases the worksheet stream.
func (*SheetReader) Err ¶ added in v0.2.0
func (s *SheetReader) Err() error
Err returns the first read error.
func (*SheetReader) Info ¶ added in v0.2.0
func (s *SheetReader) Info() SheetInfo
Info returns the worksheet metadata.
func (*SheetReader) Next ¶ added in v0.2.0
func (s *SheetReader) Next() bool
Next advances to the next row. It returns false at the end of the sheet or after an error; check Err in that case.
func (*SheetReader) Row ¶ added in v0.2.0
func (s *SheetReader) Row() *RowData
Row returns the most recently read row. The returned value is only valid until the next call to Next.
type Style ¶ added in v0.2.0
type Style struct {
Bold bool
Italic bool
Underline bool
FontSize float64
FontColor string
FillColor string
NumberFormat string
Horizontal string
Vertical string
WrapText bool
}
Style is a cell style. The zero value is the default workbook style.
type ValidateOption ¶ added in v0.2.0
type ValidateOption = ReadOption
ValidateOption customizes Validate and ValidateReaderAt.
type Workbook ¶
type Workbook struct {
// FilePath is the destination of the XLSX file written by Commit.
FilePath string
// contains filtered or unexported fields
}
Workbook represents a spreadsheet workbook.
func NewStreamWriter ¶ added in v0.3.0
NewStreamWriter creates a workbook that writes the ZIP package directly to w as rows are committed. It does not create temporary worksheet files.
The caller owns w. Call Flush to push buffered compressed bytes to the underlying writer, and Commit to write the ZIP central directory. Only one worksheet may be open at a time: finish the current worksheet with CommitRows and Commit before writing rows to another sheet.
func NewWorkbook ¶
NewWorkbook creates a new workbook, which is the base for every XLSX file. The file is written when Commit is called.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/zignd/xlsx"
)
func main() {
dir, err := os.MkdirTemp("", "xlsx-example-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
workbook := xlsx.NewWorkbook(filepath.Join(dir, "report.xlsx"))
worksheet := workbook.AddWorksheet(&xlsx.WorksheetOptions{Name: "Sales"})
for i := 0; i < 3; i++ {
row, err := worksheet.AddRow()
if err != nil {
panic(err)
}
product, err := row.AddCell()
if err != nil {
panic(err)
}
product.Value = fmt.Sprintf("Product %d", i+1)
price, err := row.AddCell()
if err != nil {
panic(err)
}
price.Value = 9.99 * float64(i+1)
if err := worksheet.CommitRows(); err != nil {
panic(err)
}
}
if err := workbook.Commit(); err != nil {
panic(err)
}
fmt.Println("created report.xlsx with 3 rows")
}
Output: created report.xlsx with 3 rows
func NewWorkbookWithOptions ¶ added in v0.2.0
NewWorkbookWithOptions creates a workbook with explicit writer options.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/zignd/xlsx"
)
func main() {
dir, err := os.MkdirTemp("", "xlsx-options-example-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
workbook, err := xlsx.NewWorkbookWithOptions(filepath.Join(dir, "styled.xlsx"), xlsx.Options{
TempDir: dir,
SharedStrings: true,
})
if err != nil {
panic(err)
}
style, err := workbook.AddStyle(xlsx.Style{Bold: true, NumberFormat: "#,##0.00"})
if err != nil {
panic(err)
}
worksheet := workbook.AddWorksheet(&xlsx.WorksheetOptions{Name: "Data"})
if err := worksheet.FreezePanes(1, 0); err != nil {
panic(err)
}
row, err := worksheet.AddRow()
if err != nil {
panic(err)
}
amount, err := row.WriteFloat(1234.5)
if err != nil {
panic(err)
}
amount.SetStyle(style)
amount.SetHyperlink("https://example.com")
formula, err := row.AddCell()
if err != nil {
panic(err)
}
formula.SetFormula("=A1*2")
if err := worksheet.CommitRows(); err != nil {
panic(err)
}
if err := workbook.Commit(); err != nil {
panic(err)
}
fmt.Printf("style ID: %d\n", style)
}
Output: style ID: 2
func NewWriter ¶ added in v0.2.0
NewWriter creates a workbook that writes its final package to w instead of a file path. The caller owns w; the workbook does not close it.
func (*Workbook) AddStyle ¶ added in v0.2.0
AddStyle registers a style and returns its style ID. Style ID 0 is the default style and style ID 1 is reserved for dates.
func (*Workbook) AddWorksheet ¶
func (wb *Workbook) AddWorksheet(opts *WorksheetOptions) *Worksheet
AddWorksheet adds a new worksheet to the workbook.
Invalid or duplicate worksheet names are reported when the worksheet (or the workbook) is committed, because this method intentionally keeps the original error-free API.
func (*Workbook) Cleanup ¶
Cleanup removes the temporary files used to build the workbook. It is safe to call multiple times and is called automatically after a successful Commit.
func (*Workbook) Close ¶ added in v0.2.0
Close releases temporary resources. It is an idempotent alias for Cleanup and is intended for defer use.
func (*Workbook) Commit ¶
Commit commits the workbook, persisting the data to the file path supplied to NewWorkbook. Worksheets with no pending rows are finalized automatically.
func (*Workbook) Flush ¶ added in v0.3.0
Flush pushes buffered data to the underlying writer. For a streaming writer it also calls Flush on the underlying writer when it implements interface{ Flush() }, which makes it useful with http.Flusher.
func (*Workbook) HasPendingWorksheets ¶
HasPendingWorksheets indicates whether the workbook has worksheets that have not been finalized yet.
func (*Workbook) TempDir ¶ added in v0.2.0
TempDir returns the current private temporary directory, or an empty string before temporary files have been created.
func (*Workbook) Worksheets ¶
Worksheets returns a copy of the workbook's worksheet slice.
type Worksheet ¶
type Worksheet struct {
// contains filtered or unexported fields
}
Worksheet represents a worksheet in a workbook.
func (*Worksheet) AddRow ¶
AddRow adds a new row to the worksheet. Rows are buffered in memory until CommitRows is called.
func (*Worksheet) Commit ¶
Commit commits the worksheet, writing its footer and closing the underlying file. Workbook.Commit finalizes worksheets automatically; calling Commit on an already committed worksheet returns ErrWorksheetCommitted.
func (*Worksheet) CommitRows ¶
CommitRows writes the rows stored in memory to disk. It can be called repeatedly to stream data without keeping the whole worksheet in memory.
func (*Worksheet) DefineColumns ¶
func (ws *Worksheet) DefineColumns(columns []*WorksheetColumn) error
DefineColumns defines the worksheet columns and writes the header row. It is optional; a worksheet can be used without columns by calling AddCell.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/zignd/xlsx"
)
func main() {
dir, err := os.MkdirTemp("", "xlsx-example-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
workbook := xlsx.NewWorkbook(filepath.Join(dir, "report.xlsx"))
worksheet := workbook.AddWorksheet(&xlsx.WorksheetOptions{Name: "Sales"})
err = worksheet.DefineColumns([]*xlsx.WorksheetColumn{
{Key: "product", Value: "Product", Width: 30},
{Key: "amount", Value: "Amount"},
})
if err != nil {
panic(err)
}
row, err := worksheet.AddRow()
if err != nil {
panic(err)
}
product, err := row.AddCellWithKey("product")
if err != nil {
panic(err)
}
product.Value = "Keyboard"
amount, err := row.AddCellWithKey("amount")
if err != nil {
panic(err)
}
amount.Value = 49.9
if err := worksheet.CommitRows(); err != nil {
panic(err)
}
if err := workbook.Commit(); err != nil {
panic(err)
}
fmt.Println("created a keyed worksheet")
}
Output: created a keyed worksheet
func (*Worksheet) FileName ¶
FileName returns the name of the worksheet part inside the XLSX package.
func (*Worksheet) FreezePanes ¶ added in v0.2.0
FreezePanes freezes the given number of rows and columns. It must be called before any rows are committed.
func (*Worksheet) MergeCells ¶ added in v0.2.0
MergeCells merges one or more A1 ranges. Ranges must be valid and must not overlap.
Example ¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/zignd/xlsx"
)
func main() {
dir, err := os.MkdirTemp("", "xlsx-layout-example-*")
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)
workbook := xlsx.NewWorkbook(filepath.Join(dir, "layout.xlsx"))
worksheet := workbook.AddWorksheet(&xlsx.WorksheetOptions{Name: "Data"})
if err := worksheet.FreezePanes(1, 0); err != nil {
panic(err)
}
if err := worksheet.SetAutoFilter("A1:B2"); err != nil {
panic(err)
}
if err := worksheet.MergeCells("A3:B3"); err != nil {
panic(err)
}
row, err := worksheet.AddRow()
if err != nil {
panic(err)
}
if err := row.Append("Name", "Amount"); err != nil {
panic(err)
}
if err := worksheet.CommitRows(); err != nil {
panic(err)
}
if err := workbook.Commit(); err != nil {
panic(err)
}
fmt.Println("layout written")
}
Output: layout written
func (*Worksheet) SetAutoFilter ¶ added in v0.2.0
SetAutoFilter sets the auto-filter range for the worksheet.
type WorksheetColumn ¶
type WorksheetColumn struct {
// Key identifies the column when calling AddCellWithKey.
Key string
// Value is the header text written in the first row.
Value string
// Width is the optional column width in Excel units. A value of zero
// leaves the column at its default width.
Width float64
}
WorksheetColumn represents a column in a worksheet.
type WorksheetOptions ¶
type WorksheetOptions struct {
// Name is the worksheet name shown in Excel.
Name string
}
WorksheetOptions has options used when creating a new worksheet.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Command basic demonstrates the everyday writer and reader API.
|
Command basic demonstrates the everyday writer and reader API. |
|
complex
command
Command complex generates a feature-rich workbook.
|
Command complex generates a feature-rich workbook. |
|
http
command
Command http demonstrates both ways of serving an XLSX download over HTTP: a temp-file-backed writer for smaller reports and a zero-temp streaming writer for large, long-running exports.
|
Command http demonstrates both ways of serving an XLSX download over HTTP: a temp-file-backed writer for smaller reports and a zero-temp streaming writer for large, long-running exports. |
|
read
command
Command read demonstrates validation and memory-flat reading.
|
Command read demonstrates validation and memory-flat reading. |
|
internal
|
|
|
testgen
command
Command testgen writes a feature-rich workbook used by the interoperability CI job.
|
Command testgen writes a feature-rich workbook used by the interoperability CI job. |