From d0179503ded2a2140dd9e93916b3ce0a8ebca975 Mon Sep 17 00:00:00 2001 From: Liu Ming Date: Tue, 16 Jun 2015 10:29:58 +0800 Subject: [PATCH] fix the bug of reading other sheets in one sheet --- workbook.go | 6 +++--- worksheet.go | 42 ++++++++++++++++++++++++------------------ xls_test.go | 20 -------------------- 3 files changed, 27 insertions(+), 41 deletions(-) delete mode 100644 xls_test.go diff --git a/workbook.go b/workbook.go index 4a2beb6..baf71f2 100644 --- a/workbook.go +++ b/workbook.go @@ -100,8 +100,8 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *BOF, pre *BOF, offset_pre int } } } - case 0x85: // BOUNDSHEET - var bs = new(Boundsheet) + case 0x85: // bOUNDSHEET + var bs = new(boundsheet) binary.Read(buf_item, binary.LittleEndian, bs) // different for BIFF5 and BIFF8 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) } -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)) w.Sheets = append(w.Sheets, &WorkSheet{bs: sheet, Name: name, wb: w}) } diff --git a/worksheet.go b/worksheet.go index 3a96421..6e21684 100644 --- a/worksheet.go +++ b/worksheet.go @@ -8,7 +8,7 @@ import ( "unicode/utf16" ) -type Boundsheet struct { +type boundsheet struct { Filepos uint32 Type byte Visible byte @@ -16,7 +16,7 @@ type Boundsheet struct { } type WorkSheet struct { - bs *Boundsheet + bs *boundsheet wb *WorkBook Name string Rows map[uint16]*Row @@ -30,6 +30,9 @@ func (w *WorkSheet) Parse(buf io.ReadSeeker) { for { if err := binary.Read(buf, binary.LittleEndian, bof); err == nil { bof_pre = w.parseBof(buf, bof, bof_pre) + if bof.Id == 0xa { + break + } } else { fmt.Println(err) break @@ -133,8 +136,13 @@ func (w *WorkSheet) parseBof(buf io.ReadSeeker, bof *BOF, pre *BOF) *BOF { } w.addRange(&hy.CellRange, &hy) + case 0x809: + log.Println("sheet start") + buf.Seek(int64(bof.Size), 1) + case 0xa: + log.Println("sheet end") 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) } if col != nil { @@ -153,27 +161,25 @@ func (w *WorkSheet) add(content interface{}) { } func (w *WorkSheet) addCell(col Coler, ch ContentHandler) { - var row *Row - 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 + w.addContent(col.Row(), ch) } 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 ok bool - for i := rang.FirstRow(); i <= rang.LastRow(); i++ { - if row, ok = w.Rows[i]; !ok { - info := new(RowInfo) - info.Index = i - row = w.addRow(info) - } - row.Cols[ch.FirstCol()] = ch + if row, ok = w.Rows[row_num]; !ok { + info := new(RowInfo) + info.Index = row_num + row = w.addRow(info) } + row.Cols[ch.FirstCol()] = ch } func (w *WorkSheet) addRow(info *RowInfo) (row *Row) { diff --git a/xls_test.go b/xls_test.go deleted file mode 100644 index c899ca3..0000000 --- a/xls_test.go +++ /dev/null @@ -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) -}