20
0

fix the bug of continue with rich text format

This commit is contained in:
Liu Ming 2016-07-01 21:29:00 +08:00
parent 991920a697
commit eaea4d0573

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"io" "io"
"log"
"os" "os"
"unicode/utf16" "unicode/utf16"
) )
@ -22,6 +23,8 @@ type WorkBook struct {
rs io.ReadSeeker rs io.ReadSeeker
sst []string sst []string
continue_utf16 uint16 continue_utf16 uint16
continue_rich uint16
continue_apsb uint32
dateMode uint16 dateMode uint16
} }
@ -94,10 +97,11 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
} }
for err == nil && offset_pre < len(wb.sst) { for err == nil && offset_pre < len(wb.sst) {
var str string var str string
// if size > 0 { if size > 0 {
str, err = wb.get_string(buf_item, size) str, err = wb.get_string(buf_item, size)
wb.sst[offset_pre] = wb.sst[offset_pre] + str wb.sst[offset_pre] = wb.sst[offset_pre] + str
// } }
offset_pre++ offset_pre++
err = binary.Read(buf_item, binary.LittleEndian, &size) err = binary.Read(buf_item, binary.LittleEndian, &size)
} }
@ -158,15 +162,21 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
_, err = buf.Read(bts) _, err = buf.Read(bts)
res = string(bts) res = string(bts)
} else { } else {
var richtext_num uint16 var richtext_num = uint16(0)
var phonetic_size uint32 var phonetic_size = uint32(0)
var flag byte var flag byte
err = binary.Read(buf, binary.LittleEndian, &flag) err = binary.Read(buf, binary.LittleEndian, &flag)
if flag&0x8 != 0 { if flag&0x8 != 0 {
err = binary.Read(buf, binary.LittleEndian, &richtext_num) err = binary.Read(buf, binary.LittleEndian, &richtext_num)
} else if w.continue_rich > 0 {
richtext_num = w.continue_rich
w.continue_rich = 0
} }
if flag&0x4 != 0 { if flag&0x4 != 0 {
err = binary.Read(buf, binary.LittleEndian, &phonetic_size) err = binary.Read(buf, binary.LittleEndian, &phonetic_size)
} else if w.continue_apsb > 0 {
phonetic_size = w.continue_apsb
w.continue_apsb = 0
} }
if flag&0x1 != 0 { if flag&0x1 != 0 {
var bts = make([]uint16, size) var bts = make([]uint16, size)
@ -177,7 +187,7 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
runes := utf16.Decode(bts[:i]) runes := utf16.Decode(bts[:i])
res = string(runes) res = string(runes)
if i < size { if i < size {
w.continue_utf16 = size - i w.continue_utf16 = size - i + 1
} }
} else { } else {
var bts = make([]byte, size) var bts = make([]byte, size)
@ -195,19 +205,29 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
runes := utf16.Decode(bts1) runes := utf16.Decode(bts1)
res = string(runes) res = string(runes)
} }
if flag&0x8 != 0 { if richtext_num > 0 {
var bts []byte var bts []byte
var seek_size int64
if w.Is5ver { if w.Is5ver {
bts = make([]byte, 2*richtext_num) seek_size = int64(2 * richtext_num)
} else { } else {
bts = make([]byte, 4*richtext_num) seek_size = int64(4 * richtext_num)
} }
bts = make([]byte, seek_size)
err = binary.Read(buf, binary.LittleEndian, bts) err = binary.Read(buf, binary.LittleEndian, bts)
if err == io.EOF {
w.continue_rich = richtext_num
} }
if flag&0x4 != 0 {
// err = binary.Read(buf, binary.LittleEndian, bts)
}
if phonetic_size > 0 {
var bts []byte var bts []byte
bts = make([]byte, phonetic_size) bts = make([]byte, phonetic_size)
err = binary.Read(buf, binary.LittleEndian, bts) err = binary.Read(buf, binary.LittleEndian, bts)
if err == io.EOF {
w.continue_apsb = phonetic_size
}
} }
} }
return return
@ -244,6 +264,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. //Notice: the max value is the limit of the max capacity of lines.
//Warning: the helper function will need big memeory if file is large.
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 {