Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to resolve the error or no response while reading from excel file in .xlsx format using Oledb in c#?

I am reading the data from Excel via Oledb connection. I am getting the error "attempted to read write protected memory. this is often an indication that other memory is corrupt"." or with no response on filling data to data table for excel files with .xlsx format. Else with .xls format it works fine.

My code is as below:
C#
if (fileExtension == ".xls" || fileExtension == ".XLS")
              {
                  connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

              }
              else if (fileExtension == ".xlsx" || fileExtension == ".XLSX")
              {
                  connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
              }


              DataTable dt = new DataTable();


              System.Data.OleDb.OleDbConnection MyConnection;
              System.Data.DataSet ds;
              System.Data.OleDb.OleDbDataAdapter MyCommand;
              MyConnection = new System.Data.OleDb.OleDbConnection(connStr);
              MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
              ds = new System.Data.DataSet();
              MyCommand.Fill(dt);
              MyConnection.Close();


What I have tried:

My code is as below:
if (fileExtension == ".xls" || fileExtension == ".XLS")
{
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";

}
else if (fileExtension == ".xlsx" || fileExtension == ".XLSX")
{
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}


DataTable dt = new DataTable();


System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet ds;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(connStr);
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
ds = new System.Data.DataSet();
MyCommand.Fill(dt);
MyConnection.Close();
Posted
Updated 8-Jul-16 0:47am

1 solution

Use the following connection string in case of XLXS as it is.

C#
connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filelocation + ";Extended Properties =\"Excel 12.0 Xml;HDR=YES\";";


Remember just copy and paste.
 
Share this answer
 
Comments
Ema112 8-Jul-16 8:33am    
Used the same copy pasting but still remains in processing mode just on filling to data table for excel with .xlsx extension . If with .xls extension it works fine.
[no name] 8-Jul-16 8:45am    
Which office version is installed on your computer?
Ema112 8-Jul-16 9:00am    
Microsoft Office Excel 2007
[no name] 8-Jul-16 9:03am    
that is the problem.
download this engine and install it what ever your OS is like 32 bit or 64 bit.
https://www.microsoft.com/en-pk/download/details.aspx?id=13255

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900