繁体   English   中英

Microsoft Jet数据库引擎无法打开文件“。它已经由另一个用户独占打开,或者您需要获得查看其数据的权限”

[英]The Microsoft Jet database engine cannot open the file ''.It is already opened exclusively by another user, or you need permission to view its data"

我正在使用OLEDB服务器读取ASP.NET MVC项目中的excel文件。 然后出现错误“ Microsoft Jet数据库引擎无法打开文件”。它已经由另一个用户专门打开,或者您需要获得查看其数据的权限”。 相同的代码与CSV文件的不同连接字符串可以正常工作,但是对于Excel connectionString,我收到此错误。 请问有人知道解决方案吗?

我的代码是:

public JsonResult ImportCSVFiles()
        {

            HttpPostedFileBase hpf = null;
            foreach (string file in Request.Files)
            {
                hpf = Request.Files[file] as HttpPostedFileBase;
            }
            string[] FileName;
            string filename = hpf.FileName;

            string DestinationPath = Server.MapPath("..") + "\\CSVFiles\\";

            if (!Directory.Exists(DestinationPath))
            {
                Directory.CreateDirectory(DestinationPath);

                if (System.IO.File.Exists(Server.MapPath("..") + "\\CSVFiles\\" + filename) == false)
                {
                    hpf.SaveAs(DestinationPath + filename);
                }
                else
                {
                    System.IO.File.Delete(Server.MapPath("..") + "\\CSVFiles\\" + filename);
                    hpf.SaveAs(DestinationPath + filename);
                }

            }
            else
            {

                if (System.IO.File.Exists(Server.MapPath("..") + "\\CSVFiles\\" + filename) == false)
                {
                    hpf.SaveAs(DestinationPath + filename);
                }
                else
                {
                    System.IO.File.Delete(Server.MapPath("..") + "\\CSVFiles\\" + filename);
                    hpf.SaveAs(DestinationPath + filename);
                }
            }

            bool isFirstRowHeader = true;
            string header = isFirstRowHeader ? "Yes" : "No";
            string path = "";
            string pathOnly = Path.GetDirectoryName(DestinationPath);
            string fileName = Path.GetFileName(DestinationPath + "\\" + filename);


            string sql = @"SELECT * FROM [" + fileName + "]";

            //using (OleDbConnection connection = new OleDbConnection(
            //          @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
            //          ";Extended Properties=\"Text;HDR=" + header + "\""))

            using (OleDbConnection connection = new OleDbConnection(
                  @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                  ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";"))


            using (OleDbCommand command = new OleDbCommand(sql, connection))
            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
            {
                DataTable dataTable = new DataTable();

                dataTable.Locale = CultureInfo.CurrentCulture;
                adapter.Fill(dataTable);
            }
}

带注释的连接字符串适用于CSV文件,并且CSV文件在相同代码下可以正常工作。

我最近正在研究的程序也有同样的问题。 我的解决方案是简单地从连接字符串中取出多余的位。 我不知道它为什么起作用,但是由于它对我有用,因此可能对您有用。 这是您新的连接字符串的样子:

using (OleDbConnection connection = new OleDbConnection(
              @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";"))

实际上,您可能能够将HDR = Yes保持为仅是因为我了解您可能需要告诉它有标头,并且我认为将其保留不会影响它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM