Reading xlsx file in c# using OleDbConnection throws error

蓝咒 提交于 2020-01-04 05:14:28

问题


I wrote a console application to read the xlsx file in C# using OleDbConnection. It throws the following error

The Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

Below is the code i have written

string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Suganya\ColumnReport.xlsx;Extended Properties=Excel 12.0;";
OleDbConnection objConn = new OleDbConnection();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
objConn = new OleDbConnection(connString);

string Query = "SELECT * FROM [Sheet1$]";
OleDbCommand objCmd = new OleDbCommand(Query, objConn);

DataTable Table = new DataTable();
dataAdapter.SelectCommand = objCmd;
dataAdapter.Fill(Table);

I have already performed following things to fix the issue.

  1. I have installed AccessDatabaseEngine.exe (32 bit) and found that ACEOLEDB.dll is present in the following path C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14

  2. Tried referring the ACEOLEDB.dll in the application. But it gave me the following error

    A reference to 'ACEOLEDB.DLL' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component

  3. The configuration platform of console application is of 32 Bit

Environment Details

  1. Windows Server 2008 (64 bit)
  2. Visual Studio 2010 ( 32 bit)
  3. MS office is not installed

Checked the following links to fix the issue

1.http://www.codeproject.com/Questions/486549/Theplus-27Microsoft-ACE-Oledb-12-0-27plusproviderp 2.http://www.codeproject.com/Questions/337953/The-Microsoft-ACE-OLEDB-12-0-provider-is-not-regis

Any help is highly appreciated.


回答1:


You have to install Microsoft Office; Office installation process will copy and register the wished assemblies ('ACEOLEDB.DLL') in the GAC, or you can add the missing assemblies in your bin (application) directory or regsiter them by your self.



来源:https://stackoverflow.com/questions/18593665/reading-xlsx-file-in-c-sharp-using-oledbconnection-throws-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!