0

我正在尝试使用以下代码加载数据。

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 驱动器中,但仍然出现相同的错误。

4

2 回答 2

0

这有效(我从一个“虚拟”路径开始,然后应用真正的运行时路径):

OleStringBuilder =
        new OleDbConnectionStringBuilder(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");

OleStringBuilder.DataSource = MapPath(@"~\App_Data\MyExcelWorksheet.xls");
于 2012-08-20T15:25:55.897 回答
0

你可以在这里下载最新版本的 Jet

http://www.microsoft.com/en-us/download/search.aspx?q=jet

于 2012-08-20T15:17:04.997 回答