[英]Error reading from Excel in C#: “…could not find the object…”
我或多或少與這篇文章中的錯誤完全相同,但是該解決方案無法解決我的問題。
我收到的錯誤消息:
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()));
}
}
}
}
好的,我解決了:
事實證明,此提供程序能夠正確連接到Excel 2003工作表,但無法讀取它。 因此,我在Excel 2007中打開了工作表,並以.xlsx
格式重新保存了該工作表 ,並相應地更改了我的連接字符串。 現在一切正常=)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.