21
0
xls/xls.go

52 lines
948 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
var root *ole2.File
2015-09-30 04:40:01 +02:00
for _, file := range dir {
name := file.Name()
if name == "Workbook" {
2017-10-23 10:14:16 +02:00
if book == nil {
book = file
}
//book = file
2015-09-30 04:40:01 +02:00
// break
}
if name == "Book" {
book = file
// break
}
if name == "Root Entry" {
root = file
}
2015-09-30 04:40:01 +02:00
}
if book != nil {
wb = newWorkBookFromOle2(ole.OpenFile(book, root))
2015-09-30 04:40:01 +02:00
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
}