从 C# 读取 Excel 文件
我有一个连接字符串来从我的 C# 项目中读取一个 excel 文件,如下所示..
String ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + VariableFile + ";" +
"Extended Properties=Excel 8.0;";
并且我还有 objConn.Open();打开文件..
问题是我的程序打开该文件的唯一时间是我手动打开 Excel 文件并运行我的程序。任何人都可以帮助我从 C# 代码打开该文件,而不必先手动打开它。当我尝试在不先打开 Excel 文件的情况下运行它时,收到错误消息:无法找到可安装的 ISAM。
谢谢
I have a connection string to read an excel file from my C# project that looks like this..
String ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + VariableFile + ";" +
"Extended Properties=Excel 8.0;";
and I also have objConn.Open(); to open the file..
The problem is the only time my program will open the file is if I open the Excel file manually and run my program. Can anyone help me to open the file from my C# code instead of having to open it first manually. I get the error message: Could not find installable ISAM when I try to run it without opening the Excel file first.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您的连接字符串格式错误,“无法找到可安装的 ISAM”通常表明了这一点。
试试这个,它来自我的一段操作代码:
Excel 2007
Excel 2003
I think your connection string is formatted wrong and the "Could not find installable ISAM" is usually an indication of this.
Try this, it's from a piece of operational code I have:
Excel 2007
Excel 2003
我最近不得不使用此提供程序来执行 Azure Web 作业,其中我需要使用 OLEDB 提供程序而不是 Excel。
您可以使用以下设置安装 Microsoft.ACE.OLEDB.12.0 提供程序。
Microsoft Access 数据库引擎 2010 可再发行组件
https://www.microsoft.com/en-us/download /details.aspx?id=13255
安装后,您可以修改 .xls 和 .xlsx 文件扩展名的连接字符串。
例如,以下代码将 Excel 文件转换为 DataSet,其中 Excel 文件中的每个工作表都有一个 DataTable。
...
I recently had to use this provider for an Azure Web Job where I needed to use an OLEDB Provider rather than the Excel.
You can install the Microsoft.ACE.OLEDB.12.0 Provider using the following setup.
Microsoft Access Database Engine 2010 Redistributable
https://www.microsoft.com/en-us/download/details.aspx?id=13255
Once installed, you can modify the connection string for .xls and .xlsx file extensions.
For example, the following code will convert an Excel file to a DataSet with a DataTable for each Worksheet in the excel file.
...
有不同的提供程序可用于连接到 Excel。也许您应该尝试使用不同的。
查看此处的示例:
http://www.connectionstrings.com/excel
Excel 提供程序
» 微软 Jet OLE DB 4.0
» ACE OLEDB 12.0
» 用于 OLE DB 的 .NET Framework 数据提供程序 (OleDbConnection)
» Microsoft Excel ODBC 驱动程序
» 用于 ODBC 的 .NET Framework 数据提供程序 (OdbcConnection)
» .NET xlReader for Microsoft Excel (ExcelConnection)
在您的情况下,您应该有这样的东西:
提供程序=Microsoft.ACE.OLEDB.12.0;数据源=c:\myFolder\myOldExcelFile.xls;扩展属性=“Excel 12.0;HDR=YES”;
There are different providers for connecting to Excel. Maybe you should try using a different one.
Have a look at the examples here:
http://www.connectionstrings.com/excel
Providers for Excel
» Microsoft Jet OLE DB 4.0
» ACE OLEDB 12.0
» .NET Framework Data Provider for OLE DB (OleDbConnection)
» Microsoft Excel ODBC Driver
» .NET Framework Data Provider for ODBC (OdbcConnection)
» .NET xlReader for Microsoft Excel (ExcelConnection)
In your case you should have something like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myOldExcelFile.xls;Extended Properties="Excel 12.0;HDR=YES";
以下代码将读取 Excel 文件&用其数据填充 DataTable
Following Code will Read the Excel file & Fill DataTable with its data
其他选择是使用专门的库而不是创建连接。看一下 EPPlus,它是一个用 C# 处理 excel 文件的开源库。这对我来说非常有效。
http://epplus.codeplex.com/
在此链接中,您可以看到使用 EPPlus 读取 excel 文件的示例:
http://blog.fryhard.com/archive/2010/10/28/reading-xlsx-files-using-c-and-epplus.aspx
Other option would be to use a specialized library instead of creating a connection. Take a look on EPPlus, its an open source library to work with excel files in C#. It has worked very good to me.
http://epplus.codeplex.com/
And in this link you can see examples on reading excel files with EPPlus:
http://blog.fryhard.com/archive/2010/10/28/reading-xlsx-files-using-c-and-epplus.aspx