excelorm

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

excelorm

Code Coverage Build Status Github Release GitHub Hits

A easier use excel file create tool for golang

install

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

Quick Start

  • define a struct with excel_header tag and implement 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 some 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 to 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 can see the result 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

  • support multi-sheets by define more structs

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 SheetModel interface
func (u Foo) SheetName() string {
	return "foo sheet name"
}
//append data to 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 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 that code execute, you will get `foo.xlsx` file with named `foo sheet name`,
// It's content like next:
+-------------------------------------------------------+
| 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 |                   - |
+-------------------------------------------------------+
// Multi-sheets
// define more structs which implement SheetModel interface
// then construct any of their objects to append to sheetModels
// different sheetModel better have different sheet name to avoid confusion
// rows ordered in Excel file is the same as 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