Documentation
¶
Index ¶
- func WriteExcelAsBytesBuffer(sheetModels []SheetModel, opts ...Option) (*bytes.Buffer, error)
- func WriteExcelSaveAs(fileName string, sheetModels []SheetModel, opts ...Option) error
- type Option
- func WithBoolValueAs(trueValue, falseValue string) Option
- func WithFloatFmt(fmt byte) Option
- func WithFloatPrecision(precision int) Option
- func WithHeadless() Option
- func WithIfNullValue(value string) Option
- func WithIntegerAsString() Option
- func WithSheetHeaders(headers ...SheetModel) Option
- func WithTimeFormatLayout(layout string) Option
- type SheetModel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteExcelAsBytesBuffer ¶
func WriteExcelAsBytesBuffer(sheetModels []SheetModel, opts ...Option) (*bytes.Buffer, error)
WriteExcelAsBytesBuffer creates an Excel file and writes it to a bytes.Buffer. Usage is the same as WriteExcelSaveAs.
func WriteExcelSaveAs ¶
func WriteExcelSaveAs(fileName string, sheetModels []SheetModel, opts ...Option) error
WriteExcelSaveAs creates an Excel file and saves it to local storage. Example usage:
// Define a struct.
type Foo struct {
ID int64 `excel_header:"id"`
Name string `excel_header:"name"`
CreatedAt time.Time `excel_header:"created_at"`
DeletedAt *time.Time `excel_header:"deleted_at"`
}
// Implement the SheetModel interface.
func (u Foo) SheetName() string {
return "foo sheet name"
}
// Append data to the Excel file.
bar1DeletedAt := time.Date(2024, 1, 3, 15, 4, 5, 0, time.Local)
sheetModels := []excelorm.SheetModel{
Foo{
ID: 1,
Name: "Bar1",
CreatedAt: time.Date(2024, 1, 2, 15, 4, 5, 0, time.Local),
DeletedAt: &bar1DeletedAt,
},
Foo{
ID: 2,
Name: "Bar2",
CreatedAt: time.Date(2024, 1, 2, 15, 4, 5, 0, time.Local),
},
}
// Build and save the Excel file.
if err := excelorm.WriteExcelSaveAs("foo.xlsx", sheetModels,
excelorm.WithTimeFormatLayout("2006/01/02 15:04:05"),
excelorm.WithIfNullValue("-"),
); err != nil {
log.Fatal(err)
}
// After this code runs, you will get a `foo.xlsx` file with a sheet named `foo sheet name`.
// Its content looks like this:
+-------------------------------------------------------+
| id | name | created_at | deleted_at |
+-------------------------------------------------------+
| 1 | Bar1 | 2024/01/02 15:04:05 | 2024/01/03 15:04:05 |
| 2 | Bar2 | 2024/01/02 15:04:05 | - |
+-------------------------------------------------------+
// Multiple sheets:
// Define more structs that implement the SheetModel interface,
// then append their objects to sheetModels.
// Different sheet models should use different sheet names to avoid confusion.
// Row order in the Excel file matches the order in sheetModels.
Types ¶
type Option ¶
type Option func(*options)
func WithBoolValueAs ¶
WithBoolValueAs sets display values for boolean true and false fields.
func WithFloatFmt ¶
func WithFloatPrecision ¶
func WithIfNullValue ¶
WithIfNullValue sets the display value when data is nil.
func WithIntegerAsString ¶
func WithIntegerAsString() Option
WithIntegerAsString displays integer fields as strings (to avoid Excel scientific notation).
func WithSheetHeaders ¶
func WithSheetHeaders(headers ...SheetModel) Option
WithSheetHeaders sets default headers to display even when there is no data.
func WithTimeFormatLayout ¶
WithTimeFormatLayout sets the formatting layout used for time values.
type SheetModel ¶
type SheetModel interface {
SheetName() string
}
Click to show internal directories.
Click to hide internal directories.