20
0

fix the bug of reading other sheets in one sheet

This commit is contained in:
Liu Ming 2015-06-16 10:29:58 +08:00
parent dff67234df
commit d0179503de
3 changed files with 27 additions and 41 deletions

View File

@ -100,8 +100,8 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *BOF, pre *BOF, offset_pre int
} }
} }
} }
case 0x85: // BOUNDSHEET case 0x85: // bOUNDSHEET
var bs = new(Boundsheet) var bs = new(boundsheet)
binary.Read(buf_item, binary.LittleEndian, bs) binary.Read(buf_item, binary.LittleEndian, bs)
// different for BIFF5 and BIFF8 // different for BIFF5 and BIFF8
wb.addSheet(bs, buf_item) wb.addSheet(bs, buf_item)
@ -179,7 +179,7 @@ func (w *WorkBook) get_string_from_bytes(bts []byte, size uint16) string {
return w.get_string(buf, size) return w.get_string(buf, size)
} }
func (w *WorkBook) addSheet(sheet *Boundsheet, buf io.ReadSeeker) { func (w *WorkBook) addSheet(sheet *boundsheet, buf io.ReadSeeker) {
name := w.get_string(buf, uint16(sheet.Name)) name := w.get_string(buf, uint16(sheet.Name))
w.Sheets = append(w.Sheets, &WorkSheet{bs: sheet, Name: name, wb: w}) w.Sheets = append(w.Sheets, &WorkSheet{bs: sheet, Name: name, wb: w})
} }

View File

@ -8,7 +8,7 @@ import (
"unicode/utf16" "unicode/utf16"
) )
type Boundsheet struct { type boundsheet struct {
Filepos uint32 Filepos uint32
Type byte Type byte
Visible byte Visible byte
@ -16,7 +16,7 @@ type Boundsheet struct {
} }
type WorkSheet struct { type WorkSheet struct {
bs *Boundsheet bs *boundsheet
wb *WorkBook wb *WorkBook
Name string Name string
Rows map[uint16]*Row Rows map[uint16]*Row
@ -30,6 +30,9 @@ func (w *WorkSheet) Parse(buf io.ReadSeeker) {
for { for {
if err := binary.Read(buf, binary.LittleEndian, bof); err == nil { if err := binary.Read(buf, binary.LittleEndian, bof); err == nil {
bof_pre = w.parseBof(buf, bof, bof_pre) bof_pre = w.parseBof(buf, bof, bof_pre)
if bof.Id == 0xa {
break
}
} else { } else {
fmt.Println(err) fmt.Println(err)
break break
@ -133,8 +136,13 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, bof *BOF, pre *BOF) *BOF {
} }
w.addRange(&hy.CellRange, &hy) w.addRange(&hy.CellRange, &hy)
case 0x809:
log.Println("sheet start")
buf.Seek(int64(bof.Size), 1)
case 0xa:
log.Println("sheet end")
default: default:
fmt.Printf("Unknow %X,%d\n", bof.Id, bof.Size) log.Printf("Unknow %X,%d\n", bof.Id, bof.Size)
buf.Seek(int64(bof.Size), 1) buf.Seek(int64(bof.Size), 1)
} }
if col != nil { if col != nil {
@ -153,27 +161,25 @@ func (w *WorkSheet) add(content interface{}) {
} }
func (w *WorkSheet) addCell(col Coler, ch ContentHandler) { func (w *WorkSheet) addCell(col Coler, ch ContentHandler) {
var row *Row w.addContent(col.Row(), ch)
var ok bool
if row, ok = w.Rows[col.Row()]; !ok {
info := new(RowInfo)
info.Index = col.Row()
row = w.addRow(info)
}
row.Cols[ch.FirstCol()] = ch
} }
func (w *WorkSheet) addRange(rang Ranger, ch ContentHandler) { func (w *WorkSheet) addRange(rang Ranger, ch ContentHandler) {
for i := rang.FirstRow(); i <= rang.LastRow(); i++ {
w.addContent(i, ch)
}
}
func (w *WorkSheet) addContent(row_num uint16, ch ContentHandler) {
var row *Row var row *Row
var ok bool var ok bool
for i := rang.FirstRow(); i <= rang.LastRow(); i++ { if row, ok = w.Rows[row_num]; !ok {
if row, ok = w.Rows[i]; !ok { info := new(RowInfo)
info := new(RowInfo) info.Index = row_num
info.Index = i row = w.addRow(info)
row = w.addRow(info)
}
row.Cols[ch.FirstCol()] = ch
} }
row.Cols[ch.FirstCol()] = ch
} }
func (w *WorkSheet) addRow(info *RowInfo) (row *Row) { func (w *WorkSheet) addRow(info *RowInfo) (row *Row) {

View File

@ -1,20 +0,0 @@
package xls
import (
"bytes"
"fmt"
"testing"
)
func TestOpen(t *testing.T) {
wb, _ := Open("n201502111031.xls", "utf-8")
fmt.Println(wb.ReadAllCells(1000))
}
func TestBof(t *testing.T) {
bof := new(BOF)
bof.Id = 0x41E
bof.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})
new(WorkBook).parseBof(buf, bof, bof, 0)
}