Skip to content

Commit 4a6cf26

Browse files
authored
Merge pull request extrame#50 from sergeilem/master
FIX: data corruption while reading, issues extrame#31 extrame#46 extrame#47
2 parents 6fdb969 + 8fb5669 commit 4a6cf26

21 files changed

+198
-83
lines changed

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
language: go
2+
3+
# Force-enable Go modules. This will be unnecessary when Go 1.12 lands.
4+
env:
5+
- GO111MODULE=on
6+
7+
go:
8+
- 1.11.x
9+
10+
# Only clone the most recent commit.
11+
git:
12+
depth: 1
13+
14+
# Skip the install step. Don't `go get` dependencies. Only build with the code
15+
# in vendor/
16+
install: true
17+
18+
# Don't email me the results of the test runs.
19+
notifications:
20+
email: false
21+
22+
script:
23+
- go test -v -race ./... # Run all the tests with the race detector enabled

BigTable.xls

1.2 MB
Binary file not shown.

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22

33
[![GoDoc](https://godoc.org/github.com/extrame/xls?status.svg)](https://godoc.org/github.com/extrame/xls)
44

5-
Pure Golang xls library writen by [Rongshu Tech(chinese)](http://www.rongshu.tech).
5+
Pure Golang xls library writen by [Rongshu Tech (chinese)](http://www.rongshu.tech), based on libxls.
66

7-
Thanks for contributions from Tamás Gulácsi, sergeilem.
8-
9-
**English User please mailto** [Liu Ming](mailto:liuming@rongshu.tech)
10-
11-
This is a xls library writen in pure Golang. Almostly it is translated from the libxls library in c.
12-
13-
The master brunch has just the reading function without the format.
14-
15-
***new_formater** branch is for better format for date and number ,but just under test, you can try it in development environment. If you have some problem about the output format, tell me the problem, I will try to fix it.*
7+
Thanks for contributions from Tamás Gulácsi @tgulacsi, @flyin9.
168

179
# Basic Usage
1810

1911
* Use **Open** function for open file
2012
* Use **OpenWithCloser** function for open file and use the return value closer for close file
2113
* Use **OpenReader** function for open xls from a reader, you should close related file in your own code
2214

23-
* Follow the example in GODOC
24-
15+
* Follow the example in GoDoc

bigtable_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package xls
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestBigTable(t *testing.T) {
10+
xlFile, err := Open("BigTable.xls", "utf-8")
11+
if err != nil {
12+
t.Fatalf("Cant open xls file: %s", err)
13+
}
14+
15+
sheet := xlFile.GetSheet(0)
16+
if sheet == nil {
17+
t.Fatal("Cant get sheet")
18+
}
19+
20+
cnt1 := 1
21+
cnt2 := 10000
22+
cnt3 := 20000
23+
date1, _ := time.Parse("2006-01-02", "2015-01-01")
24+
date2, _ := time.Parse("2006-01-02", "2016-01-01")
25+
date3, _ := time.Parse("2006-01-02", "2017-01-01")
26+
27+
for i := 1; i <= 4999; i++ {
28+
row := sheet.Row(i)
29+
if row == nil {
30+
continue
31+
}
32+
33+
col2sample := fmt.Sprintf("%d от %s", cnt1, date1.Format("02.01.2006"))
34+
col5sample := fmt.Sprintf("%d от %s", cnt2, date2.Format("02.01.2006"))
35+
col8sample := fmt.Sprintf("%d от %s", cnt3, date3.Format("02.01.2006"))
36+
37+
col2 := row.Col(2)
38+
col5 := row.Col(5)
39+
col8 := row.Col(8)
40+
41+
if col2 != col2sample {
42+
t.Fatalf("Row %d: col 2 val not eq base value: %s != %s", i, col2, col2sample)
43+
}
44+
if col5 != col5sample {
45+
t.Fatalf("Row %d: col 5 val not eq base value: %s != %s", i, col5, col5sample)
46+
}
47+
if col8 != col8sample {
48+
t.Fatalf("Row %d: col 8 val not eq base value: %s != %s", i, col8, col8sample)
49+
}
50+
51+
cnt1++
52+
cnt2++
53+
cnt3++
54+
date1 = date1.AddDate(0, 0, 1)
55+
date2 = date2.AddDate(0, 0, 1)
56+
date3 = date3.AddDate(0, 0, 1)
57+
58+
}
59+
}

col.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ func (xf *XfRk) String(wb *WorkBook) string {
5454
fNo := wb.Xfs[idx].formatNo()
5555
if fNo >= 164 { // user defined format
5656
if formatter := wb.Formats[fNo]; formatter != nil {
57-
if strings.Contains(formatter.str, "#") || strings.Contains(formatter.str, ".00") {
57+
formatterLower := strings.ToLower(formatter.str)
58+
if formatterLower == "general" ||
59+
strings.Contains(formatter.str, "#") ||
60+
strings.Contains(formatter.str, ".00") ||
61+
strings.Contains(formatterLower, "m/y") ||
62+
strings.Contains(formatterLower, "d/y") ||
63+
strings.Contains(formatterLower, "m.y") ||
64+
strings.Contains(formatterLower, "d.y") ||
65+
strings.Contains(formatterLower, "h:") ||
66+
strings.Contains(formatterLower, "д.г") {
5867
//If format contains # or .00 then this is a number
5968
return xf.Rk.String()
6069
} else {
@@ -84,7 +93,7 @@ type RK uint32
8493
func (rk RK) number() (intNum int64, floatNum float64, isFloat bool) {
8594
multiplied := rk & 1
8695
isInt := rk & 2
87-
val := rk >> 2
96+
val := int32(rk) >> 2
8897
if isInt == 0 {
8998
isFloat = true
9099
floatNum = math.Float64frombits(uint64(val) << 34)
@@ -93,13 +102,11 @@ func (rk RK) number() (intNum int64, floatNum float64, isFloat bool) {
93102
}
94103
return
95104
}
96-
//+++ add lines from here
97105
if multiplied != 0 {
98106
isFloat = true
99107
floatNum = float64(val) / 100
100108
return
101109
}
102-
//+++end
103110
return int64(val), 0, false
104111
}
105112

comparexlsxlsx.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package xls
2+
3+
import (
4+
"fmt"
5+
"github.com/tealeg/xlsx"
6+
"math"
7+
"strconv"
8+
)
9+
10+
//Compares xls and xlsx files
11+
func CompareXlsXlsx(xlsfilepathname string, xlsxfilepathname string) string {
12+
xlsFile, err := Open(xlsfilepathname, "utf-8")
13+
if err != nil {
14+
return fmt.Sprintf("Cant open xls file: %s", err)
15+
}
16+
17+
xlsxFile, err := xlsx.OpenFile(xlsxfilepathname)
18+
if err != nil {
19+
return fmt.Sprintf("Cant open xlsx file: %s", err)
20+
}
21+
22+
for sheet, xlsxSheet := range xlsxFile.Sheets {
23+
xlsSheet := xlsFile.GetSheet(sheet)
24+
if xlsSheet == nil {
25+
return fmt.Sprintf("Cant get xls sheet")
26+
}
27+
for row, xlsxRow := range xlsxSheet.Rows {
28+
xlsRow := xlsSheet.Row(row)
29+
for cell, xlsxCell := range xlsxRow.Cells {
30+
xlsxText := xlsxCell.String()
31+
xlsText := xlsRow.Col(cell)
32+
if xlsText != xlsxText {
33+
//try to convert to numbers
34+
xlsFloat, xlsErr := strconv.ParseFloat(xlsText, 64)
35+
xlsxFloat, xlsxErr := strconv.ParseFloat(xlsxText, 64)
36+
//check if numbers have no significant difference
37+
if xlsErr == nil && xlsxErr == nil {
38+
diff := math.Abs(xlsFloat - xlsxFloat)
39+
if diff > 0.0000001 {
40+
return fmt.Sprintf("sheet:%d, row/col: %d/%d, xlsx: (%s)[%d], xls: (%s)[%d], numbers difference: %f.",
41+
sheet, row, cell, xlsxText, len(xlsxText),
42+
xlsText, len(xlsText), diff)
43+
}
44+
} else {
45+
return fmt.Sprintf("sheet:%d, row/col: %d/%d, xlsx: (%s)[%d], xls: (%s)[%d].",
46+
sheet, row, cell, xlsxText, len(xlsxText),
47+
xlsText, len(xlsText))
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
return ""
55+
}

issue47_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package xls
2+
3+
import (
4+
"io/ioutil"
5+
"path"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestIssue47(t *testing.T) {
12+
testdatapath := "testdata"
13+
files, err := ioutil.ReadDir(testdatapath)
14+
if err != nil {
15+
t.Fatalf("Cant read testdata directory contents: %s", err)
16+
}
17+
for _, f := range files {
18+
if filepath.Ext(f.Name()) == ".xls" {
19+
xlsfilename := f.Name()
20+
xlsxfilename := strings.TrimSuffix(xlsfilename, filepath.Ext(xlsfilename)) + ".xlsx"
21+
err := CompareXlsXlsx(path.Join(testdatapath, xlsfilename),
22+
path.Join(testdatapath, xlsxfilename))
23+
if err != "" {
24+
t.Fatalf("XLS file %s an XLSX file are not equal: %s", xlsfilename, err)
25+
}
26+
27+
}
28+
}
29+
30+
}

testdata/bigtable.xls

1.2 MB
Binary file not shown.

testdata/bigtable.xlsx

298 KB
Binary file not shown.

testdata/float.xls

5.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)