【发布时间】:2017-08-04 21:04:00
【问题描述】:
我使用 excel 库将 Dictionary<string, List<string>> 转换为 excel 文件。字典中的值保存为字符串,但它们实际上可以是双精度数或字符串。假设 myDic 是一个示例字典。例如 myDic[0]:
[0] "\"Nm\""string
[1] "1,0" string
[2] "2,0" string
[3] "3,2" string
[4] "0,0" string
[5] "0,0" string
[6] "0,0" string
[7] "0,0" string
[8] "0,0" string
[9] "0,0" string
[10] "0,0" string
在将其保存到 excel 时,如果它们实际上是数字,我想将它们转换为数字。 我使用了这种方法,但似乎大错特错:
try
{
row.CreateCell(j).SetCellValue(Double.Parse(cellValue));
}
catch (Exception e)
{
row.CreateCell(j).SetCellValue(cellValue);
}
这非常慢,因为它会在大多数单元格上引发异常。此外,双精度值设置错误,我认为这是因为我的数字(德国数字)中有逗号,例如 1,0 保存为 10。
所以似乎有两个问题。第一:如何动态正确地转换单元格值的类型。第二:如何正确保存带逗号的数字。
有人可以帮忙吗。
【问题讨论】:
-
Faster 是使用
TryParse。 -
看来 double.Parse 会抛出一个
FormatException。试试这个代码:double.Parse(cellValue, System.Globalization.CultureInfo.InvariantCulture)
标签: c# excel type-conversion