Repository navigation
Description
Description
When a template sheet has a <sheetProtection> (or any other element the schema places between sheetData and mergeCells), SaveAsByTemplate writes <mergeCells> (and <phoneticPr>) before it. The CT_Worksheet sequence (ECMA-376 Part 1, §18.3.1.99) is:
sheetData, sheetCalcPr, sheetProtection, protectedRanges, scenarios, autoFilter, sortState,
dataConsolidate, customSheetViews, mergeCells, phoneticPr, conditionalFormatting, ...
so the filled file is invalid, and Excel refuses to open it (or offers to repair it). Most official/administrative forms are protected sheets with merged cells, so this hits every one of them.
The cause is in WriteSheetXml (1.47.1, src/MiniExcel/SaveByTemplate/ExcelOpenXmlTemplate.Impl.cs): after </sheetData> it writes the extracted autoFilter, then mergeCells, then phoneticPr, and only then the rest of the original sheet (contents[1]), which still contains sheetCalcPr, sheetProtection, protectedRanges, scenarios, sortState, dataConsolidate, customSheetViews. The same order is in src/MiniExcel.OpenXml/Templates/OpenXmlTemplate.Impl.cs on the main branch (checked by reading the code; not run on 2.0 previews). #917 fixed the place of autoFilter, not these.
Minimal reproduction
A hand-written template, so nothing else is involved — one sheet:
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<dimension ref="A1:C1"/>
<sheetData>
<row r="1"><c r="A1" t="inlineStr"><is><t>{{name}}</t></is></c><c r="B1" t="inlineStr"><is><t>merged</t></is></c><c r="C1"/></row>
</sheetData>
<sheetProtection sheet="1" objects="1" scenarios="1"/>
<mergeCells count="1"><mergeCell ref="B1:C1"/></mergeCells>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>(plus the usual [Content_Types].xml, _rels/.rels, xl/workbook.xml, xl/_rels/workbook.xml.rels), then:
using var output = File.Create("filled.xlsx");
output.SaveAsByTemplate(templateBytes, new Dictionary<string, object> { ["name"] = "Jack" });Children of xl/worksheets/sheet1.xml:
template: dimension, sheetData, sheetProtection, mergeCells, pageMargins
filled: dimension, sheetData, mergeCells, sheetProtection, pageMargins
Expected Behavior
The filled sheet keeps the schema order: sheetData, sheetProtection, mergeCells, ..., and opens in Excel like the template does.
Actual Behavior
mergeCells comes before sheetProtection. Excel (Microsoft 365, 16.0, Windows) opens template.xlsx but not filled.xlsx; through COM with alerts off, Workbooks.Open fails. Moving <sheetProtection/> back to right after </sheetData> in filled.xlsx (nothing else changed) makes it open. The Open XML SDK validator (DocumentFormat.OpenXml 3.1.1) reports Sch_UnexpectedElementContentExpectingComplex: The element has unexpected child element '…main:sheetProtection'. for the filled file and nothing for the template.
Workarounds
Post-process the filled file: stable-sort the children of each <worksheet> by the CT_Worksheet sequence (unknown elements such as mc:AlternateContent keep their place after the element before them). A fix inside WriteSheetXml could instead write the elements of contents[1] that the schema places before autoFilter/mergeCells ahead of them.
Environment
- MiniExcel Version: 1.47.1 (the same code on main, 2026-11-27)
- .NET Version: net10.0
- OS: Windows 11
Files and/or Screenshots
The template above is the whole reproduction; I can attach template.xlsx / filled.xlsx if useful.
Activity