From 62cdd3570ae2f07ba6d688c16dcf7cfd11ab7af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 19 Nov 2021 15:40:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9F=BA=E7=A1=80=E7=9A=84?= =?UTF-8?q?=E5=88=9B=E5=BB=BAexcel=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + excel/create.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++++ excel/define.go | 43 ++++++++++++++++++++++++++ go.mod | 3 +- go.sum | 8 ++--- 5 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 excel/create.go diff --git a/.gitignore b/.gitignore index a6b542a..b941df8 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ *.dll *.so *.dylib +*.xlsx # Test binary, built with `go test -c` *.test diff --git a/excel/create.go b/excel/create.go new file mode 100644 index 0000000..3e0497c --- /dev/null +++ b/excel/create.go @@ -0,0 +1,82 @@ +// Package excel... +// +// Description : excel... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-11-19 12:25 下午 +package excel + +import ( + "fmt" + + "github.com/xuri/excelize/v2" +) + +// NewExcel 获取excel实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:28 下午 2021/11/19 +func NewExcel() *Create { + return &Create{ + fileHandler: excelize.NewFile(), + } +} + +// Create 创建excel +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:27 下午 2021/11/19 +type Create struct { + // fileHandler excel文件处理句柄 + fileHandler *excelize.File +} + +// GenerateSheet 生成sheet +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:43 下午 2021/11/19 +func (c *Create) GenerateSheet(sheetList []SheetData) error { + for _, sheet := range sheetList { + sheetIndex := c.fileHandler.NewSheet(sheet.Name) + if sheet.IsDefault { + // 设置活跃 + c.fileHandler.SetActiveSheet(sheetIndex) + } + for lineIdx, colList := range sheet.Data { + for colIdx, col := range colList { + position := c.getColPosition(colIdx, lineIdx) + if err := c.fileHandler.SetCellValue(sheet.Name, position, col); nil != err { + return err + } + } + } + } + return nil +} + +// Save 保存文件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2:04 下午 2021/11/19 +func (c *Create) Save(fullFilePath string) error { + return c.fileHandler.SaveAs(fullFilePath) +} + +// getColPosition 获取列名 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 3:14 下午 2021/11/19 +func (c *Create) getColPosition(colIndex int, dataLineIndex int) string { + realIndex := colIndex % 26 + first := colIndex / 26 + if first == 0 { + return fmt.Sprintf("%s%d", WordMap[realIndex], dataLineIndex+1) + } + return fmt.Sprintf("%s%s%d", WordMap[first], WordMap[realIndex], dataLineIndex+1) +} diff --git a/excel/define.go b/excel/define.go index 68f30ad..40c9bfe 100644 --- a/excel/define.go +++ b/excel/define.go @@ -16,3 +16,46 @@ type ReadResult struct { Error error // 异常信息 Result map[string][]map[string]interface{} // 查询结果 sheet => dataList } + +// SheetData ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:35 下午 2021/11/19 +type SheetData struct { + IsDefault bool // 是否默认工作表 + Name string // sheet 名称 + Data [][]interface{} // 数据列表 +} + +var ( + // WordMap 单元格序号映射 + WordMap = map[int]string{ + 0: "A", + 1: "B", + 2: "C", + 3: "D", + 4: "E", + 5: "F", + 6: "G", + 7: "H", + 8: "I", + 9: "J", + 10: "K", + 11: "L", + 12: "M", + 13: "N", + 14: "O", + 15: "P", + 16: "Q", + 17: "R", + 18: "S", + 19: "T", + 20: "U", + 21: "V", + 22: "W", + 23: "X", + 24: "Y", + 25: "Z", + } +) diff --git a/go.mod b/go.mod index f3d373d..51a1e8d 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/spaolacci/murmur3 v1.1.0 github.com/stretchr/testify v1.7.0 github.com/tidwall/gjson v1.9.0 + github.com/xuri/excelize/v2 v2.4.1 github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 go.uber.org/zap v1.19.1 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df @@ -23,7 +24,6 @@ require ( ) require ( - github.com/360EntSecGroup-Skylar/excelize v1.4.1 // indirect github.com/StackExchange/wmi v1.2.1 // indirect github.com/cespare/xxhash/v2 v2.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect @@ -67,7 +67,6 @@ require ( github.com/tklauser/numcpus v0.3.0 // indirect github.com/ugorji/go/codec v1.1.7 // indirect github.com/xuri/efp v0.0.0-20210322160811-ab561f5b45e3 // indirect - github.com/xuri/excelize/v2 v2.4.1 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect diff --git a/go.sum b/go.sum index e42f550..28ae40c 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/360EntSecGroup-Skylar/excelize v1.4.1 h1:l55mJb6rkkaUzOpSsgEeKYtS6/0gHwBYyfo5Jcjv/Ks= -github.com/360EntSecGroup-Skylar/excelize v1.4.1/go.mod h1:vnax29X2usfl7HHkBrX5EvSCJcmH3dT9luvxzu8iGAE= github.com/Shopify/sarama v1.29.1 h1:wBAacXbYVLmWieEA/0X/JagDdCZ8NVFOfS6l6+2u5S0= github.com/Shopify/sarama v1.29.1/go.mod h1:mdtqvCSg8JOxk8PmpTNGyo6wzd4BMm4QXSfDnTXmgkE= github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc= @@ -156,7 +154,6 @@ github.com/shirou/gopsutil v3.21.9+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.3-0.20181224173747-660f15d67dbb/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= @@ -200,11 +197,11 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 h1:/pEO3GD/ABYAjuakUS6xSEmmlyVS4kxBNkeA9tLJiTI= golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk= golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -220,9 +217,9 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= -golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q= golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4 h1:DZshvxDdVoeKIbudAdFEKi+f70l51luSy/7b76ibTY0= golang.org/x/net v0.0.0-20211118161319-6a13c67c3ce4/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -253,7 +250,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=