2022-11-24 23:37:34 +08:00
|
|
|
// Package excel
|
2022-02-25 17:11:17 +08:00
|
|
|
// @Link https://github.com/bufanyun/hotgo
|
2023-02-23 17:53:04 +08:00
|
|
|
// @Copyright Copyright (c) 2023 HotGo CLI
|
2022-11-24 23:37:34 +08:00
|
|
|
// @Author Ms <133814250@qq.com>
|
2022-02-25 17:11:17 +08:00
|
|
|
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
2022-11-24 23:37:34 +08:00
|
|
|
package excel
|
2022-02-25 17:11:17 +08:00
|
|
|
|
|
|
|
import (
|
2023-01-18 16:23:39 +08:00
|
|
|
"context"
|
2022-02-25 17:11:17 +08:00
|
|
|
"fmt"
|
2023-01-18 16:23:39 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gcode"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2022-02-25 17:11:17 +08:00
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
2023-01-18 16:23:39 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
2022-02-25 17:11:17 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
"github.com/xuri/excelize/v2"
|
2023-01-18 16:23:39 +08:00
|
|
|
"hotgo/internal/library/contexts"
|
|
|
|
"hotgo/internal/model"
|
2022-02-25 17:11:17 +08:00
|
|
|
"net/url"
|
|
|
|
"reflect"
|
2023-01-18 16:23:39 +08:00
|
|
|
"time"
|
2022-02-25 17:11:17 +08:00
|
|
|
)
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
var (
|
|
|
|
// 单元格表头
|
|
|
|
char = []string{"", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
|
|
|
|
// 默认行样式
|
|
|
|
defaultRowStyle = `{"font":{"color":"#666666","size":13,"family":"arial"},"alignment":{"vertical":"center","horizontal":"center"}}`
|
|
|
|
)
|
2022-02-25 17:11:17 +08:00
|
|
|
|
2023-02-26 14:18:22 +08:00
|
|
|
// ExportByStructs 导出切片结构体到excel表格
|
2023-01-18 16:23:39 +08:00
|
|
|
func ExportByStructs(ctx context.Context, tags []string, list interface{}, fileName string, sheetName string) (err error) {
|
2022-02-25 17:11:17 +08:00
|
|
|
f := excelize.NewFile()
|
|
|
|
f.SetSheetName("Sheet1", sheetName)
|
2023-01-18 16:23:39 +08:00
|
|
|
_ = f.SetRowHeight("Sheet1", 1, 30)
|
2022-02-25 17:11:17 +08:00
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
rowStyleID, _ := f.NewStyle(defaultRowStyle)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-05-30 12:09:40 +08:00
|
|
|
_ = f.SetSheetRow(sheetName, "A1", &tags)
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
|
|
var (
|
|
|
|
length = len(tags)
|
|
|
|
headStyle = letter(length)
|
|
|
|
lastRow string
|
|
|
|
widthRow string
|
|
|
|
)
|
|
|
|
|
2022-02-25 17:11:17 +08:00
|
|
|
for k, v := range headStyle {
|
|
|
|
if k == length-1 {
|
|
|
|
lastRow = fmt.Sprintf("%s1", v)
|
|
|
|
widthRow = v
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 16:23:39 +08:00
|
|
|
if err = f.SetColWidth(sheetName, "A", widthRow, 30); err != nil {
|
2022-02-25 17:11:17 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
var rowNum = 1
|
|
|
|
for _, v := range gconv.Interfaces(list) {
|
2022-02-25 17:11:17 +08:00
|
|
|
t := reflect.TypeOf(v)
|
|
|
|
value := reflect.ValueOf(v)
|
|
|
|
row := make([]interface{}, 0)
|
|
|
|
for l := 0; l < t.NumField(); l++ {
|
|
|
|
val := value.Field(l).Interface()
|
|
|
|
row = append(row, val)
|
|
|
|
}
|
|
|
|
rowNum++
|
2023-01-18 16:23:39 +08:00
|
|
|
if err = f.SetSheetRow(sheetName, "A"+gconv.String(rowNum), &row); err != nil {
|
2023-05-30 12:09:40 +08:00
|
|
|
return
|
2023-01-18 16:23:39 +08:00
|
|
|
}
|
2023-05-30 12:09:40 +08:00
|
|
|
if err = f.SetCellStyle(sheetName, fmt.Sprintf("A%d", rowNum), lastRow, rowStyleID); err != nil {
|
|
|
|
return
|
2022-02-25 17:11:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
// 强转类型
|
|
|
|
writer := ghttp.RequestFromCtx(ctx).Response.Writer
|
|
|
|
w, ok := interface{}(writer).(*ghttp.ResponseWriter)
|
|
|
|
if !ok {
|
|
|
|
return gerror.New("Response.Writer uninitialized!")
|
|
|
|
}
|
2022-02-25 17:11:17 +08:00
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
2023-01-18 16:23:39 +08:00
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s.xlsx", url.QueryEscape(fileName)))
|
2022-02-25 17:11:17 +08:00
|
|
|
w.Header().Set("Content-Transfer-Encoding", "binary")
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
|
|
|
|
|
2023-05-30 12:09:40 +08:00
|
|
|
if err = f.Write(w); err != nil {
|
|
|
|
return
|
2022-02-25 17:11:17 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
// 加入到上下文
|
|
|
|
contexts.SetResponse(ctx, &model.Response{
|
|
|
|
Code: gcode.CodeOK.Code(),
|
|
|
|
Message: "export successfully!",
|
|
|
|
Timestamp: time.Now().Unix(),
|
|
|
|
TraceID: gctx.CtxId(ctx),
|
|
|
|
})
|
2023-05-30 12:09:40 +08:00
|
|
|
return
|
2022-02-25 17:11:17 +08:00
|
|
|
}
|
|
|
|
|
2023-01-18 16:23:39 +08:00
|
|
|
// letter 生成完整的表头
|
2022-11-24 23:37:34 +08:00
|
|
|
func letter(length int) []string {
|
2022-02-25 17:11:17 +08:00
|
|
|
var str []string
|
|
|
|
for i := 0; i < length; i++ {
|
2023-01-18 16:23:39 +08:00
|
|
|
str = append(str, numToChars(i))
|
2022-02-25 17:11:17 +08:00
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
2023-01-18 16:23:39 +08:00
|
|
|
|
|
|
|
// numToChars 将数字转换为具体的表格表头名称
|
|
|
|
func numToChars(num int) string {
|
|
|
|
var cols string
|
|
|
|
v := num
|
|
|
|
for v > 0 {
|
|
|
|
k := v % 26
|
|
|
|
if k == 0 {
|
|
|
|
k = 26
|
|
|
|
}
|
|
|
|
v = (v - k) / 26
|
|
|
|
cols = char[k] + cols
|
|
|
|
}
|
|
|
|
return cols
|
|
|
|
}
|