Parsing excel files fails - c#

I have to pull data from an excel file. I was using the below piece of code since last year:
OleDbConnection cnn = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + destFile + "; Extended Properties=Excel 12.0;");
string qText = #"select * from [sheetXY$]";
OleDbCommand oconn = new OleDbCommand(qText, cnn);
cnn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(oconn);
adp.Fill(dt);
cnn.Close();
Since yesterday I get this error message:
I had Office 2010 pro on my machine. According to some google suggestions I installed microsoft database access engine, made no difference. I re-installed whole office, made no difference. Then I installed Office 2013, no go.
What could possibly go wrong from a day to another, and how shall I fix it? I'm out of idea.

Save yourself time and headache and use EPPLUS instead. EPPLUS can be installed via Nuget and you don't have to deal with installing the Office Data access on other machine where your code runs.

You can go for openXML SDK. Which requires no machine level dependency.
http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML
http://msdn.microsoft.com/en-us/library/office/gg575571(v=office.15).aspx

Related

Excel upload not working on windows Server 2012

I have developed an application where the user have to upload a winzip archive file. The code then extracts an Excel file (in .xls format, 97-2003 workbook) from that archive and uploads the data within that Excel file into my SQL database table. I'm using ZipFile.ExtractToDirectory(zipPath, extractPath) to extract the file.
Below is the code I'm using for the excel upload.
using (OleDbConnection connExcel = new OleDbConnection(Excelconstring))
{
using (OleDbCommand cmdExcel = new OleDbCommand())
{
using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
{
cmdExcel.Connection = connExcel;
connExcel.Open();
DataTable dtExcelSchema;
string sheetName = "Chat Summary$";
//DateTime.Today.ToString("M.d.y$");
// Read Data from First Sheet.
cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
odaExcel.SelectCommand = cmdExcel;
odaExcel.Fill(dt);
connExcel.Close();
}
}
}
The problem is this works absolutely fine on my local system 64-bit Windows 7 with Microsoft Office 2010. I'm using Microsoft.Jet.OLEDB.4.0 on my local system. The problem is when I deploy the same code on my server (Windows Server 2012 with Microsoft Office 2013), this doesn't work. I have installed Microsoft Access 2010 redistributable on the server and change the provider to Microsoft.ACE.OLEDB.12.0 but still it doesn't work. The error I get after changing the provider to ACE is
External table is not in the expected format
Also when I extract the file myself, open it once and then archive it again - it works fine. I have tried many solutions from different forums but none of them seems to work. I have changed the debug mode from "anycpu" to "x86" and "x64" but still it doesn't work.
The steps I have already tried are installing the Microsoft Access 2010 redistributable, changing the debug mode from "anycpu" to "x86", changing the connection string from Jet.OLEDB.4.0 to ACE.OLEDB.12.0.
Excel 2010 is still a 32-bit application. Therefore, try setting Enable 32-bit applications to True on your IIS App Pool.

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine in MS office 2013 [duplicate]

This question already has answers here:
'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine
(39 answers)
Closed 7 years ago.
I'm getting the following error
"The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine."
while reading the data from Excel file using OLEDB Data Reader.
Its not working in both 32 and 64 bit machine. Currently my machine having MS office 2013 and VS 2010.
string Connection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;MAXSCANROWS=15;READONLY=FALSE\"";
OleDbConnection con = new OleDbConnection(Connection);
OleDbCommand command = new OleDbCommand();
DataSet ds = new DataSet();
command.CommandText = sql;
command.CommandType = CommandType.Text;
command.Connection = con;
command.Connection.Open();
OleDbDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection);
You need to change the Solution Platform from "Any CPU" to "x86". The steps are given below:
Right click on the Solution File in Solution Explorer
Click on the Configuration Manager.
Click on the Active Platform Drop down, if x86 is already there then select that, else click on New.
Select x86 from the new platform dropdown:
Compile and run your application.
If you are still facing the issue, then try installing Office System Driver. from follow site
http://www.microsoft.com/en-us/download/details.aspx?id=23734
Please refer to the following link for a possible solution:
On 64-bit Windows and 64-bit Office (2010, 2013) environments, there are many reports on this error...
...try using 2007 Office System Driver: Data Connectivity Components

Reading xlsx file in c# using OleDbConnection throws error

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.
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
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
The configuration platform of console application is of 32 Bit
Environment Details
Windows Server 2008 (64 bit)
Visual Studio 2010 ( 32 bit)
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.
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.

Trying to connect to excel 2010 doesn't work

I got this exception:
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
I was trying to connect Excel 2010 files with OleDBConnection. I have windows 7 64bit and office 2010 32 bit.
I have tried to install Microsoft Access Database Engine 2010 Redistributable which suggested from:
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered in the local machine
I also tried this suggestion:
http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/1d5c04c7-157f-4955-a14b-41d912d50a64
Neither one of them works for me. Anyone help?
Here is my code:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\Sample.xlsx;Mode=Share Deny Write;Extended Properties=\"HDR=YES;\";Jet OLEDB:Engine Type=37";
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = command;
DataSet ds = new DataSet();
adapter.Fill(ds);
ds.Tables[0].Rows.Count.Dump();
}
catch (Exception)
{
throw;
}
finally
{
connection.Close();
}
Here's a discussion about this error, please refer to it and let me know whether it is worked for you.
HOW TO FIX
Best Regards
try installing this first: http://www.microsoft.com/download/en/details.aspx?id=13255
try the following method:
NOTE: this DOES work for office 2010 even though it is for 2007 office
download and install this: Download here
in VS click add data source, follow the wizard.

I cannot open .xlsx file

I want to open an xlsx file, I have tried the below code,but neither does it open nor does it thrown any error.
Can anyone throw any light upon it
string path = "C:\\examples\\file1.xlsx";
string connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
OleDbConnection cn = new OleDbConnection(connString);
cn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", cn);
DataTable dt = new DataTable();
adapter.Fill(dt);
In December 2010 Microsoft (finally!) published a a 64-bit OLEDB driver for CSV and XLSX files.
You'll need the 64-bit Microsoft Access Database Engine 2010 Redistributable. Make sure to download the 64-bit version (AccessDatabaseEngine_X64.exe). You'll need to uninstall any 32-bit Office apps (including Sharepoint Designer!) in order to install it.
If you want CSV, you'll want the Microsoft Access Text Driver (*.txt, *.csv) driver name, but I haven't been able to find a full connection string yet using this driver from OLEDB (although if you have, leave a commend and I'll amend this answer). Note that 64-bit names are different from the 32-bit versions.
For reading XLSX files, use a connection string like this:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ FilePath
+ ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
For XLS (pre-2007 Excel) files use a connection string like this:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ FilePath
+ ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
Many thanks to this blog post and this answer for pointing me in the right direction when I ran into the same problem, and for providing content I adapted to write this answer.
Are you running it on 64 bit Windows? Last time I checked the OLE drivers for Excel workbooks did not work with 64 bit Windows.
SpreadsheetGear for .NET will let you read Excel workbooks from .NET and works with .NET 2.0+ - including 64 bit Windows.
You can see live samples here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC
See what this does in your connection string:
Extended Properties=\"Excel 8.0;HDR=YES;\" the rest of your connection string is correct I believe.
In addition to Phillip's answer make sure to set the TargetPlatform to x86.

Categories

Resources