20
0

support type 0x0204

This commit is contained in:
Liu Ming 2016-05-28 10:39:46 +08:00
parent 990d2cc41f
commit 06ae67f1d1
5 changed files with 75 additions and 31 deletions

9
col.go
View File

@ -183,6 +183,15 @@ func (c *LabelsstCol) String(wb *WorkBook) []string {
return []string{wb.sst[int(c.Sst)]} return []string{wb.sst[int(c.Sst)]}
} }
type labelCol struct {
BlankCol
Str string
}
func (c *labelCol) String(wb *WorkBook) []string {
return []string{c.Str}
}
type BlankCol struct { type BlankCol struct {
Col Col
Xf uint16 Xf uint16

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"io" "io"
"os"
"unicode/utf16" "unicode/utf16"
) )
@ -59,6 +60,9 @@ func (w *WorkBook) addFont(font *FontInfo, buf io.ReadSeeker) {
} }
func (w *WorkBook) addFormat(format *Format) { func (w *WorkBook) addFormat(format *Format) {
if w.Formats == nil {
os.Exit(1)
}
w.Formats[format.Head.Index] = format w.Formats[format.Head.Index] = format
} }
@ -132,10 +136,10 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
binary.Read(buf_item, binary.LittleEndian, f) binary.Read(buf_item, binary.LittleEndian, f)
wb.addFont(f, buf_item) wb.addFont(f, buf_item)
case 0x41E: //FORMAT case 0x41E: //FORMAT
f := new(Format) font := new(Format)
binary.Read(buf_item, binary.LittleEndian, &f.Head) binary.Read(buf_item, binary.LittleEndian, &font.Head)
f.str = wb.get_string(buf_item, f.Head.Size) font.str = wb.get_string(buf_item, font.Head.Size)
wb.addFormat(f) wb.addFormat(font)
case 0x22: //DATEMODE case 0x22: //DATEMODE
binary.Read(buf_item, binary.LittleEndian, &wb.dateMode) binary.Read(buf_item, binary.LittleEndian, &wb.dateMode)
} }
@ -229,6 +233,7 @@ func (w *WorkBook) NumSheets() int {
} }
//helper function to read all cells from file //helper function to read all cells from file
//Notice: the max value is the limit of the max capacity of lines.
func (w *WorkBook) ReadAllCells(max int) (res [][]string) { func (w *WorkBook) ReadAllCells(max int) (res [][]string) {
res = make([][]string, 0) res = make([][]string, 0)
for _, sheet := range w.sheets { for _, sheet := range w.sheets {

View File

@ -87,6 +87,13 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, b *bof, pre *bof) *bof {
case 0xFD: //LABELSST case 0xFD: //LABELSST
col = new(LabelsstCol) col = new(LabelsstCol)
binary.Read(buf, binary.LittleEndian, col) binary.Read(buf, binary.LittleEndian, col)
case 0x204:
c := new(labelCol)
binary.Read(buf, binary.LittleEndian, &c.BlankCol)
var count uint16
binary.Read(buf, binary.LittleEndian, &count)
c.Str = w.wb.get_string(buf, count)
col = c
case 0x201: //BLANK case 0x201: //BLANK
col = new(BlankCol) col = new(BlankCol)
binary.Read(buf, binary.LittleEndian, col) binary.Read(buf, binary.LittleEndian, col)

22
xls.go
View File

@ -1,33 +1,25 @@
package xls package xls
import ( import (
"github.com/extrame/ole2"
"io" "io"
"io/ioutil" "os"
"github.com/extrame/ole2"
) )
//Open one xls file //Open one xls file
func Open(file string, charset string) (*WorkBook, error) { func Open(file string, charset string) (*WorkBook, error) {
if bts, err := ioutil.ReadFile(file); err == nil { if fi, err := os.Open(file); err == nil {
return parse(bts, charset) return OpenReader(fi, charset)
} else { } else {
return nil, err return nil, err
} }
} }
//Open xls file from reader //Open xls file from reader
func OpenReader(reader io.ReadCloser, charset string) (*WorkBook, error) { func OpenReader(reader io.ReadSeeker, charset string) (wb *WorkBook, err error) {
if bts, err := ioutil.ReadAll(reader); err == nil {
return parse(bts, charset)
} else {
return nil, err
}
}
func parse(bts []byte, charset string) (wb *WorkBook, err error) {
var ole *ole2.Ole var ole *ole2.Ole
if ole, err = ole2.Open(bts, charset); err == nil { if ole, err = ole2.Open(reader, charset); err == nil {
var dir []*ole2.File var dir []*ole2.File
if dir, err = ole.ListDir(); err == nil { if dir, err = ole.ListDir(); err == nil {
var book *ole2.File var book *ole2.File

View File

@ -8,27 +8,58 @@ import (
) )
func TestOpen(t *testing.T) { func TestOpen(t *testing.T) {
xlFile, _ := Open("Table.xls", "") if xlFile, err := Open("reportComuni.xls", "utf-8"); err == nil {
sheet1 := xlFile.GetSheet(0) if sheet1 := xlFile.GetSheet(0); sheet1 != nil {
fmt.Println(sheet1.Name) fmt.Println("Total Lines ", sheet1.MaxRow, sheet1.Name)
fmt.Print(sheet1.Rows) for i := 0; i < int(sheet1.MaxRow); i++ {
for k, row1 := range sheet1.Rows { fmt.Printf("row %v point %v \n", i, sheet1.Rows[uint16(i)])
// row1 := sheet1.Rows[1] if sheet1.Rows[uint16(i)] == nil {
fmt.Printf("\n[%d]", k) continue
for _, col1 := range row1.Cols { }
// col1 := row1.Cols[0] row := sheet1.Rows[uint16(i)]
fmt.Print(col1.LastCol()) for n, col := range row.Cols {
fmt.Print(" ") fmt.Println(n, "==>", col.String(xlFile), " ")
}
// col1 := .Cols[0]
// col2 := sheet1.Rows[uint16(i)].Cols[1]
// fmt.Printf("\ncol1 %v \nCol2 %v \n", col1.String(xlFile), col2.String(xlFile))
} }
} }
} }
}
func TestOpen1(t *testing.T) {
xlFile, _ := Open("000.xls", "")
for i := 0; i < xlFile.NumSheets(); i++ {
fmt.Println(xlFile.GetSheet(i).Name)
sheet := xlFile.GetSheet(i)
row := sheet.Rows[1]
for i, col := range row.Cols {
fmt.Println(i, col.String(xlFile))
}
}
// sheet1 := xlFile.GetSheet(0)
// fmt.Println(sheet1.Name)
// fmt.Print(sheet1.Rows)
// for k, row1 := range sheet1.Rows {
// // row1 := sheet1.Rows[1]
// fmt.Printf("\n[%d]", k)
// for _, col1 := range row1.Cols {
// // col1 := row1.Cols[0]
// fmt.Print(col1.LastCol())
// fmt.Print(" ")
// }
// }
}
func TestBof(t *testing.T) { func TestBof(t *testing.T) {
b := new(bof) b := new(bof)
b.Id = 0x41E b.Id = 0x41E
b.Size = 55 b.Size = 55
buf := bytes.NewReader([]byte{0x07, 0x00, 0x19, 0x00, 0x01, 0x22, 0x00, 0xE5, 0xFF, 0x22, 0x00, 0x23, 0x00, 0x2C, 0x00, 0x23, 0x00, 0x23, 0x00, 0x30, 0x00, 0x2E, 0x00, 0x30, 0x00, 0x30, 0x00, 0x3B, 0x00, 0x22, 0x00, 0xE5, 0xFF, 0x22, 0x00, 0x5C, 0x00, 0x2D, 0x00, 0x23, 0x00, 0x2C, 0x20, 0x00}) buf := bytes.NewReader([]byte{0x07, 0x00, 0x19, 0x00, 0x01, 0x22, 0x00, 0xE5, 0xFF, 0x22, 0x00, 0x23, 0x00, 0x2C, 0x00, 0x23, 0x00, 0x23, 0x00, 0x30, 0x00, 0x2E, 0x00, 0x30, 0x00, 0x30, 0x00, 0x3B, 0x00, 0x22, 0x00, 0xE5, 0xFF, 0x22, 0x00, 0x5C, 0x00, 0x2D, 0x00, 0x23, 0x00, 0x2C, 0x20, 0x00})
new(WorkBook).parseBof(buf, b, b, 0) wb := new(WorkBook)
wb.Formats = make(map[uint16]*Format)
wb.parseBof(buf, b, b, 0)
} }
func TestMaxRow(t *testing.T) { func TestMaxRow(t *testing.T) {