0

我有一个可以下载 excel 文件的代码,它会选择其中的所有数据。这是我的代码:

string conn = string.Empty;
            if (Path.GetExtension(path).ToLower().Equals(".xls"))
            {
                conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                                + path +
                                ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
            }
            else
            {
                conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
                              + path +
                              ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
            }

            string strselect = "Select * from [Sheet1$]";

            using (OleDbConnection excelCon = new OleDbConnection(conn))
            {
                try
                {
                    excelCon.Open();
                    using (OleDbDataAdapter exDA = new OleDbDataAdapter(strselect, excelCon))
                    {
                        exDA.Fill(_data);
                    }
                }
                catch (OleDbException oledb)
                {
                    throw new Exception(oledb.Message.ToString());
                }
                finally
                {
                    excelCon.Close();
                }
            }

当我调试它时,在excelCon.Open();我的 Catch 中传输并得到一个 OleDbException:“Microsoft Jet 数据库引擎无法打开文件''。它已经由另一个用户以独占方式打开,或者您需要查看其数据的权限。”

你能指出什么是错的吗?我没有打开的文件,我检查了它的设置,但任何人都可以打开它。你们中有人遇到过这种错误吗?

4

0 回答 0