c# - 将 Excel 读取到数据表而不会丢失货币类型的精度

标签 c# asp.net .net vb.net

我正在尝试将 Excel 工作表数据读取到数据表以绑定(bind)到 GridView。我的 Excel 工作表包含如下数据,

ID    Value1                    Value2
-------------------------------------------------
1     $312976.97530297          $30790.0614862584
etc

我正在使用以下代码将值读取到数据表中。

DataTable table = new DataTable();
string filePath = @"D:\Book1.xlsx";
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"", filePath);
using (OleDbConnection dbConnection = new OleDbConnection(strConn)) 
{
using (OleDbDataAdapter dbAdapter = new OleDbDataAdapter("SELECT ID, value1,value2  FROM [Sheet1$]", dbConnection))
dbAdapter.Fill(table);
}

问题:由于 value1 和 value2 包含 $ 符号,dataAdaptor 检索两个值时不完全精确,即。而不是 312976.97530297,它只返回 312976.9753。

Note: 
1. I cannot change the input Excel sheet since the enduser will upload it to the web site.
2. If i remove the $ symbol in the excel sheet, it will return the full precision, but $ also present in the input sheet.
3. I tried using Microsoft.Office.Interop.Excel and its working, but the performance is very low.


Or I can format all the excel cell as Text/General type before filling to the datatable, Anyone knows how to do that?

请推荐一种使用 OleDbDataAdapter 的方法。

提前致谢, 威尔逊。

最佳答案

也许你应该给http://epplus.codeplex.com/releases/view/79802一试。它可以非常快地读取数据,至少与互操作相比是这样。此外,互操作性对于 Web 服务来说不够可靠。

对于你的问题:也许有办法逃避“$”

Microsoft 建议在货币的情况下使用 double 或 float 据类型。你使用什么格式?

关于c# - 将 Excel 读取到数据表而不会丢失货币类型的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11878553/

相关文章:

c# - 如何从客户端机器获取 CultureInfo?

c# - 单色触摸 : UITabBarController throws 'loaded the nib but the view outlet was not set'

javascript - Json序列化: how to send c# data to javascript script.错误:无效字符

C# ASP.Net 5 配置和向后兼容类库

asp.net - 我如何让中继器的Itemcommand事件不执行整页回发?

c# - 如何使用 DOT 文件导出数据?

c# - 类签名中的通用约束推断

c# - 如何防止窗口在 .NET 中显示?

c# - 日期时间 Jquery datetimepicker

c# - 从 StackTrace 中过滤掉非用户代码方法