forked from menduo/exsoul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
98 lines (83 loc) · 3.09 KB
/
write.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
// Created by @menduo @ 2020/7/14
package exsoul
import (
"github.com/360EntSecGroup-Skylar/excelize/v2"
"io"
)
// NewFile 为写文件而生!
func NewFile() *Exsoul {
esfile := &Exsoul{
eObj: excelize.NewFile(),
sheetRows: nil,
}
return esfile
}
// SetHeader 设置表头
func (e *Exsoul) SetHeader(values *[]interface{}) error {
sheet := e.eObj.GetSheetName(0)
return e.SetRowByColForSheet(sheet, 1, 1, values)
}
// SetHeaderForSheet 设置指定 sheet 的表头
func (e *Exsoul) SetHeaderForSheet(sheet string, values *[]interface{}) error {
return e.SetRowByColForSheet(sheet, 1, 1, values)
}
// SetHeader 设置表头
func (e *Exsoul) SetHeaderByCol(col int, values *[]interface{}) error {
sheet := e.eObj.GetSheetName(0)
return e.SetRowByColForSheet(sheet, col, 1, values)
}
// SetHeaderForSheet 设置指定 sheet 的表头
func (e *Exsoul) SetHeaderByColForSheet(sheet string, col int, values *[]interface{}) error {
return e.SetRowByColForSheet(sheet, col, 1, values)
}
// SetRow 为默认sheet 写入行,lineNum 为行号(不是索引下标),values 为切片指针
func (e *Exsoul) SetRow(lineNum int, values *[]interface{}) error {
sheet := e.eObj.GetSheetName(0)
return e.SetRowByColForSheet(sheet, 1, lineNum, values)
}
// SetRowForSheet 为指定sheet,写入行,lineNum 为行号(不是索引下标),values 为切片指针
func (e *Exsoul) SetRowForSheet(sheet string, lineNum int, values *[]interface{}) error {
return e.SetRowByColForSheet(sheet, 1, lineNum, values)
}
// SetRowByCol 为默认sheet 写入行,lineNum 为行号(不是索引下标),col为每行从第多少列开始,values 为切片指针
func (e *Exsoul) SetRowByCol(col, lineNum int, values *[]interface{}) error {
sheet := e.eObj.GetSheetName(0)
return e.SetRowByColForSheet(sheet, col, lineNum, values)
}
// SetRowByColForSheet 为指定sheet,写入行,lineNum 为行号(不是索引下标),col为每行从第多少列开始,values 为切片指针
func (e *Exsoul) SetRowByColForSheet(sheet string, col, lineNum int, values *[]interface{}) error {
axis, err := excelize.CoordinatesToCellName(col, lineNum)
if err != nil {
return err
}
err = e.eObj.SetSheetRow(sheet, axis, values)
return err
}
// SetRowByList 为默认 sheet 批量写入行,从第1行开始
func (e *Exsoul) SetRowByList(vlist []*[]interface{}) error {
sheet := e.eObj.GetSheetName(0)
return e.SetRowByListForSheet(sheet, vlist)
}
// SetRowByListForSheet 为指定sheet批量写入行
func (e *Exsoul) SetRowByListForSheet(sheet string, vlist []*[]interface{}) error {
if len(vlist) == 0 {
return nil
}
for i, v := range vlist {
lineNum := i + 1
axis, _ := excelize.CoordinatesToCellName(1, lineNum)
err := e.eObj.SetSheetRow(sheet, axis, v)
if err != nil {
return newError("[exs]error while set value for `%s`: `%v`", axis, v)
}
}
return nil
}
// SaveAs 保存到 filepath 文件中
func (e *Exsoul) SaveAs(filepath string) error {
return e.eObj.SaveAs(filepath)
}
// SaveToWriter 保存至 writer 接口
func (e *Exsoul) SaveToWriter(writer io.Writer) error {
return e.eObj.Write(writer)
}