-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
While opening excel file . It is showing recovery error #798
Comments
Thanks for your feedback, could you create the issue by an issue template and provide more details? |
Description Excel can not open saved file. See error above (once you click "yes" it simply says "file is damaged"). Steps to reproduce the issue: create "test_file.xlsx" in src folder. File works with Excel without problems. main.go
Describe the results you expected: file can be opened with excel Output of go version: go version go1.16.2 darwin/amd64 Excelize version or commit ID: Environment details (OS, Microsoft Excel™ version, physical, etc.): |
It works if I copy the content of the test_file (which has been generated automatically as the result of a db query) into a new file. So there must be something wrong with the meta data of the generated file? I am not sure how to debug this |
Hi @AnotherCoolDude, thanks for your feedback, could you provide the code to produce this issue? Note that open data stream from database query by |
Hi @xuri |
Hi @AnotherCoolDude, thanks for your feedback. There still no more details about this issue from the issue author. I close this issue and welcome left comments if you have any more questions. /cc @prakash0101 |
I got this issue either. Have you fixed this? |
Hi @Rorke76753, there are multiple reasons for cause file corrupted, which version of the Excelize are you using? Could you show us a complete, standalone example program or reproducible demo? and please try to upgrade to the master branch code. |
Output of go version: go version go1.13.15 darwin/amd64 Excelize version or commit ID: Environment details (OS, Microsoft Excel™ version, physical, etc.): Code: type BasicExcelExporter struct {
excelExporter iSpecificExporter
}
func (b BasicExcelExporter) ExportAsExcel(data []excel_handler.IExcelDbObject) (string, error) {
file := excelize.NewFile()
index := 0
sheetIndex := 1
filePath := b.excelExporter.generateFileName()
writeData := data[index:]
err := b.writeSheet(file, data, sheetIndex)
if err != nil {
return "", err
}
err := file.SaveAs(filePath)
if err != nil {
return "", err
}
return filePath, nil
}
func (b BasicExcelExporter) writeSheet(file *excelize.File, data []excel_handler.IExcelDbObject, sheetIndex int) error {
sheetName := b.excelExporter.getSheetName() + strconv.Itoa(sheetIndex)
_ = file.NewSheet(sheetName)
err := b.setTemplateHeader(file, sheetName)
if err != nil {
return err
}
err = b.setCellValue(file, sheetName, data)
if err != nil {
return err
}
return nil
}
//set first row of the sheet
func (b BasicExcelExporter) setTemplateHeader(file *excelize.File, name string) error {
topRow := 1
header := b.excelExporter.getTemplateHeader()
for i := range header {
column := convertDecimalToBase26(i + 1)
cellName := getCellName(topRow, column)
err := file.SetCellValue(name, cellName, header[i])
if err != nil {
return err
}
}
return nil
}
//input the values
func (b BasicExcelExporter) setCellValue(file *excelize.File, sheetName string, data []excel_handler.IExcelDbObject) error {
row := 2
for i := range data {
//values is a set of function which like func(excel_handler.IExcelDbObject) string
//this function provide the getter from struct field to a string value which store in excel
values := data[i].GetExcelValue()
for j := range values {
column := convertDecimalToBase26(j + 1)
cell := getCellName(row, column)
err := file.SetCellValue(sheetName, cell, values[j](data[i]))
if err != nil {
return err
}
}
row++
}
return nil
}
func getCellName(row int, column string) string {
return fmt.Sprintf("%s%d", column, row)
}
func convertDecimalToBase26(columnNumber int) string {
ans := make([]byte, 0)
for columnNumber > 0 {
columnNumber--
ans = append(ans, 'A'+byte(columnNumber%26))
columnNumber /= 26
}
for i, n := 0, len(ans); i < n/2; i++ {
ans[i], ans[n-1-i] = ans[n-1-i], ans[i]
}
return string(ans)
}
|
Hi @Rorke76753, what's your input data look likes, could you provides a simple code with entry main function to reproduce this issue? In addition, just using |
func TestExport(t *testing.T) {
exporter := GenerateTestExporter()
_, err := exporter.ExportAsExcel([]excel_handler.IExcelDbObject{testStruct{a: "a"}, testStruct{a: "b"}})
if err != nil {
panic(err.Error())
}
}
func GenerateTestExporter() BasicExcelExporter {
return BasicExcelExporter{excelExporter: testExporter{}}
}
type testStruct struct {
a string
}
func (t testStruct) SetTime(time uint32) {
}
func (t testStruct) GetExcelValue() []func(object excel_handler.IExcelDbObject) string {
return getters
}
func getA(object excel_handler.IExcelDbObject) string {
tmp := object.(testStruct)
return tmp.a
}
var getters = []func(object excel_handler.IExcelDbObject) string{getA}
type testExporter struct {
}
func (t testExporter) getSheetName() string {
return "sheet"
}
func (t testExporter) generateFileName() string {
return t.getFilePath() + "test.xlsx"
}
func (t testExporter) getFilePath() string {
//temp, project relative path
return ""
}
func (t testExporter) getTemplateHeader() excel_handler.ExcelTplRow {
return excel_handler.ExcelTplRow{"test"}
}
package excel_handler
type IExcelDbObject interface
GetExcelValue() []func(object IExcelDbObject) string
} this is my test method, and how can i send my export file to you to figure the problem |
@xuri I fix this problem now. It was caused by my create sheet name "Sheet1" conflict with the default sheet.Thank. |
Hi @jamalkaksouri, which version of Go are you using? There are some incompatible changes in the Go 1.21.0 encoding/xml library, which will cause generate corrupted workbook, please try to using Go 1.20 or previous Go version, also reference the issue #1465, #1595, #1603 and and golang/go#61881. |
I appreciate your quick reply. |
I bumped into this today. I can confirm that downgrading GO fixes the issue. If it might help: GO 1.20.7 didn't work either for me, but after downgrading to go1.19.12 everything is working again (using exelize v2.8.0. Thank you. |
SA_Opening_and_Onboarding_NPS_and_FOCUS_Salary_SO_Scores_Feb`21 (2).xlsx
I have screen shot as well as excel file for your refernece.
The text was updated successfully, but these errors were encountered: