-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
176 lines (159 loc) · 4.88 KB
/
writer.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Package excel -----------------------------
// @file : writer.go
// @author : JJXu
// @contact : [email protected]
// @time : 2023/9/1 14:11
// -------------------------------------------
package exportToExcel
import (
"errors"
"reflect"
)
var writers writerList
type writerList []IDataWriter
func (w writerList) FieldSort(sheetObj *Sheet) []string {
if sheetObj.data == nil {
for _, dw := range w {
if fs := dw.FieldSort(sheetObj.baseDataType); fs != nil {
return fs
}
}
} else {
for _, dw := range w {
if dw.Supported(sheetObj.data) {
return dw.FieldSort(sheetObj.baseDataType)
}
}
}
return nil
}
func (w writerList) WriteData(sheetObj *Sheet) error {
for _, dw := range w {
if dw.Supported(sheetObj.data) {
return dw.WriteData(sheetObj)
}
}
return errors.New("There is no supported data writer")
}
type IDataWriter interface {
Supported(any) bool //determine whether the data type supports the writer
//Tag() string
WriteData(sheetObj *Sheet) error
FieldSort(baseDataType any) []string //default field sorting method
}
func init() {
writers = writerList{&sliceWriter{}, &structWriter{}}
}
// The registered Data Writer is preferentially used
func RegisterDataWriter(writer ...IDataWriter) {
writers = append(writer, writers...)
}
//======================================================================================================================
// ======================================================================================================================
type sliceWriter struct {
}
func (s sliceWriter) Supported(data any) bool {
dataType := reflect.TypeOf(data)
if dataType.Kind() == reflect.Slice || dataType.Kind() == reflect.Array {
elementType := dataType.Elem()
if elementType.Kind() == reflect.Ptr {
elementType = elementType.Elem()
}
if elementType.Kind() == reflect.Struct {
return true
}
}
return false
}
// , SheetData reflect.Value, SheetDataType reflect.Type, firstRow int
func (s sliceWriter) WriteData(sheetObj *Sheet) error {
var cellNameList = sheetObj.Fields()
var refValue = reflect.ValueOf(sheetObj.Data())
var refType = reflect.TypeOf(sheetObj.baseDataType)
var columnLen = refType.NumField()
dataRowLen := refValue.Len()
for rowIndex := 0; rowIndex < dataRowLen; rowIndex++ {
var dataMap = s.dataToMap(refValue.Index(rowIndex), refType)
for i := 0; i < columnLen-1; i++ {
for column, v := range cellNameList {
var axis = GetCellCoord(sheetObj.firstEmptyRow+rowIndex+1, column+1)
err := sheetObj.file.SetCellValue(sheetObj.SheetName(), axis, dataMap[v])
if err != nil {
return err
}
}
}
}
//set sheet style
dataStyleID, errs := sheetObj.file.NewStyle(sheetObj.dataStyle())
if errs != nil {
return errs
}
maxCol := columnLen
titleLen := sheetObj.Title.colNum
if titleLen > maxCol {
maxCol = titleLen
}
if maxCol == 0 {
maxCol = 1
}
if err := sheetObj.file.SetCellStyle(sheetObj.SheetName(), GetCellCoord(sheetObj.firstEmptyRow+1, 1), GetCellCoord(sheetObj.rowNum, maxCol), dataStyleID); err != nil {
return err
}
//设置默认列宽
//exc.ex.SetColWidth(sheetObj.SheetName(), GetColumnIndex(1), GetColumnIndex(len(sheetObj.SheetHeaders())), 12.0)
return AutoResetCellWidth(sheetObj)
}
func (s sliceWriter) FieldSort(baseDataType any) []string {
refType := reflect.TypeOf(baseDataType)
list, _ := GetJsonFieldList(refType)
return list
}
func (s sliceWriter) dataToMap(sheet reflect.Value, sheetType reflect.Type) (dataMap map[string]any) {
dataMap = DataToMapByJsonTag(sheet, sheetType)
return dataMap
}
type structWriter struct {
}
func (s structWriter) Supported(data any) bool {
dataType := reflect.TypeOf(data)
if dataType.Kind() == reflect.Ptr {
dataType = dataType.Elem()
}
if dataType.Kind() == reflect.Struct {
return true
}
return false
}
func (s structWriter) WriteData(sheetObj *Sheet) error {
var dataType = reflect.TypeOf(sheetObj.baseDataType)
var dataValue = reflect.ValueOf(sheetObj.Data())
var dataMap = DataToMapByJsonTag(dataValue, dataType)
for column, v := range sheetObj.Fields() {
var axis = GetCellCoord(sheetObj.firstEmptyRow+1, column+1)
err := sheetObj.file.SetCellValue(sheetObj.SheetName(), axis, dataMap[v])
if err != nil {
return err
}
}
//register data style
dataStyleID, err := sheetObj.file.NewStyle(sheetObj.dataStyle())
if err != nil {
return err
}
// gets the maximum number of columns
colLen := len(sheetObj.Fields())
if sheetObj.Title.colNum > colLen {
colLen = sheetObj.Title.colNum
}
if err = sheetObj.file.SetCellStyle(sheetObj.SheetName(), GetCellCoord(sheetObj.firstEmptyRow, 1), GetCellCoord(sheetObj.firstEmptyRow+1, colLen), dataStyleID); err != nil {
return err
}
//Set the default column width
return AutoResetCellWidth(sheetObj)
}
func (s structWriter) FieldSort(baseDataType any) []string {
refType := reflect.TypeOf(baseDataType)
list, _ := GetJsonFieldList(refType)
return list
}