[Bug] Fix when CultureInfo like Czech will get invalid output with decimal numbers #331

This commit is contained in:
Wei 2022-03-03 11:42:46 +08:00
parent a222e2a4f8
commit 14586c739c
6 changed files with 92 additions and 11 deletions

View File

@ -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) )

View File

@ -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

View File

@ -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) )

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
<Version>1.23.1</Version>
<Version>1.23.2</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniExcel</AssemblyName>

View File

@ -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))
{

View File

@ -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()
{