-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
54 lines (42 loc) · 888 Bytes
/
main.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
package main
import (
"encoding/json"
"fmt"
"github.com/xuri/excelize/v2"
)
var file = "books.xlsx"
func openXlsxFile() [][]string {
f, err := excelize.OpenFile(file)
if err != nil {
panic(err.Error())
}
rows, err := f.GetRows("Sheet1")
if err != nil {
panic(err.Error())
}
return rows
}
func createKey(row []string) map[int]string {
excelHeader := make(map[int]string)
for i, col := range row {
excelHeader[i] = col
}
return excelHeader
}
func main() {
rows := openXlsxFile()
excelHeader := createKey(rows[0])
excelSheet := make([]interface{}, 0)
for _, row := range rows[1:] {
excelRow := make(map[string]interface{})
for i, colCell := range row {
excelRow[excelHeader[i]] = colCell
}
excelSheet = append(excelSheet, excelRow)
}
json, err := json.Marshal(excelSheet)
if err != nil {
panic(err.Error())
}
fmt.Println(string(json))
}