简体   繁体   中英

The Microsoft Access database engine could not find the object 'Sheet1$

I am copying the template excel file saved in the server folder in to the same folder with different name. to insert the value..I am able to copy the file but when I try to insert the values it shows sheet!$ could not found. I have given correct sheet name..Only one sheet is added in the spread sheet named as sheet1.still it shows error .My code is given below.Nay idea about this error .I googled but asked me to check the folder and sheet name..it is correct only..please help me

        string xxx = "~/temp/" + "Tempfile" + dunsno + DateTime.Today.ToString("dd.MM.yyyy") + ".xlsx";
         DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("~/temp/"));
                            var fileList = directoryInfo.GetFiles();
                            string newFileName = Server.MapPath("~/temp/" + "Tempfile" + dunsno + DateTime.Today.ToString("dd.MM.yyyy") + ".xlsx");
                            foreach (FileInfo fleInfo in fileList     

                       {
                                fleInfo.CopyTo(newFileName, true);
                       }
         string connStr = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xxx;Extended Properties=""Excel 12.0 Xml;HDR=YES""");
         OleDbConnection MyConnection;
                            OleDbCommand MyCommand = new OleDbCommand();
                            MyConnection = new OleDbConnection(@connStr);

                            MyConnection.Open();
                            MyCommand.Connection = MyConnection;
                            string sql = "Insert into [Sheet1$] (id,name) values('3','c')";
                            MyCommand.CommandText = sql;
                            MyCommand.ExecuteNonQuery();
                            MyConnection.Close();

Replace:

string connStr = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xxx;Extended Properties=""Excel 12.0 Xml;HDR=YES""");

with

string connStr = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES""", newFileName);

如果您仍然收到如下错误“Microsoft Access 数据库引擎找不到对象...”,后跟您尝试从中读取数据的工作表名称,然后尝试 [工作表名称 $]。

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