21
0
xls/xls.go

53 lines
1002 B
Go
Raw Normal View History

2015-03-19 10:39:41 +01:00
package xls
import (
"github.com/extrame/ole2"
"io"
"io/ioutil"
)
2015-09-30 04:40:01 +02:00
//Open one xls file
2015-03-19 10:39:41 +01:00
func Open(file string, charset string) (*WorkBook, error) {
if bts, err := ioutil.ReadFile(file); err == nil {
return parse(bts, charset)
} else {
return nil, err
}
}
2015-09-30 04:40:01 +02:00
//Open xls file from reader
2015-03-19 10:39:41 +01:00
func OpenReader(reader io.ReadCloser, charset string) (*WorkBook, error) {
2015-09-30 04:40:01 +02:00
if bts, err := ioutil.ReadAll(reader); err == nil {
return parse(bts, charset)
} else {
return nil, err
}
2015-03-19 10:39:41 +01:00
}
2015-09-30 04:40:01 +02:00
func parse(bts []byte, charset string) (wb *WorkBook, err error) {
var ole *ole2.Ole
if ole, err = ole2.Open(bts, charset); err == nil {
var dir []*ole2.File
if dir, err = ole.ListDir(); err == nil {
var book *ole2.File
for _, file := range dir {
name := file.Name()
if name == "Workbook" {
book = file
// break
}
if name == "Book" {
book = file
// break
}
}
if book != nil {
wb = newWorkBookFromOle2(ole.OpenFile(book))
return
}
2015-03-19 10:39:41 +01:00
}
}
2015-09-30 04:40:01 +02:00
return
2015-03-19 10:39:41 +01:00
}