Microsoft Access数据库引擎找不到对象'Sheet1$'。

问题描述 投票:1回答:2

我正在复制保存在服务器文件夹中的模板excel文件到同一文件夹中,并以不同的名称插入值......我能够复制文件,但当我试图插入值时,它显示sheet!$无法找到。我已经给了正确的工作表名称.只有一个表被添加在spread sheet命名为sheet1.仍然显示错误.我的代码是下面给出的.Nay想法这个错误.我google了,但要求我检查文件夹和工作表名称.它是正确的,只有...请帮助我。

        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();
c# asp.net ms-access-2007
2个回答
3
投票

替换。

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

替换为

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

0
投票

如果您仍然收到如下错误信息:"Microsoft Access数据库引擎无法找到该对象...",后面跟着您试图读取数据的工作表名称,那么请尝试[sheetname$]。

© www.soinside.com 2019 - 2024. All rights reserved.