Skip to content
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

XLSX files produced by this library need recover by Microsoft Excel when open it. #788

Open
dablelv opened this issue Nov 1, 2023 · 19 comments

Comments

@dablelv
Copy link

dablelv commented Nov 1, 2023

The version of tealeg/xlsx is v3.3.4, and the v1 also has the same problem.

The returned excel file from backend service api and download through browser, then open it and the Microsoft Excel will thorw a warning message:

We found a problem with some content in xxx.xslx. Do you want us to try to revocer as much as we can? If you trust the source of this workbook, click Yes.

If I click yes, the excel file will be repaired successfully and can be opened.

My backend service use Gin framework and the summary code to generate excel file and return it to browser as bellow:

func ExportHandler(c *gin.Context) {
    file := xlsx.NewFile()
    sheet, _ := file.AddSheet("InsuranceActive")

    // Add titles.
    titles := []string{
		"Id",
		"CreatedAt",
		"UpdatedAt",
		"CreatorId",
		"UpdaterId",
		"CompanyName",
    }
    row := sheet.AddRow()
    for _, title := range titles {
	cell := row.AddCell()
	cell.Value = title
    }

    // Add rows
    ...

    // Return the excel file.
    c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    filename := fmt.Sprintf("IndustryInfos_%v.xlsx", time.Now().Format("2006-01-02T15:04:05"))
    c.Header("Content-Disposition", "attachment; filename="+filename)
    if err := file.Write(c.Writer); err != nil {
        c.JSON(http.StatusOK, "failed")
        return
    }
    c.JSON(http.StatusOK, "suceess")
}
@dablelv dablelv changed the title The returned excel file from backend service api and download through browser, then open it Open the returned excel file from backend service api will encounter a warning message. Nov 1, 2023
@dablelv dablelv changed the title Open the returned excel file from backend service api will encounter a warning message. XLSX files produced by this library need repair by Microsoft Excel. Nov 1, 2023
@dablelv dablelv changed the title XLSX files produced by this library need repair by Microsoft Excel. XLSX files produced by this library need repair by Microsoft Excel when open it. Nov 1, 2023
@dablelv dablelv changed the title XLSX files produced by this library need repair by Microsoft Excel when open it. XLSX files produced by this library need recover by Microsoft Excel when open it. Nov 1, 2023
@tealeg
Copy link
Owner

tealeg commented Nov 1, 2023

@dablelv hi! This is quite a common error pattern, it usually means that some XML in the file is compliant with the standard, but not structured exactly as Excel would do it. The best debugging information you could give me is to save two versions of the generated file - one before and after it has been recovered. Comparing the XML in the xlsx files would give us a clue what it is that Excel doesn't like.

@dablelv
Copy link
Author

dablelv commented Nov 1, 2023

@dablelv hi! This is quite a common error pattern, it usually means that some XML in the file is compliant with the standard, but not structured exactly as Excel would do it. The best debugging information you could give me is to save two versions of the generated file - one before and after it has been recovered. Comparing the XML in the xlsx files would give us a clue what it is that Excel doesn't like.

I found the same issue already commited many years ago. The issue link is #53. There has one before and after it has been recovered excel file. The previous issue has been closed, but the problem remains.

@tealeg
Copy link
Owner

tealeg commented Nov 1, 2023

@dablelv - it is unlikely that it's actually the same issue. The symptom is seen for any case where Excel expects the data it receives to follow an exact pattern than is more restrictive than the Office OpenXML standard. The only way for me to know what was wrong is to reproduce the exact case you hit (by having all of the code and the input data) or by having examples of the file before and after Excel has repaired it.

@dablelv
Copy link
Author

dablelv commented Nov 2, 2023

@dablelv - it is unlikely that it's actually the same issue. The symptom is seen for any case where Excel expects the data it receives to follow an exact pattern than is more restrictive than the Office OpenXML standard. The only way for me to know what was wrong is to reproduce the exact case you hit (by having all of the code and the input data) or by having examples of the file before and after Excel has repaired it.

ok, here is the repaired before and after sample files.
xlsx_before_repaired.xlsx
xlsx_after_repaired.xlsx

I found that the repaired file's size is more than not repaired.

@tealeg
Copy link
Owner

tealeg commented Nov 2, 2023

@dablelv thanks! I'll try to take a look in the coming days.

Copy link

github-actions bot commented Jan 2, 2024

Stale issue message

Copy link

github-actions bot commented Mar 3, 2024

Stale issue message

@jlconrad3
Copy link

Any updates on this issue? I'm running across the same problem, specifically with timestamps. I've tried every cell.Set... function available. They all result in the same outcome. I read through the issue notes and I will do the same as the previous person by providing a before and after "repaired" file for comparison.

@tealeg
Copy link
Owner

tealeg commented Apr 2, 2024

@dablelv - I'm sorry, I know this has taken an age to get around to. In the case of your files, I think the route cause is that the font set-up in the file cannot render the text embedded in the spreadsheet. The repair XL seems to have done is to replace the font Arial, with the font "Song Dynasty" (宋体) in the file. I'll have to look a bit deeper to work out a solution.

@tealeg
Copy link
Owner

tealeg commented Apr 2, 2024

@jlconrad3 - as I mentioned above. Almost any error in the generated XLSX file would generate this response from excel (it's very difficult to put any two reports together as a single issue. Feel free to provide before and after examples and I'll try to look.

@jlconrad3
Copy link

I finally figured out my issue. It was the column width function. Wants I removed that function call, my exports to excel started working. I'm newish to this code base so I can't confirm that it ever worked, but it's been in the code for 3 year so I'm guessing at some point it worked.

sheet.SetColWidth

@tealeg
Copy link
Owner

tealeg commented Apr 3, 2024

@jlconrad3 interesting, I'll take a look.

@kolkov
Copy link

kolkov commented May 7, 2024

Hi! The same problem for me when updated to new version.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error423080_02.xml</logFileName><summary>Обнаружены ошибки в файле "C:\Users\Andy\Documents\***\test\dislocation (1).xlsx"</summary><removedRecords><removedRecord>Удаленные записи: Сведения о столбцах из части /xl/worksheets/sheet1.xml</removedRecord></removedRecords></recoveryLog>

@kolkov
Copy link

kolkov commented May 7, 2024

@kolkov
Copy link

kolkov commented May 7, 2024

I finally figured out my issue. It was the column width function. Wants I removed that function call, my exports to excel started working. I'm newish to this code base so I can't confirm that it ever worked, but it's been in the code for 3 year so I'm guessing at some point it worked.

sheet.SetColWidth

I confirm that! If I remove line the SetColWidth, error is diappeared. Ver 3.2.0 is working fine. @tealeg

@kolkov
Copy link

kolkov commented May 7, 2024

image

image

@kolkov
Copy link

kolkov commented May 7, 2024

@kolkov
Copy link

kolkov commented May 7, 2024

image
If only one column with custom width.

@kolkov
Copy link

kolkov commented May 7, 2024

I think that numeration of the colums is wrong. I set width to column 2, but actualy setted to column 1, file is ok now.
We need to check actual column range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants