【发布时间】:2012-08-20 15:12:28
【问题描述】:
我正在尝试使用以下代码加载数据。
string path = System.IO.Path.GetFullPath(uploadExcelFile.PostedFile.FileName);
string connString = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Rizwan shahid\\Desktop\\DataUpload\\Req.xls;Extended Properties=Excel 12.0;";
OleDbConnection oledbConn = new OleDbConnection(connString);
try
{
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
DataSet ds = new DataSet();
oleda.Fill(ds, "Table");
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
oledbConn.Close();
}
它在 32 位操作系统上运行,但在 64 位操作系统上运行此代码时出现以下错误
The Microsoft Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly. If 'Sheet1$' is not a local object, check your network connection or contact the server administrator.
我在管理员模式下运行 VS 并找到了许多解决方案,例如将 Sheet1 替换为文件名或将文件放在 C 驱动器中,但仍然出现相同的错误。
【问题讨论】:
-
catch { 返回 null; } 真的?你不关心错误是什么吗?
-
@SteveWellens 你知道我的问题吗?
-
我听说过/读过 一些 实例,如果源 .xls 文件以某种方式不可用、打开、锁定或无法访问...虽然理想情况下,如果是这种情况,您会/应该收到不同的消息。只是一个想法,可能是一个远景......我们确定 xls 文件在 64 位机器上的相同/正确位置吗?
-
@SteveWellens 你知道我的问题吗? – user1387147 是的,我阅读了您的帖子。然后我对您的代码发表了评论。
标签: c# asp.net asp.net-mvc ms-access oledb