【问题标题】:Losing precision while reading value from Excel从 Excel 读取值时丢失精度
【发布时间】:2015-01-23 07:58:04
【问题描述】:

我需要使用 OleDb Connection 将值从 Excel 读取到 DataSet。 下面是我的代码。

OleDbCommand excelCommand = new OleDbCommand(); 
OleDbDataAdapter    excelDataAdapter = new OleDbDataAdapter();
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=" + filelocation + @";Extended Properties=""Excel 8.0; IMEX=1;HDR=NO;""";
OleDbConnection excelConn = new OleDbConnection(connectionString);
excelConn.Open();
excelCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", excelConn);
excelDataAdapter.SelectCommand = excelCommand;
excelDataAdapter.Fill(ds);

在阅读时,我的单元格中的精度正在下降。

例如: Excel 单元格的值为 -0.46256613817052

我的数据集的值为 -0.4625661

我在 Excel 工作表中的大部分单元格都是数字类型。

如何解决这个问题?请帮忙。

【问题讨论】:

  • excel列是什么类型的?
  • 我的 Excel 工作表包含所有类型的列。但大多数列都是数字类型。
  • 很高兴您找到了 Dinesh 问题的解决方案。同样作为参考,您在连接字符串中使用了IMEX=1,尽管您说您没有。
  • 哦,对不起...我试过没有 IMEX=1。但我错过了从我发布的连接字符串中删除它。

标签: c# excel oledb


【解决方案1】:

根据Microsoft.Jet.OLEDB.4.0 rounding excel data?的这个答案,如果你需要得到准确的值,你需要使用visual studio tools for office。

我已经使用Excel Data Reader/ 解决了这个问题

【讨论】:

    猜你喜欢
    • 2020-11-22
    • 2016-09-26
    • 2018-03-11
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    相关资源
    最近更新 更多