gpt4 book ai didi

c# - 在 C# : "...could not find the object..." 中从 Excel 读取错误

转载 作者:行者123 更新时间:2023-12-04 22:11:06 25 4
gpt4 key购买 nike

我或多或少与 this post 中的错误完全相同,但该解决方案并没有解决我的问题。

我得到的错误信息:

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.

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:

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

最佳答案

好的,我解决了:

事实证明,该提供程序能够正确连接到 Excel 2003 工作表,但无法读取它。因此,我 在 Excel 2007 中打开工作表并将其重新保存在 .xlsx 中格式 ,并相应地更改了我的连接字符串。现在一切正常=)

关于c# - 在 C# : "...could not find the object..." 中从 Excel 读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/951005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com