如何使用 ADO.NET 读取 .XLSX (Excel 2007) 文件?我发现“无法找到可安装的 ISAM”-错误
我需要在 .net 2.0
中工作。所以我不能使用 OpenXML。
这是我的源代码,我已经安装了 AccessDatabaseEngine.exe
。
但仍然遇到异常:
“找不到可安装的 ISAM”。
我还在连接字符串中尝试了“Extended Properties=Excel 8.0”
。
static void Main(string[] args)
{
DataSet dataSet = new DataSet();
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|Data Directory|\HSC.xlsx;Extended Properties=Excel 12.0;HDR=YES;");
OleDbDataAdapter dataAdapter= new OleDbDataAdapter("select * from [Sheet1$]", connection);
dataAdapter.Fill(dataSet);
}
I need to work in .net 2.0
. So I can't use OpenXML.
This is my source code and I have already Installed AccessDatabaseEngine.exe
.
But still getting the exception:
"Could not find installable ISAM".
I have also tried "Extended Properties=Excel 8.0"
in the connection string.
static void Main(string[] args)
{
DataSet dataSet = new DataSet();
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|Data Directory|\HSC.xlsx;Extended Properties=Excel 12.0;HDR=YES;");
OleDbDataAdapter dataAdapter= new OleDbDataAdapter("select * from [Sheet1$]", connection);
dataAdapter.Fill(dataSet);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 Carl Prothman 的说法,应该是
- http://www.connectionstrings.com/excel-2007
更详细:
注意单引号。
According to Carl Prothman, that should be
-- http://www.connectionstrings.com/excel-2007
In more detail:
Note the single quotes.
我更喜欢使用 Microsoft OpenXML 2.0 SDK 来实现此类功能。它有一个非常漂亮的界面,并且不需要在读取 XLSX 文件的计算机上安装 Office,这是一件好事。
我是用手机写这篇文章的,所以很难提供链接,但谷歌搜索应该很容易找到它。
尝试一下。我想你会喜欢的。
编辑
如果您必须使用.NET 2.0,则可以改用
OleDb
的JET变体。这意味着您将执行类似以下操作来连接:
然后您可以像上面的示例中那样查询它:
尝试一下!请注意,Jet 有一些奇怪的逻辑来决定列是否为数字。有关详细信息,请参阅以下 SO 问题: 问题使用 OleDbDataAdapter 从 Excel 工作表中获取数据
I prefer to use the Microsoft OpenXML 2.0 SDK for this kind of functionality. It has a really nice interface, and it does not put a demand on having Office installed on the machine reading the XLSX file which is a good thing.
I'm writing this from my mobile, so hard to provide a link, but a Google search should easily find it for you.
Give it a try. I think you will like it.
EDIT
If you have to use .NET 2.0, you can go for using the JET variant of the
OleDb
instead.That means you will do something like this to connect:
Then you can query it like in your example above:
Try it! Just note that
Jet
have some strange logic of deciding if a column is numeric or not. See the following SO questions for details: Problem with using OleDbDataAdapter to fetch data from a Excel sheet您应该确保连接字符串如下所示(即使您正在访问 microsoft excel 版本 10 ->
You should make sure that the connection string looks like the following ( even if you are accessing microsoft excel version 10 ->