xlsxp

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2021 License: MIT Imports: 8 Imported by: 0

README

xlsxp test

Easier imports and exports with xlsx

Description

The excel tag defines what to export from struct to XLXS.

  1. header represents the header to display.
  2. index represents the order of data.
  3. format represents a method of formatting.
  4. default represents what to display when the data is empty.

Example

Export xlsx
type Test struct {
    UserName string `json:"user_name" excel:"header(Student);index(0);default(---)"`
	CompletePercent    float64 `json:"complete_percent" excel:"header(Complete Percent);index(1);format(percent)"`
}

datas := make([]Test, 0)
datas = append(datas, Test{
    UserName:        "A",
    CompletePercent: 15.3,
})

datas = append(datas, Test{
    UserName:        "B",
    CompletePercent: 17.558,
})

file := xlsx.NewFile()
err := xlsxp.ExportExcel(file, "sheet1", datas)
...
err := xlsxp.ExportExcel(file, "sheet2", datas)
...
file.Save(filepath)
...

Import xlsx
type Test struct {
    CompletePercent float64 `json:"complete_percent" excel:"index(1);parse(percent)"`
    UserName        string  `json:"user_name" excel:"index(0);"`
}

targetDatas := make([]Test, 0)
err = xlsxp.ImportExcel(xlsxBuf.Bytes(), "sheet1", &targetDatas)
Notice

Not support huge amounts of data. See:
xlsx/issues/539
xlsx/blob/master/file.go#L169
xlsx/blob/master/file.go#L410

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExportExcel

func ExportExcel(file *xlsx.File, sheetName string, vals interface{}, cformats ...CustomFormat) (err error)
导出 excel

sheetName: 表格名称

vals: 已定义 excel:"xx" 字段的结构数组数据

cformats: 自定义格式化函数

示例:

type Test struct {
	Test     string `json:"test" excel:"header(测试);index(1)"`
	UserName string `json:"user_name" excel:"header(学员);index(0)"`
}

| 学员 | 测试 | | ----------- | ----------- | | 学员数据1 | 测试数据2 | | 学员数据2 | 测试数据2 |

func ImportExcel

func ImportExcel(data []byte, sheetName string, targets interface{}, cparses ...CustomParse) (err error)

导入excel

Types

type CustomFormat

type CustomFormat struct {
	Name   string
	Format FormatFn
}

type CustomParse

type CustomParse struct {
	Name  string
	Parse ParseFn
}

type FormatFn

type FormatFn = func(fieldVal interface{}) string

格式化方法

type FormatFnMap

type FormatFnMap = map[string]FormatFn

func NewFormatFn

func NewFormatFn(cformats ...CustomFormat) FormatFnMap

自定义格式化方法

type ParseFn

type ParseFn = func(cellVal string) string

解析方法

type ParseFnMap

type ParseFnMap = map[string]ParseFn

func NewParseFn

func NewParseFn(cparses ...CustomParse) ParseFnMap

自定义解析方法

Jump to

Keyboard shortcuts

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