export

package
v0.1.36 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

export/csv.go

export/excel.go

Index

Constants

View Source
const (
	NumberFormatGeneral    = "General"
	NumberFormatInteger    = "0"
	NumberFormatDecimal2   = "0.00"
	NumberFormatPercent    = "0%"
	NumberFormatPercent2   = "0.00%"
	NumberFormatCurrency   = `"$"#,##0.00`
	NumberFormatDate       = "yyyy-mm-dd"
	NumberFormatDateTime   = "yyyy-mm-dd hh:mm:ss"
	NumberFormatTime       = "hh:mm:ss"
	NumberFormatAccounting = `_("$"* #,##0.00_)`
)

Common number formats

Variables

View Source
var (
	ErrNoData       = errors.New("export: no data to export")
	ErrInvalidData  = errors.New("export: data must be a slice")
	ErrEmptyHeaders = errors.New("export: headers cannot be empty")
)

Common errors.

Functions

This section is empty.

Types

type CSV

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

CSV represents a CSV exporter.

func FromCSV

func FromCSV(data any) *CSV

FromCSV is a convenience function to create a CSV from data.

func NewCSV

func NewCSV() *CSV

NewCSV creates a new CSV exporter.

func (*CSV) Bytes

func (c *CSV) Bytes() ([]byte, error)

Bytes returns the CSV as bytes.

func (*CSV) Delimiter

func (c *CSV) Delimiter(d rune) *CSV

Delimiter sets the field delimiter (default: comma).

func (*CSV) From

func (c *CSV) From(data any) *CSV

From populates the CSV from a slice of structs or maps.

func (*CSV) Headers

func (c *CSV) Headers(headers ...string) *CSV

Headers sets the column headers.

func (*CSV) Row

func (c *CSV) Row(values ...string) *CSV

Row adds a single row.

func (*CSV) Rows

func (c *CSV) Rows(rows [][]string) *CSV

Rows adds multiple rows.

func (*CSV) Save

func (c *CSV) Save(filename string) error

Save saves the CSV to a file.

func (*CSV) ServeHTTP

func (c *CSV) ServeHTTP(w http.ResponseWriter, filename string) error

ServeHTTP writes the CSV as an HTTP response.

func (*CSV) String

func (c *CSV) String() (string, error)

String returns the CSV as a string.

func (*CSV) TabDelimited

func (c *CSV) TabDelimited() *CSV

TabDelimited sets tab as the delimiter.

func (*CSV) UseLF

func (c *CSV) UseLF() *CSV

UseLF uses LF line endings instead of CRLF.

func (*CSV) Write

func (c *CSV) Write(w io.Writer) error

Write writes the CSV to a writer.

type CSVReader

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

CSVReader represents a CSV reader with struct mapping.

func ReadCSV

func ReadCSV(r io.Reader) *CSVReader

ReadCSV creates a new CSV reader.

func ReadCSVFile

func ReadCSVFile(filename string) (*CSVReader, error)

ReadCSVFile opens and reads a CSV file.

func (*CSVReader) Delimiter

func (r *CSVReader) Delimiter(d rune) *CSVReader

Delimiter sets the field delimiter.

func (*CSVReader) Into

func (r *CSVReader) Into(dest any) error

Into reads CSV into a slice of structs.

func (*CSVReader) ReadAll

func (r *CSVReader) ReadAll() ([][]string, error)

ReadAll reads all records.

func (*CSVReader) ReadAllWithHeaders

func (r *CSVReader) ReadAllWithHeaders() ([]map[string]string, error)

ReadAllWithHeaders reads all records, treating the first row as headers.

func (*CSVReader) TabDelimited

func (r *CSVReader) TabDelimited() *CSVReader

TabDelimited sets tab as the delimiter.

type CellStyle

type CellStyle struct {
	Bold      bool
	Italic    bool
	FontSize  float64
	FontColor string
	FillColor string
	Alignment string // left, center, right
	NumFormat string
	Border    bool
}

CellStyle represents cell styling options.

type Excel

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

Excel represents an Excel workbook exporter.

func NewExcel

func NewExcel() *Excel

NewExcel creates a new Excel workbook.

func (*Excel) Bytes

func (e *Excel) Bytes() ([]byte, error)

Bytes returns the Excel file as bytes.

func (*Excel) Close

func (e *Excel) Close() error

Close closes the Excel file and releases resources.

func (*Excel) Save

func (e *Excel) Save(filename string) error

Save saves the Excel file.

func (*Excel) ServeHTTP

func (e *Excel) ServeHTTP(w http.ResponseWriter, filename string) error

ServeHTTP writes the Excel file as an HTTP response.

func (*Excel) Sheet

func (e *Excel) Sheet(name string) *ExcelSheet

Sheet creates or gets a sheet by name.

func (*Excel) Write

func (e *Excel) Write(w io.Writer) error

Write writes the Excel file to a writer.

type ExcelSheet

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

ExcelSheet represents a sheet in an Excel workbook.

func FromExcel

func FromExcel(data any) *ExcelSheet

FromExcel is a convenience function to create an Excel file from data.

func (*ExcelSheet) AddFormula

func (s *ExcelSheet) AddFormula(col, row int, formula string) *ExcelSheet

AddFormula adds a formula to a cell.

func (*ExcelSheet) AddTable

func (s *ExcelSheet) AddTable() *ExcelSheet

AddTable formats the data as an Excel table.

func (*ExcelSheet) AutoWidth

func (s *ExcelSheet) AutoWidth() *ExcelSheet

AutoWidth enables auto-width for all columns based on content.

func (*ExcelSheet) Build

func (s *ExcelSheet) Build() *Excel

Build writes the sheet data to the Excel file.

func (*ExcelSheet) ColWidth

func (s *ExcelSheet) ColWidth(col int, width float64) *ExcelSheet

ColWidth sets the width of a column (1-indexed).

func (*ExcelSheet) FreezeHeader

func (s *ExcelSheet) FreezeHeader() *ExcelSheet

FreezeHeader freezes the header row.

func (*ExcelSheet) From

func (s *ExcelSheet) From(data any) *ExcelSheet

From populates the sheet from a slice of structs or maps.

func (*ExcelSheet) Headers

func (s *ExcelSheet) Headers(headers ...string) *ExcelSheet

Headers sets the column headers.

func (*ExcelSheet) MergeCells

func (s *ExcelSheet) MergeCells(startCol, startRow, endCol, endRow int) *ExcelSheet

MergeCells merges a range of cells.

func (*ExcelSheet) Row

func (s *ExcelSheet) Row(values ...any) *ExcelSheet

Row adds a single row.

func (*ExcelSheet) Rows

func (s *ExcelSheet) Rows(rows [][]any) *ExcelSheet

Rows adds multiple rows.

func (*ExcelSheet) SetCellStyle

func (s *ExcelSheet) SetCellStyle(startCol, startRow, endCol, endRow int, style CellStyle) *ExcelSheet

SetCellStyle sets style for a range of cells.

func (*ExcelSheet) SetDateFormat

func (s *ExcelSheet) SetDateFormat(col int, format string) *ExcelSheet

SetDateFormat sets date format for a column.

func (*ExcelSheet) SetNumberFormat

func (s *ExcelSheet) SetNumberFormat(col int, format string) *ExcelSheet

SetNumberFormat sets number format for a column.

func (*ExcelSheet) Sheet

func (s *ExcelSheet) Sheet(name string) *ExcelSheet

Sheet returns to the Excel builder to add more sheets.

type TimeValue

type TimeValue struct {
	Time   time.Time
	Format string
}

TimeValue wraps a time.Time with a specific format for Excel.

func ExcelDateTime

func ExcelDateTime(t time.Time) TimeValue

ExcelDateTime creates a TimeValue with date and time format.

func ExcelTime

func ExcelTime(t time.Time) TimeValue

ExcelTime creates a TimeValue for proper Excel date handling.

Jump to

Keyboard shortcuts

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