简体   繁体   中英

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"

i am using OLEDB server to read excel file in my ASP.NET MVC project. then im getting error "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". same code with different comnnection string of CSV file is work fine but For Excel connectionString i am getting this error. is there anyone know the solution for this please.

My code is:

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);
            }
}

commented connectionstring is for CSV file and CSV file works fine with same code.

I had the same problem with a program I was recently working on. My solution was to simply take out the extra bits from the connection string. I have no idea why it worked but since it worked for me, it might work for you. This is what your new connection string would look like:

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

You actually might be able to keep the HDR=Yes bit just because I understand that you might need that to tell it there are headers and I don't think leaving it in would affect it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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