在 C# 中从 Excel 读取时出错:“...找不到对象...”
我或多或少有与 这篇文章 中完全相同的错误,但该解决方案有没有解决我的问题。
我收到的错误消息:
The Microsoft Office Access database engine could not find the object 'Adresser$'. Make sure the object exists and that you spell its name and the path name correctly.
我已检查并仔细检查名称是否正确,我已重命名工作表并将名称复制粘贴到我的代码中,但似乎没有任何效果。 我究竟做错了什么?
这是我的代码:
string conStr = String.Format(
@"Provider={0};Data Source=""{1}"";Extended Properties=""{2}""",
"Microsoft.ACE.OLEDB.12.0",
"REGISTER 090310.xls",
"Excel 12.0 Xml;IMEX=1;HDR=YES;");
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
using (IDbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = conStr;
using (IDbCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT TOP 10 * FROM [Adresser$]";
connection.Open();
// The exception is thrown on this line, with yellow highlight on
// IDataReader dr = command.ExecuteReader()
using (IDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine(
string.Format("First name: {0}\tLast name: {1}",
dr[0].ToString(),
dr[1].ToString()));
}
}
}
}
I have more or less exactly the same error as in this post, but that solution has not solved my problem.
The error message I get:
The Microsoft Office Access database engine could not find the object 'AdresserI have checked and double-checked that the name is right, I have renamed the sheet and copy-pasted the name into my code, but nothing seems to work. What am i doing wrong?
This is my code:
. Make sure the object exists and that you spell its name and the path name correctly.string conStr = String.Format( @"Provider={0};Data Source=""{1}"";Extended Properties=""{2}""", "Microsoft.ACE.OLEDB.12.0", "REGISTER 090310.xls", "Excel 12.0 Xml;IMEX=1;HDR=YES;"); DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); using (IDbConnection connection = factory.CreateConnection()) { connection.ConnectionString = conStr; using (IDbCommand command = connection.CreateCommand()) { command.CommandText = "SELECT TOP 10 * FROM [Adresser$]"; connection.Open(); // The exception is thrown on this line, with yellow highlight on // IDataReader dr = command.ExecuteReader() using (IDataReader dr = command.ExecuteReader()) { while (dr.Read()) { Console.WriteLine( string.Format("First name: {0}\tLast name: {1}", dr[0].ToString(), dr[1].ToString())); } } } }
I have checked and double-checked that the name is right, I have renamed the sheet and copy-pasted the name into my code, but nothing seems to work. What am i doing wrong?
This is my code:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我解决了这个问题:
事实证明,该提供程序能够正确连接到 Excel 2003 工作表,但无法读取它。 因此,我在 Excel 2007 中打开工作表,并将其重新保存为
.xlsx
格式,并相应地更改了我的连接字符串。 现在一切正常了 =)OK, I solved it:
It turns out, this provider was able to connect correctly to an Excel 2003 worksheet, but couldn't read it. Thus, I opened the worksheet in Excel 2007 and re-saved it in the
.xlsx
format, and changed my connection string accordingly. It all works now =)