21
0
xls/comparexlsxlsx.go

56 lines
1.5 KiB
Go
Raw Normal View History

package xls
import (
"fmt"
"github.com/tealeg/xlsx"
2019-03-05 21:25:31 +01:00
"math"
"strconv"
)
//Compares xls and xlsx files
2019-03-05 21:25:31 +01:00
func compareXlsXlsx(xlsfilepathname string, xlsxfilepathname string) string {
xlsFile, err := Open(xlsfilepathname, "utf-8")
if err != nil {
return fmt.Sprintf("Cant open xls file: %s", err)
}
2019-03-05 21:25:31 +01:00
xlsxFile, err := xlsx.OpenFile(xlsxfilepathname)
if err != nil {
return fmt.Sprintf("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")
}
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 {
2019-03-05 21:25:31 +01:00
//try to convert to numbers
xlsFloat, xlsErr := strconv.ParseFloat(xlsText, 64)
xlsxFloat, xlsxErr := strconv.ParseFloat(xlsxText, 64)
//check if numbers have no significant difference
if xlsErr == nil && xlsxErr == nil {
diff := math.Abs(xlsFloat - xlsxFloat)
if diff > 0.0000001 {
return fmt.Sprintf("sheet:%d, row/col: %d/%d, xlsx: (%s)[%d], xls: (%s)[%d], numbers difference: %f.",
sheet, row, cell, xlsxText, len(xlsxText),
xlsText, len(xlsText), diff)
}
} else {
return fmt.Sprintf("sheet:%d, row/col: %d/%d, xlsx: (%s)[%d], xls: (%s)[%d].",
sheet, row, cell, xlsxText, len(xlsxText),
xlsText, len(xlsText))
}
}
}
}
}
return ""
}