Documentation
¶
Overview ¶
Package xlsx lets you create XLSX files incrementally.
Rows are buffered only until CommitRows is called and are then streamed to temporary files on disk. This keeps memory usage low even when writing very large spreadsheets.
Quick start ¶
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.Value = "Revenue"
if err := worksheet.CommitRows(); err != nil {
log.Fatal(err)
}
if err := worksheet.Commit(); err != nil {
log.Fatal(err)
}
if err := workbook.Commit(); err != nil {
log.Fatal(err)
}
Values ¶
Cell.Value accepts strings, booleans, all integer and unsigned integer types, floats, time.Time, []byte and database/sql/driver.Valuer implementations (such as the sql.Null* types). Unsupported values are reported when rows are committed.
Streaming ¶
Call Worksheet.CommitRows after adding a batch of rows to release the rows from memory. The worksheet keeps a buffered writer open, so repeated batches are cheap to append.
Index ¶
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") )
Sentinel errors returned by the package. They can be inspected with errors.Is.
Functions ¶
This section is empty.
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{}
// 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").
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 Row ¶
type Row struct {
// 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.
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 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 (*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) 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) HasPendingWorksheets ¶
HasPendingWorksheets indicates whether the workbook has worksheets that have not been finalized yet.
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.
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.