Skip to content

fix: Dynamic XML namespace prefixes for consistent Excel files#28

Merged
Shane32 merged 9 commits into
Shane32:masterfrom
TeaJAge:master
May 7, 2026
Merged

fix: Dynamic XML namespace prefixes for consistent Excel files#28
Shane32 merged 9 commits into
Shane32:masterfrom
TeaJAge:master

Conversation

@TeaJAge
Copy link
Copy Markdown

@TeaJAge TeaJAge commented Dec 19, 2025

Summary by CodeRabbit

  • Refactor
    • Updated worksheet XML serialization to consistently emit namespace-prefixed tags when saving. This improves how workbook files are written and parsed without changing public APIs or member signatures. No user-facing behavior or document formats are removed; files saved with this update may have namespace-qualified XML elements for better interoperability.

Thomas Herrmann added 2 commits December 2, 2025 15:29
…ility

Dynamically generated sheetData content was missing namespace prefixes,
causing XML inconsistency when worksheet root uses 'x:' prefix.

Symptoms: Excel opens the file with correct file size but displays an
empty worksheet - data is silently ignored due to malformed XML.
When saving, inconsistent namespace prefixes were used, resulting in
invalid XML documents.  The prefix is now dynamically determined from the
document and used consistently across all elements.

- Extract _nsPrefix from WorksheetXml. DocumentElement
- Replace all hardcoded "x:" prefixes with _nsPrefix
- Add using alias RegexMatch to resolve naming conflict
- Fix $"" combined with AppendFormat/string.Format calls
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 19, 2025

Review Change Stack

📝 Walkthrough

Walkthrough

ExcelWorksheet now computes and stores the worksheet XML namespace prefix and uses it when writing serialized worksheet XML tags; a Regex Match alias and using reorder were also added.

Changes

XML Namespace-Aware Serialization

Layer / File(s) Summary
Imports & Regex alias
EPPlus/ExcelWorksheet.cs
Reordered usings and added RegexMatch alias for System.Text.RegularExpressions.Match.
Private field
EPPlus/ExcelWorksheet.cs
Add private field _nsPrefix to hold the worksheet XML namespace prefix.
Compute prefix in SaveXml
EPPlus/ExcelWorksheet.cs
Add GetWorksheetNamespacePrefix() and set _nsPrefix in SaveXml before serializing worksheet blocks.
Regex alias usages
EPPlus/ExcelWorksheet.cs
Switch local regex Match usages to the RegexMatch alias in GetWorkSheetXml and GetBlockPos.
Merged cells serialization
EPPlus/ExcelWorksheet.cs
UpdateMergedCells emits <mergeCells>/<mergeCell> with _nsPrefix.
Row/column breaks & cols
EPPlus/ExcelWorksheet.cs
UpdateColBreaks, UpdateRowBreaks, and UpdateColumnData emit <colBreaks>, <rowBreaks>, <cols> and child elements with _nsPrefix.
SheetData and cell emission
EPPlus/ExcelWorksheet.cs
UpdateRowCellData and GetFormulaValue wrap <sheetData>, <row>, <c>, <f>, and <v> tags using _nsPrefix across formula and value paths.
WriteRow row emission
EPPlus/ExcelWorksheet.cs
WriteRow overloads emit <row ...> start and </row> end tags using _nsPrefix and updated row attributes.
Hyperlinks serialization
EPPlus/ExcelWorksheet.cs
UpdateHyperLinks writes <hyperlinks> and <hyperlink .../> with _nsPrefix and closes the container with the prefixed tag.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: implementing dynamic XML namespace prefixes in the ExcelWorksheet class to ensure consistent Excel file generation.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
EPPlus/ExcelWorksheet.cs (1)

3440-3449: Translate comment to English for consistency.

The comment on line 3440 is in German. For codebase consistency and maintainability, it should be in English.

Suggested fix
-            // Ermittle den Namespace-Prefix aus dem Dokument
+            // Determine the namespace prefix from the document
             var worksheetNode = _worksheetXml.DocumentElement;
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9428edb and 5a745df.

📒 Files selected for processing (1)
  • EPPlus/ExcelWorksheet.cs (27 hunks)
🔇 Additional comments (6)
EPPlus/ExcelWorksheet.cs (6)

58-58: LGTM!

The RegexMatch alias is a clean approach to avoid naming conflicts while maintaining code clarity.


375-375: LGTM!

The field is correctly initialized with an empty string default, ensuring consistent behavior for both prefixed and non-prefixed XML output.


1465-1472: LGTM!

The namespace prefix is correctly applied to the merged cells XML elements. The formatting with string interpolation is clean and readable.


3653-3653: LGTM!

The namespace prefix is consistently applied to all sheetData elements and closing tags. The string interpolation pattern $"<{_nsPrefix}sheetData>" correctly handles both prefixed and non-prefixed XML output.

Also applies to: 3784-3785


4064-4064: LGTM!

The namespace prefix is correctly integrated into all hyperlink XML elements while preserving the existing logic for handling internal and external hyperlinks.

Also applies to: 4073-4074, 4097-4102, 4110-4110


3873-3873: LGTM!

The namespace prefix is correctly applied to value elements while preserving the existing XML escaping logic.

Comment thread EPPlus/ExcelWorksheet.cs Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates ExcelWorksheet's worksheet XML serializer to emit worksheet content with a dynamic namespace prefix derived from the loaded worksheet document, so saved XML stays namespace-aware when a worksheet uses prefixed main-namespace elements.

Changes:

  • Added _nsPrefix detection in SaveXml based on the worksheet document element.
  • Updated streamed worksheet XML writing for rows, cells, formulas, merges, hyperlinks, column definitions, and page breaks to prepend the detected prefix.
  • Refactored a couple of Match references to use an alias after introducing a new namespace import.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread EPPlus/ExcelWorksheet.cs Outdated
Comment thread EPPlus/ExcelWorksheet.cs Outdated
Comment thread EPPlus/ExcelWorksheet.cs Outdated
Shane32 and others added 3 commits May 5, 2026 22:07
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread EPPlus/ExcelWorksheet.cs Outdated
@Shane32 Shane32 enabled auto-merge (squash) May 6, 2026 03:26
@Shane32 Shane32 disabled auto-merge May 7, 2026 04:09
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@EPPlus/ExcelWorksheet.cs`:
- Around line 3437-3451: The code collapses sheetData into a hardcoded
unprefixed element causing prefixed templates to lose their namespace; update
GetWorkSheetXml (and places that call CreateNode("d:sheetData")) to use the
actual prefix found by GetWorksheetNamespacePrefix or the stored _nsPrefix when
creating/replacing the empty sheetData node so the created node name is prefix +
"sheetData" (or ":sheetData" omitted if no prefix), and ensure you operate
on/replace the existing _worksheetXml sheetData node rather than inserting a new
unprefixed one.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7ff65796-9a63-423d-aca3-9f531c28b488

📥 Commits

Reviewing files that changed from the base of the PR and between 5a745df and 2f11099.

📒 Files selected for processing (1)
  • EPPlus/ExcelWorksheet.cs

Comment thread EPPlus/ExcelWorksheet.cs
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread EPPlus/ExcelWorksheet.cs
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
EPPlus/ExcelWorksheet.cs (1)

3437-3526: ⚡ Quick win

Add regression tests for both default and explicit worksheet namespace prefixes.

Given how many emitters now depend on _nsPrefix, add coverage for both <worksheet xmlns="..."> and <x:worksheet xmlns:x="..."> inputs to assert valid, single-instance sheetData/row/c/v/mergeCells/hyperlinks output.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@EPPlus/ExcelWorksheet.cs` around lines 3437 - 3526, Add regression tests that
cover both default and explicit worksheet namespace prefixes to exercise
GetWorksheetNamespacePrefix and SaveXml (which sets _nsPrefix). Create two test
inputs: one with <worksheet xmlns="..."> and one with <x:worksheet
xmlns:x="...">, call the SaveXml flow (or the public method that triggers
SaveXml) and assert the serialized output contains exactly one instance of each
emitted block (sheetData, row, c, v, mergeCells, hyperlinks, rowBreaks,
colBreaks) and that element names are correctly prefixed (or unprefixed)
matching _nsPrefix; ensure tests fail if duplicate blocks or incorrect prefixes
are emitted so future changes to GetWorksheetNamespacePrefix/SaveXml are
covered.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@EPPlus/ExcelWorksheet.cs`:
- Around line 3437-3526: Add regression tests that cover both default and
explicit worksheet namespace prefixes to exercise GetWorksheetNamespacePrefix
and SaveXml (which sets _nsPrefix). Create two test inputs: one with <worksheet
xmlns="..."> and one with <x:worksheet xmlns:x="...">, call the SaveXml flow (or
the public method that triggers SaveXml) and assert the serialized output
contains exactly one instance of each emitted block (sheetData, row, c, v,
mergeCells, hyperlinks, rowBreaks, colBreaks) and that element names are
correctly prefixed (or unprefixed) matching _nsPrefix; ensure tests fail if
duplicate blocks or incorrect prefixes are emitted so future changes to
GetWorksheetNamespacePrefix/SaveXml are covered.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6b630176-a887-4657-9910-e6a8d5e8e1e6

📥 Commits

Reviewing files that changed from the base of the PR and between 2f11099 and b4ac302.

📒 Files selected for processing (1)
  • EPPlus/ExcelWorksheet.cs

@Shane32 Shane32 merged commit 686684b into Shane32:master May 7, 2026
4 checks passed
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.

3 participants