OleDB的Jet – Float读取Excel数据的问题

当我使用OleDbDataReader将表单读入一个DataTable时,浮点数字将失去精度。

我试图迫使OleDb作为string读取Excel数据,但是虽然数据现在包含在一个DataRow中,每个列定义为System.String它失去了精度(18.125 – > 18.124962832)。

任何想法如何避免这种行为?

我只是testing你的数据,这个贴在这里的方法工作。
即放入DataSet时,单元格值保持精度18.124962832。

我很确定,Jet试图根据它在前五行中看到的内容为每列分配一个数据types。 如果在前五行之后的东西不属于这种数据types,它将会转换它或者什么也不返回。

电子表格的前五行是否比开始截断的项目具有更低的精度?

看看这个post。

以下代码的输出显示了如何使用SpreadsheetGear for .NET获取基础编号和格式化文本:

这里是代码的输出:

x=18.124962832 y=18.124962832 formattedText=18.125 

这里是代码:

 namespace Program { class Program { static void Main(string[] args) { // Create a new workbook and get a reference to Sheet1!A1. var workbook = SpreadsheetGear.Factory.GetWorkbook(); var sheet1 = workbook.Worksheets[0]; var a1 = workbook.Worksheets[0].Cells["A1"]; // Put the number in the cell. double x = 18.124962832; a1.Value = x; a1.NumberFormat = "0.000"; double y = (double)a1.Value; string formattedText = a1.Text; System.Console.WriteLine("x={0} y={1} formattedText={2}", x, y, formattedText); } } } 

您可以在这里看到实时的SpreadsheetGear示例,并在此处下载免费试用版。

免责声明:我自己的SpreadsheetGear LLC