c# - Microsoft Access 数据库引擎找不到对象“Sheet1$”

标签 c# asp.net ms-access-2007

我正在将保存在服务器文件夹中的模板 excel 文件复制到具有不同名称的同一文件夹中。插入值..我能够复制文件但是当我尝试插入值时它显示工作表!找不到$。我已经给出了正确的工作表名称。在名为 sheet1 的电子表格中只添加了一张工作表。它仍然显示错误。我的代码在下面给出。关于这个错误的想法。我用谷歌搜索但要求我检查文件夹和工作表名称..这是正确的..请帮助我

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

最佳答案

替换:

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

关于c# - Microsoft Access 数据库引擎找不到对象“Sheet1$”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756759/

相关文章:

c# - 覆盖Windows 8中的证书验证

asp.net - UpdatePanel 内按钮的 OnClientClick 事件出现问题

c# - 如何在 PagedList.MVC 中自定义设计?

c# - 你能防止 WPF 样式被代码覆盖吗?

c# - 没有隐式引用 'card' 从 'card' 到 IComparable 的转换

asp.net - 如何在RadioButtonList之间添加项目?

sql - MS Access FE/SQL BE 保存后需要自动编号填充到表单上

java - 我需要连接我的 java 应用程序来访问 Windows 7 x64 中的数据库?

sql - 将 WHERE 语句添加到引用 ODBC 链接的子查询时访问查询失败

c# - 如何在接口(interface)或基类中定义一个固定大小的数组