From 14586c739c996438f9a63d0109000c7aa65277cd Mon Sep 17 00:00:00 2001 From: Wei Date: Thu, 3 Mar 2022 11:42:46 +0800 Subject: [PATCH] [Bug] Fix when CultureInfo like `Czech` will get invalid output with decimal numbers #331 --- docs/README.md | 3 +- docs/README.zh-CN.md | 4 +- docs/README.zh-Hant.md | 3 +- src/MiniExcel/MiniExcelLibs.csproj | 2 +- .../OpenXml/ExcelOpenXmlSheetWriter.cs | 31 +++++++--- tests/MiniExcelTests/MiniExcelIssueTests.cs | 60 +++++++++++++++++++ 6 files changed, 92 insertions(+), 11 deletions(-) diff --git a/docs/README.md b/docs/README.md index f35d4ba..5ba6614 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,8 +16,9 @@ --- -### 1.24.0 +### 1.23.2 - [New] Support System.ComponentModel.DisplayName's `[DisplayName]` as title [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT) +- [Bug] Fix when CultureInfo like `Czech` will get invalid output with decimal numbers #331 ### 1.23.0 - [New] Support `GetReader` method #328 #290 (Thanks [杨福来 Yang](https://github.com/yfl8910) ) diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index 413a338..4543123 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -26,8 +26,10 @@ -### 1.24.0 +### 1.23.2 + - [New] 支持 System.ComponentModel.DisplayName 的 `[DisplayName]` 作为excel标题 [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT) +- [Bug] 修正 `Czech` 等国家CultureInfo会生成错误 decimal 数字提示 #331 ### 1.23.0 diff --git a/docs/README.zh-Hant.md b/docs/README.zh-Hant.md index cff7238..3c62e23 100644 --- a/docs/README.zh-Hant.md +++ b/docs/README.zh-Hant.md @@ -18,8 +18,9 @@ --- -### 1.24.0 +### 1.23.2 - [New] 支持 System.ComponentModel.DisplayName 的 `[DisplayName]` 作為excel標題 [#I4TXGT](https://gitee.com/dotnetchina/MiniExcel/issues/I4TXGT) +- [Bug] 修正 `Czech` 等國家CultureInfo會生成錯誤 decimal 數字提示 #331 ### 1.23.0 - [New] 新增 `GetReader` 方法 #328 #290 (感謝 [楊福來 Yang](https://github.com/yfl8910) ) diff --git a/src/MiniExcel/MiniExcelLibs.csproj b/src/MiniExcel/MiniExcelLibs.csproj index e21df68..53b09bc 100644 --- a/src/MiniExcel/MiniExcelLibs.csproj +++ b/src/MiniExcel/MiniExcelLibs.csproj @@ -1,7 +1,7 @@  net45;netstandard2.0;net5.0 - 1.23.1 + 1.23.2 MiniExcel diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index ae661ae..56195c4 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -391,15 +391,32 @@ namespace MiniExcelLibs.OpenXml else if (TypeHelper.IsNumericType(type)) { if (_configuration.Culture != CultureInfo.InvariantCulture) - { - t = "str"; - v = ((decimal)value).ToString(_configuration.Culture); - } + t = "str"; //TODO: add style format else - { t = "n"; - v = value.ToString(); - } + + if (type.IsAssignableFrom(typeof(decimal))) + v = ((decimal)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(Int32))) + v = ((Int32)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(Double))) + v = ((Double)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(Int64))) + v = ((Int64)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(UInt32))) + v = ((UInt32)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(UInt16))) + v = ((UInt16)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(UInt64))) + v = ((UInt64)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(Int16))) + v = ((Int16)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(Single))) + v = ((Single)value).ToString(_configuration.Culture); + else if (type.IsAssignableFrom(typeof(Single))) + v = ((Single)value).ToString(_configuration.Culture); + else + v = (decimal.Parse(value.ToString())).ToString(_configuration.Culture); } else if (type == typeof(bool)) { diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 4ff47d6..12a87eb 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -31,6 +31,66 @@ namespace MiniExcelLibs.Tests this.output = output; } + [Fact] + public void TestIssue331_2() + { + var cln = CultureInfo.CurrentCulture.Name; + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("cs-CZ"); + + var config = new OpenXmlConfiguration() + { + Culture = CultureInfo.GetCultureInfo("cs-CZ") + }; + + var rnd = new Random(); + var data = Enumerable.Range(1, 100).Select(x => new TestIssue331Dto + { + Number = x, + Text = $"Number {x}", + DecimalNumber = (decimal)rnd.NextDouble(), + DoubleNumber = rnd.NextDouble() + }); + + var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx"; + MiniExcelLibs.MiniExcel.SaveAs(path, data, configuration: config); + Console.WriteLine(path); + + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cln); + } + + [Fact] + public void TestIssue331() + { + var cln = CultureInfo.CurrentCulture.Name; + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("cs-CZ"); + + var data = Enumerable.Range(1, 10).Select(x => new TestIssue331Dto + { + Number = x, + Text = $"Number {x}", + DecimalNumber = (decimal)x / (decimal)2, + DoubleNumber = (double)x / (double)2 + }); + + var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx"; + MiniExcelLibs.MiniExcel.SaveAs(path, data); + Console.WriteLine(path); + + var rows = MiniExcel.Query(path,startCell:"A2").ToArray(); + Assert.Equal(1.5, rows[2].B); + Assert.Equal(1.5, rows[2].C); + + CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cln); + } + + public class TestIssue331Dto + { + public int Number { get; set; } + public decimal DecimalNumber { get; set; } + public double DoubleNumber { get; set; } + public string Text { get; set; } + } + [Fact] public void TestIssueI4TXGT() {