Skip to content

Keep carriage returns and shared-underscore _xHHHH_ text on export - #1084

Open
vtxe02023274 wants to merge 1 commit into
mini-software:masterfrom
vtxe02023274:fix/encode-cr-and-underscore-escapes
Open

vtxe02023274 wants to merge 1 commit into
mini-software:masterfrom
vtxe02023274:fix/encode-cr-and-underscore-escapes

Conversation

@vtxe02023274

@vtxe02023274 vtxe02023274 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Two kinds of text change on export, both in XmlHelper.EncodeString.

Carriage return. \r is written as a raw character. An XML parser reads a raw CR or CRLF as LF, so "a\r\nb" comes back from Query as "a\nb", and Excel shows the same. Excel writes CR as _x000D_ (a cell with CRLF saved by Excel has a_x000D_ followed by the line break in sharedStrings.xml), and DecodeString already reads that back as \r. The encoder now writes _x000D_ too.

_xHHHH_ sequences that share an underscore. The escape regex _(x[\dA-Fa-f]{4})_ consumes the trailing underscore, so in _x0041_x0042_ only the first sequence is escaped. The cell is written as _x005F_x0041_x0042_, and both Excel and Query show _x0041B. The regex now matches only the leading underscore (_(?=x[\dA-Fa-f]{4}_)), giving _x005F_x0041_x005F_x0042_, which is what Excel writes for that text. A single _x0041_ is encoded as before.

Checked with Excel 365: I exported 28 strings (control characters, surrogates, _xHHHH_ variants, CR/LF, XML special characters) and read the cells through COM. On master two differ from the input (_x0041_x0042_ shows as _x0041B, cr\r\nlf loses the CR); with this change all 28 match.

Tests:

  • TestExportCarriageReturnAndEscapedUnderscores (inline and shared strings): checks the written XML and that Query returns the exported text
  • Issue149 (sync and async) no longer skips \r in the export round trips
  • TestIssue294NewLine expected the raw CR in the XML; it now expects _x000D_

On master 10 of these fail; with the change tests/MiniExcel.OpenXml.Tests (584) and tests/MiniExcel.Csv.Tests (92) pass on net8.0, net9.0, net10.0 and net11.0, and src/MiniExcel.OpenXml builds for netstandard2.0.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Excel export and import handling for carriage returns and text containing escaped sequences.
    • Carriage returns are now preserved when exporting, and values with these characters can round-trip through import without losing content.

EncodeString wrote \r as a raw character, and an XML parser reads a raw
CR or CRLF as LF, so the CR was gone when the file was opened in Excel or
read back with Query. Excel itself writes it as _x000D_, which is what the
encoder now does.

The _xHHHH_ escape consumed the trailing underscore, so in _x0041_x0042_
the second sequence was left unescaped and Excel showed _x0041B. The regex
now matches only the leading underscore, giving _x005F_x0041_x005F_x0042_
like Excel.
@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The XML string encoder now encodes carriage returns as _x000D_ and escapes the leading underscore in lowercase _xHHHH_ sequences. Tests verify the encoded output and round-trip values.

Changes: XML string encoding

Layer / File(s) Summary
Update XML encoding rules
src/MiniExcel.OpenXml/Helpers/XmlHelper.cs
The regex matches the underscore before lowercase _xHHHH_ sequences. EncodeString replaces that underscore with _x005F_ and encodes carriage returns as _x000D_.
Verify encoded output and round trips
tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs, tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesAsyncTests.cs
Tests check carriage-return and escaped-underscore encoding in inline and shared string storage, and verify exported values round-trip through the importer. Issue149 assertions now include index 13.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: michelebastione, shps951023

Merge Risk: Minimal · up to 15d29

Exports now preserve carriage returns and text that looks like _xHHHH_ sequences, with tests covering the written XML and round trips. I found no remaining merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the two export changes in the PR: carriage returns are kept (encoded as x000D) and shared-underscore xHHHH text is preserved on export. The raw summary and objectives both describe…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two export changes in the PR: carriage returns are kept (encoded as x000D) and shared-underscore xHHHH text is preserved on export. The raw summary and objectives both describe these same changes to XmlHelper.EncodeString and the related tests, so the title matches the main change. It is specific enough for a teammate scanning history to understand the primary change.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ff11a2de-721f-4ecc-b29e-cd0b86f45418
Commits

Reviewing files that changed from the base of the PR and between b9eb432 and 15d29a7.

Files selected for processing: 3

  • src/MiniExcel.OpenXml/Helpers/XmlHelper.cs
  • tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesAsyncTests.cs
  • tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant