-
-
Notifications
You must be signed in to change notification settings - Fork 405
fix: fix template export generating corrupted Excel files #917
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
base: master
Are you sure you want to change the base?
fix: fix template export generating corrupted Excel files #917
Conversation
Summary of ChangesHello @A-nony-mous, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves critical issues causing Excel files generated from templates to become corrupted or unreadable. The changes primarily focus on correcting the OpenXML structure, cell type handling, and XML element ordering to strictly comply with the ECMA-376 specification, thereby ensuring the reliability and integrity of exported Excel documents. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request provides a comprehensive fix for a bug causing corrupted Excel file generation from templates. The changes correctly address multiple root causes related to OpenXML specification compliance, including element ordering, cell type representation for strings, and handling of shared strings. The introduction of the SetCellType method is an excellent refactoring that centralizes and standardizes cell type management. The code is well-structured, and the fixes are thorough. I have a couple of suggestions to further improve robustness and code style.
| if (type == "b") | ||
| c.SetAttribute("t", "b"); | ||
| else | ||
| c.RemoveAttribute("t"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Change SharedString conversion to use inlineStr format with <is><t> structure - Add SetCellType method to properly handle cell types (inlineStr for strings, remove t attr for numbers) - Fix XML element order per ECMA-376 spec (autoFilter before mergeCells/phoneticPr) - Clean duplicate xmlns declarations from phoneticPr, conditionalFormatting, autoFilter - Remove empty <v></v> tags that cause invalid XML - Support <is><t> structure in value node lookup Fixes template SaveAs generating files that Excel cannot open
c49d438 to
7eb1196
Compare
|
Hello @A-nony-mous, we appreciate your contribution. Unfortunately 8 tests are failing after your modifications, so until you investigate why that happens and fix it this PR cannot be accepted. |
|
@michelebastione I am sorry for previous bugs I made and I have fixed them. Besides, I updated MiniExcel/tests/MiniExcel.Core.Tests/MiniExcelIssueTests.cs Lines 1033 to 1047 in 5cfa15b
[<v>] tags to InlineString [<is><t>] tags (to satisfy ECMA-376 and avoid corruption), the assertion in this test needed to be updated to verify the new [<t>] tag format instead of the old [<v>] tag.
I've verified that all tests passed locally.
Green text shows "Successful" in Chinese.
I will leave this as a draft for your review. If you are happy with this please let me know and make it ready for another test? |
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request provides a comprehensive fix for a bug causing corrupted Excel file generation from templates. The changes are well-structured and address multiple root causes, including incorrect XML element order, invalid cell types for shared strings, and redundant namespace declarations. The introduction of the SetCellType method is a great refactoring for centralizing cell type handling. I've added a few suggestions to further improve code robustness and maintainability.
Fixing potential bug with inline string tag not being correctly identified when <v> tag is not null but also not matching the regex condition, and added GeneratedRegexAttribute for optimizing empty <v> tag regex replace function
|
Looks good, I just want to make a couple more checks but I'll probably merge it tomorrow. |

Description
This PR addresses a bug where Excel files generated from templates could become corrupted or throw an "XML error" upon opening.
Problem
When using

MiniExcel.SaveAsByTemplate()with Excel templates containing SharedStrings, the generated files would fail to open in Excel with the error "We found a problem with some content in 'xxx.xlsx'".template:
GachaLog.xlsx
demo error file:
1.xslx
logic code:
Root Causes
Invalid cell type for SharedStrings: The code was setting
t="str"with<v>element, but OpenXML requirest="inlineStr"with<is><t>...</t></is>structure for inline strings.Incorrect XML element order: Elements were written in wrong order. Per ECMA-376 specification, the correct order after
</sheetData>must be:autoFilter→mergeCells→phoneticPr→conditionalFormatting.Duplicate namespace declarations:
phoneticPr,conditionalFormatting, andautoFilterelements contained redundantxmlns="..."declarations when written to the output.Number cells with wrong type attribute: Numeric cells were incorrectly getting
t="inlineStr"attribute, which should be omitted for numbers.Related Issue
issue1
issue2
Changes
ReplaceSharedStringsToStr: Convert SharedString references to properinlineStrformat with<is><t>structureSetCellTypemethod: Centralized cell type handling - usesinlineStrfor strings, removestattribute for numbersWriteSheetXmlAsync: Fixed element order per ECMA-376 spec, extract and rewriteautoFilterin correct positionCleanXml: Added cleanup forxmlns="..."declarationsGenerateCellValuesAsync: Clean up empty<v></v>tags, support<is><t>structure in value lookupTesting
Breaking Changes
None. These are bug fixes that produce valid OpenXML output.