Documentation ¶
Overview ¶
Package exl
Excel binding to struct written in Go.(Only supports Go1.18+)
Index ¶
- Variables
- func NewFileFromSlice[T WriteConfigurator](ts []T) *xlsx.File
- func Read[T ReadConfigurator](reader io.Reader, filterFunc ...func(t T) (add bool)) ([]T, error)
- func ReadBinary[T ReadConfigurator](bytes []byte, filterFunc ...func(t T) (add bool)) ([]T, error)
- func ReadExcel(file string, sheetIndex int, walk func(index int, rows *xlsx.Row)) error
- func ReadFile[T ReadConfigurator](file string, filterFunc ...func(t T) (add bool)) ([]T, error)
- func UnmarshalBool(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalExcelUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalFloat(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalString(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalTextUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalTime(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func UnmarshalUInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
- func WriteExcel(file string, data [][]string) error
- func WriteExcelTo(w io.Writer, data [][]string) error
- func WriteFile[T WriteConfigurator](file string, ts []T) error
- func WriteTo[T WriteConfigurator](w io.Writer, ts []T) error
- type ContentError
- type ExcelUnmarshalParameters
- type ExcelUnmarshaler
- type FieldError
- type ReadConfig
- type ReadConfigurator
- type UnmarshalErrorHandling
- type UnmarshalExcelFunc
- type WriteConfig
- type WriteConfigurator
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( ErrSheetIndexOutOfRange = errors.New("exl: sheet index out of range") ErrHeaderRowIndexOutOfRange = errors.New("exl: header row index out of range") ErrDataStartRowIndexOutOfRange = errors.New("exl: data start row index out of range") ErrNoUnmarshaler = errors.New("no unmarshaler") ErrNoDestinationField = errors.New("no destination field with matching tag") )
var DefaultUnmarshalFuncs = map[reflect.Kind]UnmarshalExcelFunc{ reflect.String: UnmarshalString, reflect.Bool: UnmarshalBool, reflect.Int: UnmarshalInt, reflect.Int8: UnmarshalInt, reflect.Int16: UnmarshalInt, reflect.Int32: UnmarshalInt, reflect.Int64: UnmarshalInt, reflect.Uint: UnmarshalUInt, reflect.Uintptr: UnmarshalUInt, reflect.Uint8: UnmarshalUInt, reflect.Uint16: UnmarshalUInt, reflect.Uint32: UnmarshalUInt, reflect.Uint64: UnmarshalUInt, reflect.Float32: UnmarshalFloat, reflect.Float64: UnmarshalFloat, }
var ErrCannotCastUnmarshaler = errors.New("cannot cast to unmarshaler interface")
ErrCannotCastUnmarshaler is returned in case a field technically implements an unmarshaler interface, but casting to it at runtime failed for some reason.
var ErrNegativeUInt = errors.New("negative integer provided for unsigned field")
var ErrNoRecognizedFormat = errors.New("no recognized format")
var ErrOverflow = errors.New("numeric overflow, number is too large for this field")
Functions ¶
func NewFileFromSlice ¶
func NewFileFromSlice[T WriteConfigurator](ts []T) *xlsx.File
func Read ¶
func Read[T ReadConfigurator](reader io.Reader, filterFunc ...func(t T) (add bool)) ([]T, error)
Read io.Reader each row bind to `T`
func ReadBinary ¶
func ReadBinary[T ReadConfigurator](bytes []byte, filterFunc ...func(t T) (add bool)) ([]T, error)
ReadBinary each row bind to `T`
func ReadFile ¶
func ReadFile[T ReadConfigurator](file string, filterFunc ...func(t T) (add bool)) ([]T, error)
ReadFile each row bind to `T`
func UnmarshalBool ¶
func UnmarshalBool(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalExcelUnmarshaler ¶
func UnmarshalExcelUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalFloat ¶
func UnmarshalFloat(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalInt ¶
func UnmarshalInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalString ¶
func UnmarshalString(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalTextUnmarshaler ¶
func UnmarshalTextUnmarshaler(destField reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalTime ¶
func UnmarshalTime(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func UnmarshalUInt ¶
func UnmarshalUInt(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func WriteExcel ¶
WriteExcel defines write [][]string to excel
params: file, excel file pull path
params: data, write data to excel
func WriteExcelTo ¶
WriteExcelTo defines write [][]string to excel
params: w, the dist writer
params: data, write data to excel
func WriteFile ¶
func WriteFile[T WriteConfigurator](file string, ts []T) error
WriteFile defines write []T to excel file
params: file,excel file full path
params: typed parameter T, must be implements exl.Bind
Types ¶
type ContentError ¶
type ContentError struct { FieldErrors []FieldError LimitReached bool }
func (ContentError) Unwrap ¶
func (e ContentError) Unwrap() []error
Unwrap Error implements the anonymous unwrap interface used by errors.Unwrap and others.
type ExcelUnmarshaler ¶
type ExcelUnmarshaler interface {
UnmarshalExcel(cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
}
type FieldError ¶
type FieldError struct { RowIndex int // 0-based row index. Printed as 1-based row number in error text. ColumnIndex int // 0-based column index. ColumnHeader string Err error }
func (FieldError) Unwrap ¶
func (e FieldError) Unwrap() error
Unwrap Error implements the anonymous unwrap interface used by errors.Unwrap and others.
type ReadConfig ¶
type ReadConfig struct { // The tag name to use when looking for fields in the target struct. // Defaults to "excel". TagName string // The index of the worksheet to be read. // Defaults to 0, the first worksheet. SheetIndex int // The row index at which the column headers are read from. // Zero-based, defaults to 0. HeaderRowIndex int // Start the data reading at this row. // The header row counts as row. // Zero-based, defaults to 1. DataStartRowIndex int // Configure the default string unmarshaler to trim space after reading a cell. // Does not impact any other default unmarshaler, // but is available to custom unmarshalers via ExcelUnmarshalParameters.TrimSpace. // Defaults to false. TrimSpace bool // Fallback date formats for date parsing. // If an Excel cell is to be unmarshalled into a date, // and that cell is either not formatted as Date or contains raw text // (which can happen if Excel does not correctly recognize the date format) // then these formats are used in the order specified to try and parse // the raw cell value into a date. // There are no fallback formats configured by default. FallbackDateFormats []string // Skip reading columns for which no target field is found. // Defaults to true. SkipUnknownColumns bool // Skip reading columns, if there is a target field, // but the target type is unsupported // or caused an error when determining the unmarshaler to use. // Defaults to false. SkipUnknownTypes bool // Configure how errors during unmarshalling are handled. // Unmarshalling errors are e.g. invalid number formats in the cell, // date parsing with invalid input, // or attempting to unmarshal non-numeric text into a numeric field. // Defaults to UnmarshalErrorAbort. UnmarshalErrorHandling UnmarshalErrorHandling // If UnmarshalErrorHandling is configured as UnmarshalErrorCollect, // this option limits the number of errors which are collected before // parsing is aborted. // Configure a limit of 0 to collect all errors, without upper limit. // Defaults to 10. MaxUnmarshalErrors uint64 // Parse data from key. DropListMap map[string][]struct { Key string Value string } // Set pointer struct field to nil when read empty string. PointerCanNil bool }
type ReadConfigurator ¶
type ReadConfigurator interface{ ReadConfigure(rc *ReadConfig) }
type UnmarshalErrorHandling ¶
type UnmarshalErrorHandling uint8
const ( // UnmarshalErrorIgnore // Ignore any errors during unmarshalling UnmarshalErrorIgnore UnmarshalErrorHandling = iota // UnmarshalErrorAbort // Abort reading when encountering the first unmarshalling error UnmarshalErrorAbort // UnmarshalErrorCollect // Collect unmarshalling errors up to a limit, but continue reading. // Collected errors are returned as one error at the end, of type UnmarshalErrorCollect )
type UnmarshalExcelFunc ¶
type UnmarshalExcelFunc func(destValue reflect.Value, cell *xlsx.Cell, params *ExcelUnmarshalParameters) error
func GetUnmarshalFunc ¶
func GetUnmarshalFunc(destField reflect.Value) UnmarshalExcelFunc
type WriteConfig ¶
type WriteConfig struct { SheetName string TagName string // Skip when struct field have NOT matched tagName. SkipNoTag bool // Skip when struct field is a nil pointer. SkipNilPointer bool // Set dropList and write value which is transformed from key. DropListMap map[string][]struct { Key string Value string } // Transform TRUE/FALSE to Chinese 是/否. ChineseBool bool WriteTimeFmt string }
type WriteConfigurator ¶
type WriteConfigurator interface{ WriteConfigure(wc *WriteConfig) }
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer define a writer for exl
func NewWriter ¶
func NewWriter(options ...xlsx.FileOption) *Writer
NewWriter returns new exl writer