-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexcel.go
68 lines (57 loc) · 1.53 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
package main
import (
"fmt"
"time"
"github.com/360EntSecGroup-Skylar/excelize"
gft "github.com/snowlyg/gotransformer"
)
// 基础数据模型 beego/orm
type BaseModel struct {
Id int64
CreatedAt time.Time `orm:"auto_now_add;type(datetime);column(created_at);type(timestamp)"`
UpdatedAt time.Time `orm:"auto_now;type(datetime);column(updated_at);type(timestamp)"`
}
// 数据模型
type Model struct {
BaseModel
Name string `orm:"column(Name);null" description:""`
FirstName string `orm:"column(first_name);size(255);null" description:""`
DeletedAt time.Time `form:"-" orm:"column(deleted_at);type(timestamp);null" `
}
func main() {
titles := map[string]string{"0": "Name", "1": "FirstName"}
f, err := excelize.OpenFile("Book1.xlsx")
if err != nil {
fmt.Println(err)
return
}
// Excel 导入行数据转换
// 获取 Sheet1 上所有单元格
rows := f.GetRows("Sheet1")
// 提取 excel 数据
ms := make([]*Model, 0)
for roI, row := range rows {
if roI > 0 {
// 将数组 转成对应的 map
m := Model{}
x := gft.NewXlxsTransform(&m, titles, row, "", time.RFC3339, nil)
err := x.XlxsTransformer()
if err != nil {
fmt.Println(err)
return
}
ms = append(ms, &m)
}
}
fmt.Println(ms)
//Excel 导入单元格数据转换
titlesCell := map[string]string{"Name": "B1", "FirstName": "B2"}
m1 := Model{}
x := gft.NewXlxsTransform(&m1, titlesCell, nil, "sheet1", time.RFC3339, f)
err = x.XlxsCellTransformer()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(m1)
}