excelorm

package module
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 7 Imported by: 0

README

excelorm

Code Coverage Build Status GitHub Release GitHub Hits

A lightweight and easy-to-use Excel file generation tool for Go.

Installation

 go get -u 'github.com/varushsu/excelorm@latest'

Quick Start

  • Define a struct with excel_header tags and implement the SheetName method.
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"`
}
func (u Foo) SheetName() string {
    return "foo sheet name"
}
  • Construct sample data.
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),
    },
}
  • Write the data to an Excel file.
if err := excelorm.WriteExcelSaveAs("foo.xlsx", sheetModels,
    excelorm.WithTimeFormatLayout("2006/01/02 15:04:05"),
    excelorm.WithIfNullValue("-"), 
); err != nil {
    log.Fatal(err)
}
  • You should see the following output in the file:
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 -

foo.xlsx

  • To support multiple sheets, define more structs that implement SheetName.

Documentation

Index

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

func WithBoolValueAs(trueValue, falseValue string) Option

WithBoolValueAs sets display values for boolean true and false fields.

func WithFloatFmt

func WithFloatFmt(fmt byte) Option

func WithFloatPrecision

func WithFloatPrecision(precision int) Option

func WithHeadless

func WithHeadless() Option

WithHeadless disables writing the header row.

func WithIfNullValue

func WithIfNullValue(value string) Option

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

func WithTimeFormatLayout(layout string) Option

WithTimeFormatLayout sets the formatting layout used for time values.

type SheetModel

type SheetModel interface {
	SheetName() string
}

Jump to

Keyboard shortcuts

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