Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打开XML SDK将双值写入Excel单元格_C#_.net_Excel_Openxml - Fatal编程技术网

C# 打开XML SDK将双值写入Excel单元格

C# 打开XML SDK将双值写入Excel单元格,c#,.net,excel,openxml,C#,.net,Excel,Openxml,我正在将SQL数据库中的值转换为Excel工作表。我可以写字符串和整数没有问题,但当我写双倍Excel需要修复文档。它说数字的格式是文本 我使用以下代码: double val = Math.Round(reader.GetDouble(3),2); cell.CellValue= new CellValue(Convert.ToString(val)); cell.DataType = new EnumValue<CellValues>(CellValues.Number); d

我正在将SQL数据库中的值转换为Excel工作表。我可以写字符串和整数没有问题,但当我写双倍Excel需要修复文档。它说数字的格式是文本

我使用以下代码:

double val = Math.Round(reader.GetDouble(3),2);
cell.CellValue= new CellValue(Convert.ToString(val));
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
double val=Math.Round(reader.GetDouble(3,2);
cell.CellValue=新的CellValue(Convert.ToString(val));
cell.DataType=新的枚举值(CellValues.Number);
请尝试使用:

cell.CellValue = new CellValue(val.ToString(CultureInfo.InvariantCulture));
而不是:

cell.CellValue= new CellValue(Convert.ToString(val));

使用CultureInfo.InvariantCulture不会覆盖所有情况非常大或非常小的数字,NaN和+-Inf仍会触发Excel来修复文件。目前,我将此作为一种解决方法:

 bool isNumeric = IsNumeric(value, out double number);
            if (isNumeric && ! (double.IsNaN(number) | double.IsInfinity(number) | Math.Abs(number) >1e100))
            {
                cell.DataType = CellValues.Number;
                cell.CellValue = new CellValue(String.Format(CultureInfo.InvariantCulture, "{0:0.####################}", number));
            }
            else
            {
                cell.DataType = CellValues.String;
                cell.CellValue = new CellValue(value.ToString());
            }
有更好的解决办法吗