使用 ADO.Net 在 C# 中读取 excel 文件时出现问题
我今天刚开始使用 C#,读取 Excel 文件时遇到问题。
这就是我所做的:
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended Properties=""Excel 12.0;HDR=YES;""");
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet$]", connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
我有一个例外,说它找不到“Sheet$” (我无法复制/粘贴异常,因为该消息是法语,而且我还不知道如何使用英语提供通用消息)
有人可以告诉我我做错了什么吗?
我按照他们在教程中所说的或在这里 从 C# 读取 Excel 文件
真的谢谢!
I just started C# today, and i have trouble reading an excel file.
Here's what i did :
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended Properties=""Excel 12.0;HDR=YES;""");
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet$]", connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
And i have an exception saying it can't find "Sheet$"
(i can't copy/paste the exception, because the message is in french, and i don't know yet how to have generic messages in english)
Could someone tell me what did i do wrong ?
I followed what they say in tutorials or like here Reading Excel files from C#
Thanks, really !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以从 Excel 文件中获取“表”(工作表或命名范围)列表:
TABLE_NAME 字段是您应该在查询中使用的字段。如果该值周围有单引号,则需要将其包括在内。例如,我的文件包含名为“GP”和“Kimberly Clark”的工作表。在架构表中,它们显示为“GP$”和“'Kimberly Clark$'”。在查询中,我会将它们引用为“[GP$]”或“['Kimberly Clark$']”。
You can get a list of "Tables" (worksheets or named ranges) from your Excel file:
The TABLE_NAME field is the one you should use in your query. If the value has single quotes around it, you'll need to include those. For example, my file has worksheets named "GP" and "Kimberly Clark". In the schema table, they appear as "GP$" and "'Kimberly Clark$'". In a query, I would reference them as "[GP$]" or "['Kimberly Clark$']".