Documentation
¶
Overview ¶
Package spreadsheet is a pure-Go drop-in replacement for Excelize that adds full support for legacy .xls files (Excel 97-2003 BIFF8) in addition to the modern .xlsx / .xlsm / .xltx format.
Quick start:
f, err := spreadsheet.OpenFile("book.xlsx")
if err != nil { log.Fatal(err) }
defer f.Close()
val, _ := f.GetCellValue("Sheet1", "A1")
fmt.Println(val)
Index ¶
- Variables
- type Alignment
- type Border
- type DocProps
- type File
- func (f *File) AddPicture(sheet, cell, picture string, opts *GraphicOptions) error
- func (f *File) AutoFilter(sheet, hCell, vCell string) error
- func (f *File) Close() error
- func (f *File) DeleteSheet(name string) error
- func (f *File) DuplicateSheet(from, to int) error
- func (f *File) FreezePane(sheet, cell string, row, col int) error
- func (f *File) GetCellFormula(sheet, axis string) (string, error)
- func (f *File) GetCellValue(sheet, axis string) (string, error)
- func (f *File) GetCols(sheet string) ([][]string, error)
- func (f *File) GetRows(sheet string) ([][]string, error)
- func (f *File) GetSheetIndex(name string) (int, error)
- func (f *File) GetSheetList() ([]string, error)
- func (f *File) GetSheetName(index int) (string, error)
- func (f *File) InsertCols(sheet, col string, n int) error
- func (f *File) InsertRows(sheet string, row int) error
- func (f *File) MergeCell(sheet, hCell, vCell string) error
- func (f *File) NewSheet(name string) (int, error)
- func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error)
- func (f *File) NewStyle(style *StyleConfig) (int, error)
- func (f *File) ProtectSheet(sheet string, opts *SheetProtection) error
- func (f *File) RemoveCol(sheet, col string) error
- func (f *File) RemoveRow(sheet string, row int) error
- func (f *File) Rows(sheet string) (*Rows, error)
- func (f *File) Save(path string) error
- func (f *File) SaveAs(path string) error
- func (f *File) SetCellFormula(sheet, axis, formula string) error
- func (f *File) SetCellStyle(sheet, hCell, vCell string, styleID int) error
- func (f *File) SetCellValue(sheet, axis string, value any) error
- func (f *File) SetDocProps(props *DocProps) error
- func (f *File) SetSheetName(oldName, newName string) error
- func (f *File) UnmergeCell(sheet, hCell string) error
- func (f *File) UnprotectSheet(sheet, password string) error
- type FileType
- type Fill
- type Font
- type GraphicOptions
- type Rows
- type SheetEntity
- type SheetProtection
- type SheetsCollection
- type StreamWriter
- type StyleConfig
- type WorkBookEntity
Constants ¶
This section is empty.
Variables ¶
var ( // ErrColumnWidth defines an error for the column width isn't valid. ErrColumnWidth = errors.New("invalid column width") // ErrParameterInvalid defines an error for the parameter is invalid. ErrParameterInvalid = errors.New("parameter is invalid") // ErrStreamSetRow defines an error for the stream writer using the SetRow function. ErrStreamSetRow = errors.New("can't use SetRow in stream mode") // ErrUnknownEncryptMechanism defines an error for unknown encryption mechanism. ErrUnknownEncryptMechanism = errors.New("unknown encryption mechanism") // ErrUnsupportedChartType defines an error for the unsupported chart type. ErrUnsupportedChartType = errors.New("unsupported chart type") // ErrUnsupportedFileType defines an error for the unsupported file type. ErrUnsupportedFileType = errors.New("unsupported file type") // ErrWorkbookFileFormatUndefined defines an error for the workbook file format is undefined. ErrWorkbookFileFormatUndefined = errors.New("workbook file format is undefined") // ErrMaxRows defines an error for the maximum number of rows exceeded. ErrMaxRows = errors.New("max number of rows exceeded") // ErrMaxColumns defines an error for the maximum number of columns exceeded. ErrMaxColumns = errors.New("max number of columns exceeded") )
Common errors returned by the spreadsheet package.
Functions ¶
This section is empty.
Types ¶
type File ¶
type File struct {
// Path is the file path from which the document was loaded (empty for new files).
Path string
// Type indicates the underlying file format.
Type FileType
// WorkBook is the exposed workbook entity (both spellings for compat).
WorkBook *xmltypes.WorkBookEntity
Workbook *xmltypes.WorkBookEntity
// contains filtered or unexported fields
}
File represents an open spreadsheet document. It transparently wraps either an .xls or an .xlsx file and exposes a unified Excelize-compatible API.
func OpenFile ¶
OpenFile opens a spreadsheet document from the local filesystem. The format is determined from the file extension (.xls, .xlsx, .xlsm, .xlt, .xltx).
Example:
f, err := spreadsheet.OpenFile("report.xlsx")
if err != nil { log.Fatal(err) }
defer f.Close()
func OpenReader ¶
OpenReader opens a spreadsheet document from an io.Reader. The format is auto-detected from the stream's magic bytes.
Example:
f, err := spreadsheet.OpenReader(os.Stdin)
func (*File) AddPicture ¶
func (f *File) AddPicture(sheet, cell, picture string, opts *GraphicOptions) error
AddPicture embeds a picture file at the given cell in a worksheet.
func (*File) AutoFilter ¶
AutoFilter sets an auto-filter for the given range.
func (*File) DeleteSheet ¶
DeleteSheet removes the named worksheet from the workbook.
func (*File) DuplicateSheet ¶
DuplicateSheet duplicates the worksheet at index from and appends the copy.
func (*File) FreezePane ¶
FreezePane creates a freeze pane at the given row and column.
func (*File) GetCellFormula ¶
GetCellFormula returns the formula stored in the given cell.
func (*File) GetCellValue ¶
GetCellValue returns the raw string value of the cell at axis (e.g. "A1").
func (*File) GetSheetIndex ¶
GetSheetIndex returns the 0-based index of the named worksheet, or -1 if not found.
func (*File) GetSheetList ¶
GetSheetList returns the names of all worksheets in order.
func (*File) GetSheetName ¶
GetSheetName returns the name of the worksheet at the given 0-based index.
func (*File) InsertCols ¶
InsertCols inserts n columns before the given column letter.
func (*File) InsertRows ¶
InsertRows inserts n empty rows after the given 1-based row index.
func (*File) NewSheet ¶
NewSheet creates a new worksheet with the given name. Returns the 0-based index of the new sheet.
func (*File) NewStreamWriter ¶
func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error)
NewStreamWriter returns a streaming writer for the named worksheet.
func (*File) NewStyle ¶
func (f *File) NewStyle(style *StyleConfig) (int, error)
NewStyle creates a new style from a StyleConfig and returns its integer index.
func (*File) ProtectSheet ¶
func (f *File) ProtectSheet(sheet string, opts *SheetProtection) error
ProtectSheet applies password protection to a worksheet.
func (*File) Rows ¶
Rows returns a streaming row iterator for the named worksheet.
Example:
rows, _ := f.Rows("Sheet1")
for rows.Next() {
cols, _ := rows.Columns()
_ = cols
}
func (*File) SetCellFormula ¶
SetCellFormula writes a formula string to the given cell.
func (*File) SetCellStyle ¶
SetCellStyle applies a named style index to a range of cells from hCell to vCell.
func (*File) SetCellValue ¶
SetCellValue sets the value of a cell. The value may be any Go type; it will be converted to its string representation.
func (*File) SetDocProps ¶
SetDocProps sets document-level metadata properties.
func (*File) SetSheetName ¶
SetSheetName renames a worksheet from oldName to newName.
func (*File) UnmergeCell ¶
UnmergeCell removes a merge from the given range.
func (*File) UnprotectSheet ¶
UnprotectSheet removes protection from a worksheet.
type GraphicOptions ¶
type GraphicOptions = xmltypes.GraphicOptions
GraphicOptions holds options for embedding pictures.
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows is a streaming row iterator for a worksheet. It reads one row at a time, suitable for very large files.
type SheetEntity ¶
type SheetEntity = xmltypes.SheetEntity
SheetEntity represents a single sheet entry in the workbook. Kept for backward compatibility.
type SheetProtection ¶
type SheetProtection = xmltypes.SheetProtection
SheetProtection holds sheet-protection settings.
type SheetsCollection ¶
type SheetsCollection = xmltypes.SheetsCollection
SheetsCollection represents all sheets in a workbook. Kept for backward compatibility.
type StreamWriter ¶
type StreamWriter struct {
// contains filtered or unexported fields
}
StreamWriter provides a streaming write interface for large datasets. Use SetRow to append rows one at a time, then call Flush when done.
Example:
sw, _ := f.NewStreamWriter("Sheet1")
sw.SetRow("A1", []any{1, "Alice", 99.5})
sw.SetRow("A2", []any{2, "Bob", 87.0})
sw.Flush()
func (*StreamWriter) Flush ¶
func (sw *StreamWriter) Flush() error
Flush commits all pending row data to the document. It must be called after all rows have been written.
type StyleConfig ¶
type StyleConfig = xmltypes.StyleConfig
StyleConfig holds all styling properties that can be applied to a cell.
type WorkBookEntity ¶
type WorkBookEntity = xmltypes.WorkBookEntity
WorkBookEntity represents the main workbook structure.
Directories
¶
| Path | Synopsis |
|---|---|
|
Command examples demonstrates the go-spreadsheet library.
|
Command examples demonstrates the go-spreadsheet library. |
|
internal
|
|
|
ole
Package ole implements a pure-Go reader for OLE2 Compound Document files.
|
Package ole implements a pure-Go reader for OLE2 Compound Document files. |
|
ooxml
Package ooxml implements a pure-Go parser and writer for Office Open XML (OOXML) spreadsheet files (.xlsx, .xlsm, .xltx, .xltm).
|
Package ooxml implements a pure-Go parser and writer for Office Open XML (OOXML) spreadsheet files (.xlsx, .xlsm, .xltx, .xltm). |
|
xmltypes
Package xmltypes defines shared types used across the go-spreadsheet library.
|
Package xmltypes defines shared types used across the go-spreadsheet library. |
|
Package xls provides a pure-Go reader and writer for legacy Excel .xls files (BIFF5/BIFF8 format) via OLE2 compound document parsing.
|
Package xls provides a pure-Go reader and writer for legacy Excel .xls files (BIFF5/BIFF8 format) via OLE2 compound document parsing. |
|
Package xlsx provides a pure-Go reader and writer for Office Open XML spreadsheet files (.xlsx, .xlsm, .xltx, .xltm).
|
Package xlsx provides a pure-Go reader and writer for Office Open XML spreadsheet files (.xlsx, .xlsm, .xltx, .xltm). |