所以我有一个 excel 导入器,它从 excel 工作表中获取数据并填充一个数据表,然后我将它插入到一个 SQL 表中。我已经将它用于数以千计的导入,但对于这个工作表,它不会从工作表中提取单元格的正确值。当单元格实际包含“$4.32”时,我的函数返回类似“1/4/1900 7:42:25AM”的值,并为“每单位价格”的单元格标题值返回空值。单元格的公式类似于“=(J9*(1-K9)*(1-M9))”。整个列的格式为“货币”。
我不知道发生了什么,所以欢迎任何见解。
获取excel工作表的代码->DataTable:
DataTable dt;
try
{
if (FileInUse(filename))
{
e.Result = false;
Core.ShowExclamation("The file is already open. Please save and close the file before continuing.");
return;
}
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + @";Extended Properties=""Excel 12.0;HDR=NO;""");
string sheet = GetExcelSheetNames(con)[0];
OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + sheet + "]", con);
dt = new DataTable();
da.Fill(dt);
}
返回一些值的代码:
string foo = dt.Rows[5][13].ToString() + "\r\n";
foo += dt.Rows[6][13].ToString() + "\r\n";
foo += dt.Rows[7][13].ToString() + "\r\n";
foo += dt.Rows[8][13].ToString() + "\r\n";
foo += dt.Rows[9][13].ToString() + "\r\n";
foo += dt.Rows[10][13].ToString();
Core.ShowInfo(foo);
Returns: Should Returns:
12/30/1899 12:00:00 AM 0.00%
9/27/2013 12:00:00 AM 9/27/2013
{} Reseller Price Per Unit
1/4/1900 7:42:25 AM $4.32
1/4/1900 7:42:25 AM $4.32
3/10/1901 10:23:42 AM $435.43
最佳答案
最后从 Øyvind Knobloch-Bråthen 的提问中找到这篇文章
http://neosblogs.blogspot.com/2008/06/how-jet-ole-db-decide-excel-column-type.html
修复:
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + @";Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""");
只需设置“IMEX=1”
关于c# - Excel 返回日期而不是计算值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20102328/