21
0
xls/xls.go

59 lines
1.1 KiB
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
}
}
2018-03-22 02:25:17 +01:00
//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
}
}
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" {
book = file
// 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
}