excelizero

package module
v0.0.0-...-538a430 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2022 License: MIT Imports: 6 Imported by: 0

README

Excelizero

Excelizero is simple encapsulation of Excelize. It is used for insert go objects into excel files.

Example

1. Write Single Object
type User struct {
    Name   string
    Age    int
    Height int
}
u := User{
    Name:   "Jack",
    Age:    11,
    Height: 180,
}

f := excelize.NewFile()
excelizero := NewExcelizero(f)
err := excelizero.WriteStruct("Sheet1", "A1", u)
err = excelizero.SaveAs("test.xlsx")
if err != nil {
    fmt.Println(err)
}
2. Write Multiple Object
type User struct {
Name   string
    Age    int
    Height int
}
u := User{
    Name:   "Jack",
    Age:    11,
    Height: 180,
}
u2 := User{
    Name:   "Jack2",
    Age:    222,
    Height: 190,
}
u3 := &User{
    Name:   "Jack",
    Age:    11,
    Height: 180,
}
u4 := &User{
    Name:   "Jack2",
    Age:    222,
    Height: 190,
}
var l []User
var l2 []*User
l = append(l, u, u2)
l2 = append(l2, u3, u4)


f := excelize.NewFile()
excelizero := NewExcelizero(f)
// insert slice of structs
err1 := excelizero.WriteStructs("Sheet1", "A1", l)
// insert slice of pointers to struct
err2 := excelizero.WriteStructs("Sheet1", "A1", &l)
// insert pointer to slice of structs
err3 := excelizero.WriteStructs("Sheet1", "A1", l2)
// insert pointer to slice of pointers to structs
err4 := excelizero.WriteStructs("Sheet1", "A1", &l2)
err = excelizero.SaveAs("test.xlsx")
if err != nil {
    fmt.Println(err)
}
3. Write Multiple Object With Tag
type User struct {
    Name   string `xlsx:"1"`
    Age    int    `xlsx:"2"`
    Height int    `xlsx:"3"`
}
u := User{
    Name:   "Jack",
    Age:    11,
    Height: 180,
}
u2 := User{
    Name:   "Jack2",
    Age:    222,
    Height: 190,
}
u3 := &User{
    Name:   "Jack",
    Age:    11,
    Height: 180,
}
u4 := &User{
    Name:   "Jack2",
    Age:    222,
    Height: 190,
}
var l []User
var l2 []*User
l = append(l, u, u2)
l2 = append(l2, u3, u4)
f := excelize.NewFile()
excelizero := NewExcelizero(f)
// insert slice of structs
err1 := excelizero.WriteStructs("Sheet1", "A1", l)
// insert slice of pointers to struct
err2 := excelizero.WriteStructs("Sheet1", "A1", &l)
// insert pointer to slice of structs
err3 := excelizero.WriteStructs("Sheet1", "A1", l2)
// insert pointer to slice of pointers to structs
err4 := excelizero.WriteStructs("Sheet1", "A1", &l2)
err = excelizero.SaveAs("test.xlsx")
if err != nil {
fmt.Println(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetColumn

func GetColumn(col string) (string, error)

func GetNextAxis

func GetNextAxis(cur string) (string, error)

A2 -> A3

func GetNextRow

func GetNextRow(from, cur int) string

Types

type Excelizero

type Excelizero struct {
	// contains filtered or unexported fields
}

func NewExcelizero

func NewExcelizero(f *excelize.File) *Excelizero

func (*Excelizero) SaveAs

func (x *Excelizero) SaveAs(name string) error

func (*Excelizero) WriteHeader

func (x *Excelizero) WriteHeader(sheetName string, hs []string) error

func (*Excelizero) WriteStruct

func (x *Excelizero) WriteStruct(sheetName string, axis string, obj interface{}) error

WriteStruct Writes a struct to row f. a pointer to excelizero object sheetName. sheetName to write to, if sheet not exist, return error axis. where to write from obj. struct need to be written to excel

func (*Excelizero) WriteStructWithTag

func (x *Excelizero) WriteStructWithTag(sheetName string, fromRow int, sl interface{}) error

WriteStructWithTag: write structs to excel rows only insert field that has tag: xlsx sheetName. sheetName to write to, if sheet not exist, return error fromRow. no of row where to start to insert sl. slice of struct that need to be written to excel

func (*Excelizero) WriteStructs

func (x *Excelizero) WriteStructs(sheetName string, axis string, sl interface{}) error

WriteStructs Writes structs to rows f. a pointer to excelizero object sheetName. sheetName to write to, if sheet not exist, return error axis. where to write from sl. slice of struct that need to be written to excel

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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