index.go
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package exceltool
import (
"errors"
"fmt"
"math/rand"
"time"
excelize "github.com/360EntSecGroup-Skylar/excelize/v2"
)
type ExcelHead struct {
Name string
Key string
}
//ExcelMaker 构建excel文档
type ExcelMaker struct {
Xlsx *excelize.File
fileName string
header []ExcelHead
}
//NewExcelMaker ....
func NewExcelMaker() *ExcelMaker {
return &ExcelMaker{
Xlsx: excelize.NewFile(),
}
}
func (e *ExcelMaker) SetListHead(h []ExcelHead) {
e.header = h
}
func (e *ExcelMaker) SetFileName(n string) {
e.fileName = n
}
func (e *ExcelMaker) GetFileName() string {
return e.fileName
}
//MakeListExcel 根据列表形式的数据创建excel文档
//@sourData []map[string]string; 原始数据,要输入excel文档的数据
func (e *ExcelMaker) MakeListExcel(sourData []map[string]string) (err error) {
if len(e.header) == 0 {
return errors.New("xlsHeader 数据格式错误")
}
headEn := []string{} //数据字段英文名
headCn := []string{} //excel字段中文描述
alphaSlice := []string{} //excel列字母索引
for key, val := range e.header {
headEn = append(headEn, val.Key)
headCn = append(headCn, val.Name)
alpha, _ := excelize.ColumnNumberToName(key + 1)
// alpha := ToAlphaString(key)
alphaSlice = append(alphaSlice, alpha)
}
//设置excel文档第一行的字段中文描述
for index := range headCn {
//索引转列名,索引从0开始
cellAlpha := fmt.Sprintf("%s%d", alphaSlice[index], 1) // 单元格行坐标从1开始,如:a1,指第一行a列。
e.Xlsx.SetCellStr("Sheet1", cellAlpha, headCn[index])
}
//从excel第二行开始设置实际数据的值
for key1 := range sourData {
for i := 0; i < len(headEn); i++ {
cellAlpha := fmt.Sprintf("%s%d", alphaSlice[i], key1+2) // 单元格行坐标从1开始,如:a1,指第一行a列。
e.Xlsx.SetCellStr("Sheet1", cellAlpha, sourData[key1][headEn[i]])
}
}
e.SetFileName(GetRandomString(8) + ".xlsx")
return nil
}
//GetRandomString 生成随机字符串
func GetRandomString(lenght int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < lenght; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
func ToAlphaString(value int) string {
if value < 0 {
return ""
}
var ans string
i := value + 1
for i > 0 {
ans = string((i-1)%26+65) + ans
i = (i - 1) / 26
}
return ans
}