forked from extrame/xls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xls.go
61 lines (56 loc) · 1.18 KB
/
xls.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
package xls
import (
"io"
"os"
"github.com/extrame/ole2"
)
//Open one xls file
func Open(file string, charset string) (*WorkBook, error) {
if fi, err := os.Open(file); err == nil {
return OpenReader(fi, charset)
} else {
return nil, err
}
}
//Open one xls file and return the closer
func OpenWithCloser(file string, charset string) (*WorkBook, io.Closer, error) {
if fi, err := os.Open(file); err == nil {
wb, err := OpenReader(fi, charset)
return wb, fi, err
} else {
return nil, nil, err
}
}
//Open xls file from reader
func OpenReader(reader io.ReadSeeker, charset string) (wb *WorkBook, err error) {
var ole *ole2.Ole
if ole, err = ole2.Open(reader, charset); err == nil {
var dir []*ole2.File
if dir, err = ole.ListDir(); err == nil {
var book *ole2.File
var root *ole2.File
for _, file := range dir {
name := file.Name()
if name == "Workbook" {
if book == nil {
book = file
}
//book = file
// break
}
if name == "Book" {
book = file
// break
}
if name == "Root Entry" {
root = file
}
}
if book != nil {
wb = newWorkBookFromOle2(ole.OpenFile(book, root))
return
}
}
}
return
}