71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
// Package excel -----------------------------
|
|
// @file : templateInter_test.go
|
|
// @author : JJXu
|
|
// @contact : wavingbear@163.com
|
|
// @time : 2022/7/23 15:59
|
|
// -------------------------------------------
|
|
package example
|
|
|
|
import (
|
|
"fmt"
|
|
"gingogo2/utils/excel"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
//定义sheet表结构
|
|
type sheet1Define struct {
|
|
Xid int `json:"xid" form:"xid" db:"column:xid;comment: "`
|
|
Name string `json:"name" form:"name" db:"column:name;comment: "`
|
|
Age int `json:"age" form:"age" db:"column:age;comment: "`
|
|
}
|
|
|
|
func TestExcelTpl_WriteToExcel(t *testing.T) {
|
|
var sheet1Data = []sheet1Define{
|
|
{1, "张三", 16},
|
|
{2, "黑猫警长", 18},
|
|
}
|
|
|
|
var sheet1 = excel.NewSheet("Sheet1", sheet1Data)
|
|
//var suffixFunc = func() string { return fmt.Sprintf("%v", time.Now().Unix()) }
|
|
exCreator, err := excel.NewExcelTemplate("demo.xlsx", "./", "./demo.xlsx", sheet1)
|
|
if err != nil {
|
|
t.Log(err)
|
|
}
|
|
exCreator.UseOption(func(excel *excel.Excel) {
|
|
ext := filepath.Ext(excel.SaveName)
|
|
name := strings.Split(excel.SaveName, ext)[0]
|
|
excel.SaveName = fmt.Sprintf("%s_%v%s", name, time.Now().Unix(), ext)
|
|
})
|
|
path, name, err := exCreator.WriteToFile()
|
|
if err != nil {
|
|
t.Log(err)
|
|
} else {
|
|
t.Log(path, name)
|
|
}
|
|
}
|
|
|
|
func TestReadExcel(t *testing.T) {
|
|
var file = "demo_1671435052.xlsx"
|
|
ex := excel.Excel{OriginFilePath: file}
|
|
var datas []sheet1Define
|
|
err := ex.ReadSheetData("Sheet1", func(rowIndex int, rows []string) {
|
|
if rowIndex == 0 {
|
|
//跳过首行
|
|
return
|
|
}
|
|
datas = append(datas, sheet1Define{
|
|
Xid: excel.Int[int](rows[0]),
|
|
Name: rows[1],
|
|
Age: excel.Int[int](rows[2]),
|
|
})
|
|
})
|
|
if err != nil {
|
|
t.Error(err.Error())
|
|
}
|
|
fmt.Println(datas)
|
|
|
|
}
|