Skip to content

Commit e9960cd

Browse files
authored
#2325 - Throws NotSupportedException if opening workbook saved in Strict OpenXML format (#2327)
1 parent a17f6d2 commit e9960cd

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/EPPlus/ExcelWorkbook.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,14 +1127,33 @@ public bool Date1904
11271127
}
11281128
}
11291129

1130+
private const string StrictSpreadsheetMainNamespace = "http://purl.oclc.org/ooxml/spreadsheetml/main";
1131+
1132+
// Call this when loading the workbook XML
1133+
private void ValidateWorkbookNamespace()
1134+
{
1135+
string defaultNamespace = _workbookXml.DocumentElement.NamespaceURI;
1136+
if (defaultNamespace == StrictSpreadsheetMainNamespace)
1137+
{
1138+
throw new NotSupportedException(
1139+
"The workbook is saved in the \"Strict Open XML Spreadsheet\" format, " +
1140+
"which is not supported by EPPlus. " +
1141+
"Please re-save the workbook as a regular \"Excel Workbook (.xlsx)\" " +
1142+
"in the Save As dialog and try again.");
1143+
}
1144+
}
1145+
11301146

11311147
/// <summary>
11321148
/// Create or read the XML for the workbook.
11331149
/// </summary>
11341150
private void CreateWorkbookXml(XmlNamespaceManager namespaceManager)
11351151
{
11361152
if (_package.ZipPackage.PartExists(WorkbookUri))
1153+
{
11371154
_workbookXml = _package.GetXmlFromUri(WorkbookUri);
1155+
ValidateWorkbookNamespace();
1156+
}
11381157
else
11391158
{
11401159
// create a new workbook part and add to the package

src/EPPlusTest/EPPlus.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@
250250
<None Update="Workbooks\SignedWorkbook1.xlsm">
251251
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
252252
</None>
253+
<None Update="Workbooks\StrictOpenXml.xlsx">
254+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
255+
</None>
253256
<None Update="Workbooks\SubtotalFilters.xlsx">
254257
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
255258
</None>

src/EPPlusTest/Issues/WorksheetIssues.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,5 +1130,16 @@ public void InsertAndShift_ShouldNotThrow_WhenArrayIsFull()
11301130
sheet.InsertColumn(5, 1);
11311131
}
11321132

1133+
[TestMethod]
1134+
public void Issue2325()
1135+
{
1136+
var ex = Assert.ThrowsExactly<NotSupportedException>(() =>
1137+
{
1138+
var package = OpenTemplatePackage("StrictOpenXml.xlsx");
1139+
var ws = package.Workbook.Worksheets.First();
1140+
});
1141+
Assert.IsTrue(ex.Message.Contains("Strict Open XML"));
1142+
}
1143+
11331144
}
11341145
}
8.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)