无法弄清楚为什么我收到 Excel 文件的 System.Data.OleDb.OleDbException

发布于 2024-12-20 16:55:29 字数 2025 浏览 3 评论 0原文

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    // Show the dialog and get result.
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK) // Test result.
    {
        labelFilePath.Text = openFileDialog1.FileName;
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + labelFilePath.Text.Trim() + ";Extended Properties=\"Excel 8.0;HDR=YES\"";
        using (var conn = new System.Data.OleDb.OleDbConnection(connString)) 
        { 
            conn.Open(); 
            try
            {
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "Select * From [Sheet1]";
                    using (OleDbDataReader reader = cmd.ExecuteReader())
                    {
                        int firstNameOrdinal = reader.GetOrdinal("First Name");
                        int lastNameOrdinal = reader.GetOrdinal("Last Name");
                        while (reader.Read())
                        {
                            Console.WriteLine("First Name: {0}, Last Name: {1}", reader.GetString(firstNameOrdinal), reader.GetString(lastNameOrdinal));
                        }
                    }
                }
            }
            catch (OleDbException odbe)
            {
                Console.WriteLine(odbe.Errors.ToString());
                Console.WriteLine(odbe.Message.ToString());
            }
        } 

    }
    Console.WriteLine(result); // <-- For debugging use only.

}

我在 OleDbDataReader reader = cmd.ExecuteReader() 处收到错误,

这是输出

“System.Data.OleDb.OleDbException”类型的第一次机会异常 发生在System.Data.dll System.Data.OleDb.OleDbErrorCollection 中 Microsoft Jet 数据库引擎找不到对象“Sheet1”。 确保该对象存在并且拼写其名称和路径 正确命名。好的

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    // Show the dialog and get result.
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK) // Test result.
    {
        labelFilePath.Text = openFileDialog1.FileName;
        string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + labelFilePath.Text.Trim() + ";Extended Properties=\"Excel 8.0;HDR=YES\"";
        using (var conn = new System.Data.OleDb.OleDbConnection(connString)) 
        { 
            conn.Open(); 
            try
            {
                using (OleDbCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "Select * From [Sheet1]";
                    using (OleDbDataReader reader = cmd.ExecuteReader())
                    {
                        int firstNameOrdinal = reader.GetOrdinal("First Name");
                        int lastNameOrdinal = reader.GetOrdinal("Last Name");
                        while (reader.Read())
                        {
                            Console.WriteLine("First Name: {0}, Last Name: {1}", reader.GetString(firstNameOrdinal), reader.GetString(lastNameOrdinal));
                        }
                    }
                }
            }
            catch (OleDbException odbe)
            {
                Console.WriteLine(odbe.Errors.ToString());
                Console.WriteLine(odbe.Message.ToString());
            }
        } 

    }
    Console.WriteLine(result); // <-- For debugging use only.

}

I get the error at OleDbDataReader reader = cmd.ExecuteReader()

Here is the output

A first chance exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll System.Data.OleDb.OleDbErrorCollection The
Microsoft Jet database engine could not find the object 'Sheet1'.
Make sure the object exists and that you spell its name and the path
name correctly. OK

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

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

发布评论

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

评论(2

浮生面具三千个 2024-12-27 16:55:29

尝试更改以下行(注意 $):

cmd.CommandText = "Select * From [Sheet1$]";

在本例中,您将从工作表中选择所有内容,但是此表示法扩展到选择命名范围,如下所示:

cmd.CommandText = "Select * From [Sheet1$NamedRange]";

Try changing the following line (note the $):

cmd.CommandText = "Select * From [Sheet1$]";

In this case you are selecting everything from the sheet, however this notation extends to select named ranges as follows:

cmd.CommandText = "Select * From [Sheet1$NamedRange]";
﹏半生如梦愿梦如真 2024-12-27 16:55:29

只是一个想法:当我在选择了 64 位平台目标的项目中使用 Excel 驱动程序时,我在 Excel 驱动程序中遇到了 oledb 异常。尝试转到项目设置并将“构建”选项卡上的平台目标更改为 x86。

Just a thought: I have had oledb exceptions in Excel drivers when i was using them in a project that had 64 bit platform target selected. Try going to project settings and changing platform target on Build tab to x86.

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