无法使用 C# 读取我的 Excel 文件

发布于 2024-09-10 09:33:29 字数 620 浏览 1 评论 0原文

我有一个 Excel 2007 文件“my.xlsx”和一个名为“States”的工作表,并且我有以下代码

 using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\my.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO'"))
        {
            OleDbCommand cmd = new OleDbCommand("select * from [States]", con);

            con.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
                Console.WriteLine(reader[0]);
        }

它不断抛出异常,提示“Microsoft Office Access 数据库引擎找不到对象“States”。请确保该对象存在并且您正确拼写其名称和路径名。”。

有人可以帮忙看看我的代码有什么问题吗?

I have an Excel 2007 file "my.xlsx" and a sheet named "States", and I have the following code

 using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\my.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO'"))
        {
            OleDbCommand cmd = new OleDbCommand("select * from [States]", con);

            con.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
                Console.WriteLine(reader[0]);
        }

It keeps throwing exception saying "The Microsoft Office Access database engine could not find the object 'States'. Make sure the object exists and that you spell its name and the path name correctly.".

Could someone help to see what's wrong with my code please?

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

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

发布评论

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

评论(3

野生奥特曼 2024-09-17 09:33:29

我知道这可能不完全是您想听到的,但您是一长串尝试使用 oledb 读取 excel 文件时遇到困难的人之一...

我使用诸如NPOI 从 C# 读取 Excel 文件:

http://npoi.codeplex.com/(推荐)

http://nexcel.sourceforge.net/

http://sourceforge.net/projects/koogra/

I know this may be not exactly what you want to hear, but you're in a long line of people who have had struggles trying to read excel files by using oledb...

I've had a lot more luck using libraries such as NPOI to read Excel files from C#:

http://npoi.codeplex.com/ (recommended)

http://nexcel.sourceforge.net/

http://sourceforge.net/projects/koogra/

北笙凉宸 2024-09-17 09:33:29

我认为您需要在创建命令之前打开连接 - 不确定这是否有什么大不了的...

然后在工作表名称末尾添加一个 $

OleDbCommand cmd = new OleDbCommand("select * from [States$]", con);

基本上 [Name] 指的是命名范围,而 [Name$] 指的是工作表。

有关详细信息,请参阅此知识库:http://support.microsoft.com/kb/316934

I think that you need to open the connection before creating the command - not sure if it's a big deal...

Then add a $ at the end of the sheet name:

OleDbCommand cmd = new OleDbCommand("select * from [States$]", con);

Basically [Name] refers to a named range, whereas [Name$] refers to a sheet.

For more information see this KB: http://support.microsoft.com/kb/316934

独木成林 2024-09-17 09:33:29

我刚刚通过访问此页面

http:// /www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx

对于我的要求,我只需要读取一个Excel文件。我搜索的大多数解决方案都显示您使用某种库,这对我来说太过分了。我真的想找人发布一个关于如何读取文件的代码片段,但我只发现在我放置链接的页面上。

I just got it working by visiting this page

http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx

For my requirement, I only need to read an Excel file. Most of the solutions I searched show you to use some kind of library, which is overkill for me. I was really looking for someone to just post a code snippet on how to read the file, but I only found that on the page I put the link with.

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