Skip to content

Commit

Permalink
FIX: data corruption while reading, issue extrame#47
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeilem committed Dec 1, 2018
1 parent 3be8a7e commit 722fc16
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func (xf *XfRk) String(wb *WorkBook) string {
fNo := wb.Xfs[idx].formatNo()
if fNo >= 164 { // user defined format
if formatter := wb.Formats[fNo]; formatter != nil {
if (strings.Contains(formatter.str, "#") || strings.Contains(formatter.str, ".00")){
if strings.Contains(formatter.str, "#") || strings.Contains(formatter.str, ".00") {
//If format contains # or .00 then this is a number
return xf.Rk.String()
}else{
return xf.Rk.String()
} else {
i, f, isFloat := xf.Rk.number()
if !isFloat {
f = float64(i)
Expand Down
12 changes: 6 additions & 6 deletions comparexlsxlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@ import (
)

//Compares xls and xlsx files
func compareXlsXlsx(filepathname string) string {
func compareXlsXlsx(filepathname string) error {
xlsFile, err := Open(path.Join("testdata", filepathname)+".xls", "utf-8")
if err != nil {
return fmt.Sprintf("Cant open xls file: %s", err)
return fmt.Errorf("Cant open xls file: %s", err)
}

xlsxFile, err := xlsx.OpenFile(path.Join("testdata", filepathname) + ".xlsx")
if err != nil {
return fmt.Sprintf("Cant open xlsx file: %s", err)
return fmt.Errorf("Cant open xlsx file: %s", err)
}

for sheet, xlsxSheet := range xlsxFile.Sheets {
xlsSheet := xlsFile.GetSheet(sheet)
if xlsSheet == nil {
return fmt.Sprintf("Cant get xls sheet")
return fmt.Errorf("Cant get xls sheet")
}
for row, xlsxRow := range xlsxSheet.Rows {
xlsRow := xlsSheet.Row(row)
for cell, xlsxCell := range xlsxRow.Cells {
xlsText := xlsRow.Col(cell)
xlsxText := xlsxCell.String()
if xlsText != xlsxText {
return fmt.Sprintf("Sheet: %d, row: %d, col: %d, xlsx: (%s)[%d], xls: (%s)[%d].",
return fmt.Errorf("Sheet: %d, row: %d, col: %d, xlsx: (%s)[%d], xls: (%s)[%d].",
sheet, row, cell, xlsxText, len(xlsxText), xlsText, len(xlsText))
}
}
}
}

return ""
return nil
}
6 changes: 3 additions & 3 deletions issue47_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
)

func TestIssue47(t *testing.T) {
e := compareXlsXlsx("issue47")
err := compareXlsXlsx("issue47")

if e != "" {
t.Fatalf("XLS an XLSX are not equal: %s", e)
if err != nil {
t.Fatalf("XLS an XLSX are not equal: %s", err)
}

}
Binary file modified testdata/issue47.xls
Binary file not shown.
Binary file modified testdata/issue47.xlsx
Binary file not shown.
24 changes: 9 additions & 15 deletions workbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package xls
import (
"bytes"
"encoding/binary"
"golang.org/x/text/encoding/charmap"
"io"
"os"
"unicode/utf16"
"golang.org/x/text/encoding/charmap"
)

//xls workbook type
Expand Down Expand Up @@ -76,7 +76,7 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
binary.Read(buf, binary.LittleEndian, bts)
buf_item := bytes.NewReader(bts)
switch b.Id {
case 0x809:
case 0x809: //BIFF 5-8
bif := new(biffHeader)
binary.Read(buf_item, binary.LittleEndian, bif)
if bif.Ver != 0x600 {
Expand All @@ -86,12 +86,7 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
case 0x042: // CODEPAGE
binary.Read(buf_item, binary.LittleEndian, &wb.Codepage)
case 0x3c: // CONTINUE
// step back if previous element not yet completed
if wb.continue_utf16 > 0 {
offset_pre--
}

if pre.Id == 0xfc {
if pre.Id == 0xfc { //SST
var size uint16
var err error
if wb.continue_utf16 >= 1 {
Expand Down Expand Up @@ -134,12 +129,12 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
wb.sst[i] = wb.sst[i] + str
}

if err == io.EOF {
if err == io.EOF && wb.continue_utf16>0 {
break
}
}
offset = i
case 0x85: // bOUNDSHEET
case 0x85: // boundsheet
var bs = new(boundsheet)
binary.Read(buf_item, binary.LittleEndian, bs)
// different for BIFF5 and BIFF8
Expand Down Expand Up @@ -169,9 +164,9 @@ func (wb *WorkBook) parseBof(buf io.ReadSeeker, b *bof, pre *bof, offset_pre int
return
}
func decodeWindows1251(enc []byte) string {
dec := charmap.Windows1251.NewDecoder()
out, _ := dec.Bytes(enc)
return string(out)
dec := charmap.Windows1251.NewDecoder()
out, _ := dec.Bytes(enc)
return string(out)
}
func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err error) {
if w.Is5ver {
Expand Down Expand Up @@ -199,8 +194,6 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
if flag&0x1 != 0 {
var bts = make([]uint16, size)
var i = uint16(0)
// we need local err here
var err error
for ; i < size && err == nil; i++ {
err = binary.Read(buf, binary.LittleEndian, &bts[i])
}
Expand All @@ -217,6 +210,7 @@ func (w *WorkBook) get_string(buf io.ReadSeeker, size uint16) (res string, err e
if i < size {
w.continue_utf16 = size - i + 1
}

} else {
var bts = make([]byte, size)
var n int
Expand Down

0 comments on commit 722fc16

Please sign in to comment.