-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.go
71 lines (67 loc) · 1.73 KB
/
excel.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
/**
* Created by angelina on 2017/7/29.
*/
package yeeexcel
import "github.com/tealeg/xlsx"
// 传入excel的文件地址
// 第一个参数 是excel表格第一行的数据数组,一般为解释性type
// 第二个参数 是excel中真正的数据
// 第三个参数 为二维数组形式返回是否错误
func ReadExcel(filePath string) ([]string, [][]string, error) {
firstRow := make([]string, 0)
otherRows := make([][]string, 0)
file, err := xlsx.OpenFile(filePath)
if err != nil {
return firstRow, otherRows, err
}
for _, sheet := range file.Sheets {
for k, row := range sheet.Rows {
if k == 0 {
for _, cell := range row.Cells {
str := cell.String()
firstRow = append(firstRow, str)
}
} else {
otherRow := make([]string, 0)
for _, cell := range row.Cells {
str := cell.String()
otherRow = append(otherRow, str)
}
otherRows = append(otherRows, otherRow)
}
}
}
return firstRow, otherRows, nil
}
// 创建excel
// 第一个参数 是excel表格第一行的数据数组,一般为解释性type
// 第二个参数 是excel中真正的数据
// 第三个参数 存储文件的路径
func CreateExcel(firstRow []string, otherRows [][]string, path string) error {
file := xlsx.NewFile()
sheet, err := file.AddSheet("Sheet1")
if err != nil {
return err
}
//添加首行数据
row := sheet.AddRow()
columns := len(firstRow)
for i := 0; i < columns; i++ {
cell := row.AddCell()
cell.Value = firstRow[i]
}
//添加剩余的行数数据
rows := len(otherRows)
for i := 0; i < rows; i++ {
row = sheet.AddRow()
for j := 0; j < columns; j++ {
cell := row.AddCell()
cell.Value = otherRows[i][j]
}
}
err = file.Save(path)
if err != nil {
return err
}
return nil
}