excel_stream

package module
v0.0.0-...-042a6ab Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2017 License: BSD-2-Clause Imports: 9 Imported by: 0

README

excel_stream

The purpose of the StreamFile library is to allow streamed writing of XLSX files. It relies heavily on the XLSX library. Directions:

  1. Create a StreamFileBuilder with NewStreamFileBuilder() or NewStreamFileBuilderForPath().
  2. Add the sheets and their first row of data by calling AddSheet().
  3. Call Build() to get a StreamFile. Once built, all functions on the builder will return an error.
  4. Write to the StreamFile with WriteRow(). Writes begin on the first sheet. New rows are always written and flushed to the io. All rows written to the same sheet must have the same number of cells as the header provided when the sheet was created or an error will be returned.
  5. Call NextSheet() to proceed to the next sheet. Once NextSheet() is called, the previous sheet can not be edited.
  6. Call Close() to finish.

Future work suggestions: Currently the only supported cell type is string, since the main reason this library was written was to prevent strings from being interpreted as numbers. It would be nice to have support for numbers and money so that the exported files could better take advantage of Excel's features. All text is written with the same text style. Support for additional text styles could be added to highlight certain data in the file. The current default style uses fonts that are not on Macs by default so opening the XLSX files in Numbers causes a pop up that says there are missing fonts. The font could be changed to something that is usually found on Mac and PC.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NoCurrentSheetError     = errors.New("No Current Sheet")
	WrongNumberOfRowsError  = errors.New("Invalid number of cells passed to WriteRow. All calls to WriteRow on the same sheet must have the same number of cells.")
	AlreadyOnLastSheetError = errors.New("NextSheet() called, but already on last sheet.")
	UnsupportedCellType     = errors.New("Unsupported cell type")
	UnknownCellType         = errors.New("Unknown cell type")
)
View Source
var BuiltExcelStreamBuilderError = errors.New("StreamFileBuilder has already been built, functions may no longer be used")

Functions

This section is empty.

Types

type StreamFile

type StreamFile struct {
	// contains filtered or unexported fields
}

func (*StreamFile) Close

func (sf *StreamFile) Close() error

Close closes the Stream File. Any sheets that have not yet been written to will have an empty sheet created for them.

func (*StreamFile) NextSheet

func (sf *StreamFile) NextSheet() error

NextSheet will switch to the next sheet. Sheets are selected in the same order they were added. Once you leave a sheet, you cannot return to it.

func (*StreamFile) WriteRow

func (sf *StreamFile) WriteRow(cells []string) error

WriteRow will write a row of cells to the current sheet. Every call to WriteRow on the same sheet must contain the same number of cells as the header provided when the sheet was created or an error will be returned. This function will always trigger a flush on success. Currently the only supported data type is string data.

type StreamFileBuilder

type StreamFileBuilder struct {
	// contains filtered or unexported fields
}

func NewStreamFileBuilder

func NewStreamFileBuilder(writer io.Writer) *StreamFileBuilder

NewExcelBuilder creates an StreamFileBuilder that will write to the the provided io.writer

func NewStreamFileBuilderForPath

func NewStreamFileBuilderForPath(path string) (*StreamFileBuilder, error)

NewExcelBuilderForFile takes the name of an XLSX file and returns a builder for it. The file will be created if it does not exist, or truncated if it does.

func (*StreamFileBuilder) AddSheet

func (sb *StreamFileBuilder) AddSheet(name string, headers []string) error

AddSheet will add sheets with the given name with the provided headers. The headers cannot be edited later, and all rows written to the sheet must contain the same number of cells as the header. Sheet names must be unique, or an error will be thrown.

func (*StreamFileBuilder) Build

func (sb *StreamFileBuilder) Build() (*StreamFile, error)

Build begins streaming the XLSX file to the io, by writing all the Excel metadata. It creates a StreamFile struct that can be used to write the rows to the sheets.

Jump to

Keyboard shortcuts

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