21
0
xls/xls.go

45 lines
817 B
Go
Raw Normal View History

2015-03-19 10:39:41 +01:00
package xls
import (
"io"
2016-05-28 04:39:46 +02:00
"os"
"github.com/extrame/ole2"
2015-03-19 10:39:41 +01:00
)
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) {
2016-05-28 04:39:46 +02:00
if fi, err := os.Open(file); err == nil {
return OpenReader(fi, charset)
2015-03-19 10:39:41 +01:00
} else {
return nil, err
}
}
2015-09-30 04:40:01 +02:00
//Open xls file from reader
2016-05-28 04:39:46 +02:00
func OpenReader(reader io.ReadSeeker, charset string) (wb *WorkBook, err error) {
2015-09-30 04:40:01 +02:00
var ole *ole2.Ole
2016-05-28 04:39:46 +02:00
if ole, err = ole2.Open(reader, charset); err == nil {
2015-09-30 04:40:01 +02:00
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
}