-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxls.go
More file actions
55 lines (51 loc) · 1012 Bytes
/
Copy pathxls.go
File metadata and controls
55 lines (51 loc) · 1012 Bytes
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 sheet
import (
"errors"
"io"
"io/ioutil"
"github.com/extrame/xls"
)
type xlsReader struct{}
func (xr *xlsReader) Read(r io.Reader, s Specification) (si *Info, err error) {
var wb *xls.WorkBook
wb, err = xls.OpenReader(ioutil.NopCloser(r), "CP-1252") // charset parameter isn't used in the underlying library
if err != nil {
return
}
var ws *xls.WorkSheet
if s.SheetName != "" {
found := false
for i := 0; i < wb.NumSheets(); i++ {
ws = wb.GetSheet(i)
if ws == nil {
continue
}
if ws.Name == s.SheetName {
found = true
break
}
}
if !found {
err = errors.New("Workbook does not include a sheet named " + s.SheetName)
return
}
}
if ws == nil {
ws = wb.GetSheet(s.SheetIndex)
}
if ws == nil {
err = errors.New("Failed to open the worksheet")
return
}
si = &Info{}
for k, row := range ws.Rows {
if k == s.HeaderRowIndex {
si.ColCount = len(row.Cols)
continue
}
if k >= s.DataRowStartIndex {
si.RowCount++
}
}
return
}