C# 从Excel工作表中读取数字会改变值

C# 从Excel工作表中读取数字会改变值,c#,excel,xslt,openxml-sdk,C#,Excel,Xslt,Openxml Sdk,我有一个C程序,它读取excel工作簿,然后构建一个可以针对XSLT运行的XML文件。出现的问题是其中一个字段是一个数字,从excel工作表中读取时,该值正在更改。以下是一个例子: excel电子表格被读入,数据被加载到数据表中。我这样做的方法之一是使用我创建的电子表格文档,并将单元格引用传递到此方法中: dataRow[columnIndex] = GetCellValue(spreadSheetDocument, cell); private static string GetCellVa

我有一个C程序,它读取excel工作簿,然后构建一个可以针对XSLT运行的XML文件。出现的问题是其中一个字段是一个数字,从excel工作表中读取时,该值正在更改。以下是一个例子:

excel电子表格被读入,数据被加载到数据表中。我这样做的方法之一是使用我创建的电子表格文档,并将单元格引用传递到此方法中:

dataRow[columnIndex] = GetCellValue(spreadSheetDocument, cell);

private static string GetCellValue(SpreadsheetDocument document, Cell cell)
    {
        //This process uses the OpenXML SDK to get individual cells values to populate the DataTable
        SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;
        string value = "";
        //One of the things that needed to be accounted for was empty cells
        try
        {
            value = cell.CellValue.InnerXml;
        }
        catch (Exception)
        {
            value = "";
        }
        //Setting cell data type right now just sets everything to strings
        //Later, the better option will be to work on the Date Conversions and Data Types here
        if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
        {
            return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
        }
        else
        {
            return value;
        }
    }
例如,如果读取的单元格为115,则输出如下:

114.99999999999999
125.00000000000001
然后在其他时间,如果值为125,则输出如下所示:

114.99999999999999
125.00000000000001

输出中的不一致性有点令人困惑。我希望能够深入了解造成这种情况的原因,而不是稍后在XSLT中修复它。

因此我找到了一种解决方法,而不是实际的解决方案。显然,这是OpenXMLSDK中的一个bug。我找到了指向这个方向的初始文档

我发现的解决方法是:

private static string GetCellValue(SpreadsheetDocument document, Cell cell)
    {
        //This process uses the OpenXML SDK to get individual cells values to populate the DataTable
        SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;
        string value = "";
        //One of the things that needed to be accounted for was empty cells
        try
        {
            value = cell.CellValue.InnerXml;
        }
        catch (Exception)
        {
            value = "";
        }
        //Checking to see if this string contains a decimal with values on either side
        if (Regex.IsMatch(value, regexpattern))
        {
            value = Math.Round(Double.Parse(value), 0, MidpointRounding.AwayFromZero).ToString();
        }
        //Setting cell data type right now just sets everything to strings
        //Later, the better option will be to work on the Date Conversions and Data Types here
        if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
        {
            return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
        }
        else
        {
            return value;
        }
    }
我使用正则表达式来确定是否遇到了这个bug,然后使用一些舍入进行补偿。有趣的是,只有整数才会出现这种情况


谢谢

可能是在Excel方面吗?如果114.999999是计算结果或存储结果,Excel会将其显示为舍入值,但存储和计算的精度更高。这是我首先检查的。在处理之前,我在excel工作表中手动输入了115的值。