-
Notifications
You must be signed in to change notification settings - Fork 40
/
common.go
36 lines (32 loc) · 929 Bytes
/
common.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
// Package excel provide a simple and light reader to read `*.xlsx` as a relate-db-like table.
// See `ReadMe.md` or `Examples` for more usage.
package excel
// NewConnecter make a new connecter to connect to a exist xlsx file.
func NewConnecter() Connecter {
return &connect{}
}
// UnmarshalXLSX unmarshal a sheet of XLSX file into a slice container.
// The sheet name will be inferred from element of container
// If container implement the function of GetXLSXSheetName()string, the return string will used.
// Oterwise will use the reflect struct name.
func UnmarshalXLSX(filePath string, container interface{}) error {
conn := NewConnecter()
err := conn.Open(filePath)
if err != nil {
return err
}
rd, err := conn.NewReader(container)
if err != nil {
conn.Close()
return err
}
err = rd.ReadAll(container)
if err != nil {
conn.Close()
rd.Close()
return err
}
conn.Close()
rd.Close()
return nil
}