提交 7084d898 authored 作者: mooncake's avatar mooncake

add excel utils

上级 ff2ee12c
module gitlab.wanzhuangkj.com/tush/xpkg module gitlab.wanzhuangkj.com/tush/xpkg
go 1.23.0 go 1.24.0
toolchain go1.23.9 toolchain go1.24.3
require ( require (
github.com/DATA-DOG/go-sqlmock v1.5.0 github.com/DATA-DOG/go-sqlmock v1.5.0
...@@ -29,7 +29,7 @@ require ( ...@@ -29,7 +29,7 @@ require (
github.com/shirou/gopsutil/v3 v3.23.8 github.com/shirou/gopsutil/v3 v3.23.8
github.com/spf13/cast v1.5.0 github.com/spf13/cast v1.5.0
github.com/spf13/viper v1.12.0 github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.11.1
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.2 github.com/swaggo/gin-swagger v1.5.2
github.com/swaggo/swag v1.8.12 github.com/swaggo/swag v1.8.12
...@@ -43,8 +43,8 @@ require ( ...@@ -43,8 +43,8 @@ require (
go.opentelemetry.io/otel/sdk v1.24.0 go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/trace v1.24.0 go.opentelemetry.io/otel/trace v1.24.0
go.uber.org/zap v1.24.0 go.uber.org/zap v1.24.0
golang.org/x/crypto v0.40.0 golang.org/x/crypto v0.43.0
golang.org/x/sync v0.16.0 golang.org/x/sync v0.17.0
google.golang.org/grpc v1.67.1 google.golang.org/grpc v1.67.1
google.golang.org/protobuf v1.36.9 google.golang.org/protobuf v1.36.9
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
...@@ -62,6 +62,8 @@ require ( ...@@ -62,6 +62,8 @@ require (
github.com/hashicorp/consul/api v1.12.0 github.com/hashicorp/consul/api v1.12.0
github.com/mojocn/base64Captcha v1.3.8 github.com/mojocn/base64Captcha v1.3.8
github.com/nacos-group/nacos-sdk-go v1.1.6 github.com/nacos-group/nacos-sdk-go v1.1.6
github.com/shopspring/decimal v1.4.0
github.com/xuri/excelize/v2 v2.10.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
) )
...@@ -86,8 +88,13 @@ require ( ...@@ -86,8 +88,13 @@ require (
github.com/kr/pretty v0.3.1 // indirect github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.4 // indirect
github.com/tiendc/go-deepcopy v1.7.1 // indirect
github.com/xuri/efp v0.0.1 // indirect
github.com/xuri/nfp v0.0.2-0.20250530014748-2ddeb826f9a9 // indirect
go.uber.org/goleak v1.3.0 // indirect go.uber.org/goleak v1.3.0 // indirect
golang.org/x/image v0.23.0 // indirect golang.org/x/image v0.25.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
) )
...@@ -164,11 +171,11 @@ require ( ...@@ -164,11 +171,11 @@ require (
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.20.0 // indirect golang.org/x/arch v0.20.0 // indirect
golang.org/x/net v0.42.0 // indirect golang.org/x/net v0.46.0 // indirect
golang.org/x/sys v0.35.0 // indirect golang.org/x/sys v0.37.0 // indirect
golang.org/x/text v0.27.0 // indirect golang.org/x/text v0.30.0 // indirect
golang.org/x/time v0.1.0 // indirect golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.34.0 // indirect golang.org/x/tools v0.37.0 // indirect
google.golang.org/appengine v1.6.8 // indirect google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect
......
差异被折叠。
No preview for this file type
package excelutils
import (
"bytes"
"fmt"
"reflect"
"github.com/xuri/excelize/v2"
)
type Excel struct {
f *excelize.File
e error
sheetCount int
}
func New() *Excel {
f := excelize.NewFile()
return &Excel{
f: f,
}
}
func (x *Excel) Close() *Excel {
x.e = x.f.Close()
return x
}
func (x *Excel) Err() error {
x.f.Close()
return x.e
}
func (x *Excel) WriteSheet(sheetName string, data any) *Excel {
if x.e != nil {
return x
}
if x.sheetCount == 0 {
x.f.SetSheetName("Sheet1", sheetName)
} else {
x.f.NewSheet(sheetName)
}
x.sheetCount++
v := reflect.ValueOf(data)
if v.Kind() != reflect.Slice {
x.e = fmt.Errorf("excel data must be a slice")
return x
}
t := v.Index(0).Type()
headers := make([]interface{}, 0, t.NumField())
for j := 0; j < t.NumField(); j++ {
name := t.Field(j).Tag.Get("xls")
if name == "" {
name = t.Field(j).Name
}
headers = append(headers, name)
}
if err := x.f.SetSheetRow(sheetName, "A1", &headers); err != nil {
x.e = err
return x
}
for i := 0; i < v.Len(); i++ {
item := make([]interface{}, 0, t.NumField())
for j := 0; j < t.NumField(); j++ {
item = append(item, v.Index(i).Field(j).Interface())
}
if err := x.f.SetSheetRow(sheetName, fmt.Sprintf("A%d", i+2), &item); err != nil {
x.e = err
return x
}
}
return x
}
func (x *Excel) WriteBuf(buf *bytes.Buffer) *Excel {
if x.e != nil {
return x
}
x.e = x.f.Write(buf)
return x
}
func (x *Excel) SaveAsFile(filepath string) *Excel {
if x.e != nil {
return x
}
x.e = x.f.SaveAs(filepath)
return x
}
package excelutils_test
import (
"bytes"
"os"
"testing"
"github.com/shopspring/decimal"
"github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/excelutils"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/xtime"
)
func TestNew(t *testing.T) {
type Payment struct {
OrderNO string `xls:"订单编号"`
Amount decimal.Decimal `xls:"金额"`
CreatedAt xtime.DateTime `xls:"创建时间"`
}
d := make([]Payment, 0)
for i := 0; i < 100; i++ {
d = append(d,
Payment{
OrderNO: "OD000" + cast.ToString(i+1),
Amount: decimal.NewFromInt(int64((i + 1) * 10)),
CreatedAt: xtime.Now(),
},
)
}
buf := bytes.Buffer{}
if e := excelutils.New().WriteSheet("交易明细1", d).WriteSheet("交易明细2", d).WriteSheet("交易明细3", d).WriteBuf(&buf).Err(); e != nil {
t.Error(e)
}
f, e := os.Create("./1.xlsx")
if e != nil {
t.Error(e.Error())
}
defer f.Close()
f.Write(buf.Bytes())
t.Log("OK")
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论