Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C:“中读取Excel时出错”;。。。找不到对象…”;_C#_Excel_Oledb - Fatal编程技术网

C# 在C:“中读取Excel时出错”;。。。找不到对象…”;

C# 在C:“中读取Excel时出错”;。。。找不到对象…”;,c#,excel,oledb,C#,Excel,Oledb,我的错误或多或少与中的完全相同,但该解决方案并没有解决我的问题 我收到的错误消息是: 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. 好的,我解决了: 事实证明,此提供程序能够正确连接到Excel 2003工

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

我收到的错误消息是:

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. 好的,我解决了:


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

有没有可能是Addresser$?两个d?不,在瑞典语中,它的拼写只有一个d…;)这太糟糕了(我的意思是,不管现在的俚语是什么,这都是一件坏事)。它应该总是能够阅读旧版本,除非有一个广为人知的休息。它是生病了。最糟糕的是,它抱怨找不到工作表,而不是无法读取文件。问题不仅愚蠢,错误消息也没有直接的帮助。。。唉…我忘了指定文件的扩展名,所以它找不到它-但错误是它找不到工作表!
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()));
            }
        }
    }
}