Skip to content

Commit fa7b9a9

Browse files
committed
Fix Excel style.xml duplicate numFmtId, system error: An item with the same key has already been added. #772
1 parent b198816 commit fa7b9a9

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

samples/xlsx/TestIssue772.xlsx

7.87 MB
Binary file not shown.

src/MiniExcel/OpenXml/ExcelOpenXmlStyles.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using MiniExcelLibs.Zip;
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56

67
namespace MiniExcelLibs.OpenXml
78
{
@@ -83,7 +84,8 @@ public ExcelOpenXmlStyles(ExcelOpenXmlZip zip)
8384
type = typeof(DateTime?);
8485
}
8586

86-
_customFormats.Add(numFmtId, new NumberFormatString(formatCode, type));
87+
if (!_customFormats.ContainsKey(numFmtId))
88+
_customFormats.Add(numFmtId, new NumberFormatString(formatCode, type));
8789
reader.Skip();
8890
}
8991
else if (!XmlReaderHelper.SkipContent(reader))

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
using Xunit.Abstractions;
2020
using static MiniExcelLibs.Tests.MiniExcelOpenXmlTests;
2121
using MiniExcelLibs.Picture;
22+
using DocumentFormat.OpenXml.Spreadsheet;
23+
using NPOI.Util;
24+
using TableStyles = MiniExcelLibs.OpenXml.TableStyles;
2225

2326
namespace MiniExcelLibs.Tests;
2427

@@ -4155,7 +4158,6 @@ public void TestIssue768()
41554158
}
41564159
/// <summary>
41574160
/// https://github.com/mini-software/MiniExcel/issues/186
4158-
/// Optimize CleanXml method #751
41594161
/// </summary>
41604162
[Fact]
41614163
public void TestIssue186()
@@ -4183,4 +4185,15 @@ public void TestIssue186()
41834185
};
41844186
MiniExcel.AddPicture(path, images);
41854187
}
4188+
4189+
/// <summary>
4190+
/// https://github.com/mini-software/MiniExcel/issues/772
4191+
/// </summary>
4192+
[Fact]
4193+
public void TestIssue772()
4194+
{
4195+
var path = PathHelper.GetFile("xlsx/TestIssue772.xlsx");
4196+
var rows = MiniExcel.Query(path, sheetName: "Supply plan(daily)", startCell:"A1").Cast<IDictionary<string, object>>().ToArray();
4197+
Assert.Equal("01108083-1Delta", (string)rows[19]["C"]);
4198+
}
41864199
}

0 commit comments

Comments
 (0)