excelorm

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 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

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 生成excel并保存为 bytes.Buffer, 用法同 WriteExcelSaveAs

func WriteExcelSaveAs

func WriteExcelSaveAs(fileName string, sheetModels []SheetModel, opts ...Option) error

WriteExcelSaveAs 生成excel文件并保存到本地 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 当字段类型为bool时,true和false的展示内容

func WithFloatFmt

func WithFloatFmt(fmt byte) Option

func WithFloatPrecision

func WithFloatPrecision(precision int) Option

func WithHeadless

func WithHeadless() Option

WithHeadless 不显示表头

func WithIfNullValue

func WithIfNullValue(value string) Option

WithIfNullValue 当数据为nil时展示内容

func WithIntegerAsString

func WithIntegerAsString() Option

WithIntegerAsString int类型的字段是否以字符串形式显示(避免excel自动转为科学计数法)

func WithSheetHeaders

func WithSheetHeaders(headers ...SheetModel) Option

WithSheetHeaders 当没有数据时,默认也要展示表头

func WithTimeFormatLayout

func WithTimeFormatLayout(layout string) Option

WithTimeFormatLayout 时间类型的格式化版图

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