-
Notifications
You must be signed in to change notification settings - Fork 0
/
xlsx.go
55 lines (46 loc) · 907 Bytes
/
xlsx.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
package file
import (
"log"
"github.com/tealeg/xlsx/v3"
)
// XLSXReader is a reader for xlsx file
type XLSXReader struct{}
func (r *XLSXReader) Read(path string) (file *File, err error) {
wb, err := xlsx.OpenFile(path)
if err != nil {
return
}
sheets := []*Sheet{}
for _, sh := range wb.Sheets {
grid := &Grid{
cells: map[int]map[int]*Cell{},
}
grid.RowTotal = sh.MaxRow
grid.ColTotal = sh.MaxCol
for row := 0; row < grid.RowTotal; row++ {
for col := 0; col < grid.ColTotal; col++ {
c, err := sh.Cell(row, col)
if err != nil {
log.Println(err)
continue
}
value, err := c.FormattedValue()
if err != nil {
log.Println(err)
continue
}
grid.SetCell(row, col, value)
}
}
sheet := &Sheet{
Name: sh.Name,
Grid: grid,
}
sheets = append(sheets, sheet)
}
file = &File{
Name: path,
Sheets: sheets,
}
return
}