Skip to content

Commit 14586c7

Browse files
committed
[Bug] Fix when CultureInfo like Czech will get invalid output with decimal numbers #331
1 parent a222e2a commit 14586c7

File tree

6 files changed

+92
-11
lines changed

6 files changed

+92
-11
lines changed

docs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
---
1818

19-
### 1.24.0
19+
### 1.23.2
2020
- [New] Support System.ComponentModel.DisplayName's `[DisplayName]` as title [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT)
21+
- [Bug] Fix when CultureInfo like `Czech` will get invalid output with decimal numbers #331
2122

2223
### 1.23.0
2324
- [New] Support `GetReader` method #328 #290 (Thanks [杨福来 Yang](https://github.com/yfl8910) )

docs/README.zh-CN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727

2828

29-
### 1.24.0
29+
### 1.23.2
30+
3031
- [New] 支持 System.ComponentModel.DisplayName 的 `[DisplayName]` 作为excel标题 [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT)
32+
- [Bug] 修正 `Czech` 等国家CultureInfo会生成错误 decimal 数字提示 #331
3133

3234
### 1.23.0
3335

docs/README.zh-Hant.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
---
1919

2020

21-
### 1.24.0
21+
### 1.23.2
2222
- [New] 支持 System.ComponentModel.DisplayName 的 `[DisplayName]` 作為excel標題 [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT)
23+
- [Bug] 修正 `Czech` 等國家CultureInfo會生成錯誤 decimal 數字提示 #331
2324

2425
### 1.23.0
2526
- [New] 新增 `GetReader` 方法 #328 #290 (感謝 [楊福來 Yang](https://github.com/yfl8910) )

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
4-
<Version>1.23.1</Version>
4+
<Version>1.23.2</Version>
55
</PropertyGroup>
66
<PropertyGroup>
77
<AssemblyName>MiniExcel</AssemblyName>

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,32 @@ private void WriteCell(StreamWriter writer, int rowIndex, int cellIndex, object
391391
else if (TypeHelper.IsNumericType(type))
392392
{
393393
if (_configuration.Culture != CultureInfo.InvariantCulture)
394-
{
395-
t = "str";
396-
v = ((decimal)value).ToString(_configuration.Culture);
397-
}
394+
t = "str"; //TODO: add style format
398395
else
399-
{
400396
t = "n";
401-
v = value.ToString();
402-
}
397+
398+
if (type.IsAssignableFrom(typeof(decimal)))
399+
v = ((decimal)value).ToString(_configuration.Culture);
400+
else if (type.IsAssignableFrom(typeof(Int32)))
401+
v = ((Int32)value).ToString(_configuration.Culture);
402+
else if (type.IsAssignableFrom(typeof(Double)))
403+
v = ((Double)value).ToString(_configuration.Culture);
404+
else if (type.IsAssignableFrom(typeof(Int64)))
405+
v = ((Int64)value).ToString(_configuration.Culture);
406+
else if (type.IsAssignableFrom(typeof(UInt32)))
407+
v = ((UInt32)value).ToString(_configuration.Culture);
408+
else if (type.IsAssignableFrom(typeof(UInt16)))
409+
v = ((UInt16)value).ToString(_configuration.Culture);
410+
else if (type.IsAssignableFrom(typeof(UInt64)))
411+
v = ((UInt64)value).ToString(_configuration.Culture);
412+
else if (type.IsAssignableFrom(typeof(Int16)))
413+
v = ((Int16)value).ToString(_configuration.Culture);
414+
else if (type.IsAssignableFrom(typeof(Single)))
415+
v = ((Single)value).ToString(_configuration.Culture);
416+
else if (type.IsAssignableFrom(typeof(Single)))
417+
v = ((Single)value).ToString(_configuration.Culture);
418+
else
419+
v = (decimal.Parse(value.ToString())).ToString(_configuration.Culture);
403420
}
404421
else if (type == typeof(bool))
405422
{

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,66 @@ public MiniExcelIssueTests(ITestOutputHelper output)
3131
this.output = output;
3232
}
3333

34+
[Fact]
35+
public void TestIssue331_2()
36+
{
37+
var cln = CultureInfo.CurrentCulture.Name;
38+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("cs-CZ");
39+
40+
var config = new OpenXmlConfiguration()
41+
{
42+
Culture = CultureInfo.GetCultureInfo("cs-CZ")
43+
};
44+
45+
var rnd = new Random();
46+
var data = Enumerable.Range(1, 100).Select(x => new TestIssue331Dto
47+
{
48+
Number = x,
49+
Text = $"Number {x}",
50+
DecimalNumber = (decimal)rnd.NextDouble(),
51+
DoubleNumber = rnd.NextDouble()
52+
});
53+
54+
var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
55+
MiniExcelLibs.MiniExcel.SaveAs(path, data, configuration: config);
56+
Console.WriteLine(path);
57+
58+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cln);
59+
}
60+
61+
[Fact]
62+
public void TestIssue331()
63+
{
64+
var cln = CultureInfo.CurrentCulture.Name;
65+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("cs-CZ");
66+
67+
var data = Enumerable.Range(1, 10).Select(x => new TestIssue331Dto
68+
{
69+
Number = x,
70+
Text = $"Number {x}",
71+
DecimalNumber = (decimal)x / (decimal)2,
72+
DoubleNumber = (double)x / (double)2
73+
});
74+
75+
var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
76+
MiniExcelLibs.MiniExcel.SaveAs(path, data);
77+
Console.WriteLine(path);
78+
79+
var rows = MiniExcel.Query(path,startCell:"A2").ToArray();
80+
Assert.Equal(1.5, rows[2].B);
81+
Assert.Equal(1.5, rows[2].C);
82+
83+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cln);
84+
}
85+
86+
public class TestIssue331Dto
87+
{
88+
public int Number { get; set; }
89+
public decimal DecimalNumber { get; set; }
90+
public double DoubleNumber { get; set; }
91+
public string Text { get; set; }
92+
}
93+
3494
[Fact]
3595
public void TestIssueI4TXGT()
3696
{

0 commit comments

Comments
 (0)