在 C# 中从 Excel 读取时出错:“...找不到对象...”

发布于 2024-07-22 06:53:00 字数 1491 浏览 2 评论 0原文

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

我收到的错误消息:

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 'Adresser

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烦人精 2024-07-29 06:53:00

好的,我解决了这个问题:

事实证明,该提供程序能够正确连接到 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 =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文