When I try read Excel with OLE DB all values are empty - c#

I have done a little program to parser excel. It works fine only when before to execute it I open Excel file manually (is not it strange?). I.e. first I open excel file, second I execute program and I get good results
If I don't open excel before to execute it I get empty values
My connection string (excel file has extension .XLSX):
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + path + "\\" + f.Name + ";" +
"Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";
My code to open connection with oleDB:
using (OleDbConnection cnn = new OleDbConnection(connectionString))
{
cnn.Open();
...
String sql = "SELECT * FROM [" + sheetNames[i] + "]";
OleDbDataAdapter da = new OleDbDataAdapter(sql, cnn);
DataTable dt = new DataTable();
da.Fill(dt); // Now 'dt' should has all data
}
Also, I have installed AccessDatabaseEngine.exe and AccessRuntime.exe
Obviously, my purpose is run the program without having to manually open the file. Any suggestion?
Thanks for your time.

I found it a real pain when I tried to get OleDb and Excel to play nicely together. Fortunately, I found a much better approach: EPPlus
EPPlus is a .net library that reads and writes Excel 2007/2010 files using the Open Office Xml format (xlsx).
Open source, feature rich and easy to use. If at all possible, use it instead of OleDb.

Related

Unable to read xlsx file through OleDb with the size more than ~1mb

I'd like to use sql bulk copy in order to load data from *.xlsx file to the data base. But, I've faced the problem when file size is more than approximately 1mb. When I try to open OleDbConnection I get an error
No error message available, result code: E_FAIL(0x80004005)
Does anyone have an idea about such behavior?
P.S. If file size is less than mentioned above everything works as expected.
string connString = connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 Xml;";
// Create the connection object
OleDbConnection oledbConn = new OleDbConnection(connString);
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + WorkSheetName + "$" + DataRange + "]", oledbConn);
OleDbDataReader dr = cmd.ExecuteReader();
string ProfDbBulkCopyConnString = ConfigurationManager.ConnectionStrings["DbBulkCopyConnString"].ToString();
SqlBulkCopy sb = new SqlBulkCopy(ProfDbBulkCopyConnString);
sb.ColumnMappings.Add("Status", "ActionStatus");
sb.ColumnMappings.Add("Process", "ProcessExec");
sb.DestinationTableName = "dba.Execute";
sb.WriteToServer(dr);
Just a brief description of fixing. For more information I recommend to visit:
https://social.msdn.microsoft.com/Forums/en-US/4d1eeb6d-436d-4595-8645-fde90b2f9b18/oledb-error-opening-large-excel-2007-files-on-web-server?forum=adodotnetdataproviders
Export to excel spreadsheet (XLSX) failing
Microsoft ACE OLEDB connection creating empty Excel when there are 166,110 rows
Essentially, xlsx format is some kind of zip archive with a bunch of xml files. So, first of all ACEOLEDB provider try to unzip all data directly to memory buffer, but in case of large xlsx file provider unable to unzip all data to memory buffer and it forced to create temp file on the hard drive. If user hasn't permission to the folder Content.MSO on hard drive mentioned problem appears.
Path to folder depends on your enviroment. In my case it is C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.MSO (32-bit driver on 64-bit Windosw Server 2008 R2).
So, grant access to Content.MSO for user "IIS AppPool\DefaultAppPool" and problem goes away.

Shall I read an excel file via OleDb Jet4.0 and save into dataset?

I have to read an excel file and put into a dataset.
Shall I read excel file content via OleDbDataAdapter, and then Fill into a dataset?
I tried but faild. It said the application cannot recognize database format when data adapter is doing Fill method.
Code:
String queryAll = "SELECT * FROM [Sheet1$]";
String xlsPath = Directory.GetCurrentDirectory() + "\\paid.xls";
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsPath;
try
{
m_dbDA = new OleDbDataAdapter(queryAll, strConn);
DataSet dsPaidXls = new DataSet();
m_dbDA.Fill(dsPaidXls); //exception here
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
Does it mean there is no way to directly read an excel data and put into a new dataset?
And the only one way is to read excel data cell by cell and insert to a new DataSet with datatable?
Thanks in advance.
========================================
Resolved
========================================
String queryAll = "SELECT * FROM [Sheet1$]";
String xlsPath = Directory.GetCurrentDirectory() + "\\paid.xls";
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + xlsPath +
";Extended Properties='Excel 8.0;IMEX=1';";
try
{
m_dbDA = new OleDbDataAdapter(queryAll, strConn);
DataSet dsPaidXls = new DataSet();
m_dbDA.Fill(dsPaidXls,"[Sheet1$]");
dataGridView1.DataSource = dsPaidXls;
dataGridView1.DataMember = "[Sheet1$]";
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
OLEDB works quite well once you have the correct connection string and are aware of issues with data types. Jet is for versions prior to 2007 and you need to add extended properties for Excel.
String strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ xlsPath + "Extended Properties='Excel 12.0 Xml;HDR=YES';";
See:
Connection Strings
How To Use ADO with Excel Data from Visual Basic or VBA (contains useful notes)
Various notes
When you read Excel files via OleDB, make sure you have the right version of the provider (one for xls, one for xlsx), and also make sure you have x86 selected as platform.
If you don't, it will compile to x64 on a 64-Bit system, and because OleDb is deprecated, there are no 64-bit OleDb drivers, which means your program will crash on calling OleDb.
Also, the Office 2007 system driver (ACE Data Connectivity Components) must be installed.
See here:
Diagnosing an OLEDB exception when Quering Excel 2010
You could also use ODBC or Excel Package Plus.
Again, you need to take a different libary here if the format is xls and not xlsx.
ExcelLibrary for XLS
http://code.google.com/p/excellibrary/
Excel Package Plus for XLSX
http://epplus.codeplex.com/
You should avoid using OleDb to read Excel files. Many pitfals.
For xls files, this works much better and more reliably.
http://www.codeproject.com/KB/office/ExcelReader.aspx
For xlsx files, use the Office Open XML SDK:
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=5124

C# read open Excel file through OleDb

I need to connect to an open Excel 2003 file using .NET 3.5
It seems the OleDb connection which I am trying to use wants the file exclusively. But I need to have this file open in Excel in the same time.
Is non-locking reading possible?
EDIT: I resolved this by copying file before opening it.
the question seems have no answer. and I cant delete it....
my solution was - run macro on timer to save the excel file in question and C# app was copying the file to another one and reading it using OleDb.
This seems like a similar problem:
Writing into excel file with OLEDB
Does that work out for you?
What parameters are you passing in when you open the Excel document? Could you set the "ReadOnly" parameter in Workbook.Open() to true? See here.
Refer to the code below how to get the information of Excel data into an array. Then you will perform any validations on that Excel sheet.
var fileName = #"D:\Pavan\WorkDiployed.xlsx";
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=Excel 12.0;", fileName);
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", con);
con.Open();
System.Data.DataSet excelDataSet = new DataSet();
cmd.Fill(excelDataSet);
DataTable data = excelDataSet.Tables[0];
DataRow[] arrdata = data.Select();
foreach (DataRow rw in arrdata)
{
object[] cval = rw.ItemArray;
}
con.Close();
MessageBox.Show (excelDataSet.Tables[0].ToString ());

C# 3.5 Reading Excel files

I have a strange behavior when i try to read an XML worksheet using this code :
string CONNEC_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=No;IMEX=2;\"";
string fullFilePath = #"C:\Tmp\TestFile.xls";
using (OleDbConnection objCon = new OleDbConnection(string.Format(CONNEC_STRING, fullFilePath)))
{
using (OleDbCommand cm = new OleDbCommand("Select * From [MYCELLSRANGE]", objCon))
using (OleDbDataAdapter da = new OleDbDataAdapter(cm))
{
DataTable dt = new DataTable();
objCon.Open();
da.Fill(dt);
objCon.Close();
}
}
If the Excel file is closed, I receive the error 'External table is not in the expected format.'
When I open the file, if I execute the above code, it works fine and I can read data contained in MYCELLSRANGE.
So, has anyone any idea about this problem ?
Thanks for answers.
I believe its because the Jet Engine uses the Excel app to interpret the file. I ran into this issue once myself. When you use the Excel.dll reference and you interpret the Excel file from that you have to create a new Application instance to read the file. Jet just does this very thing to get the data. I suggest using that code instead.
C# Corner example of how to Import Excel to a ListView
C# Corner example of how to Create an Excel Worksheet in C#
MSDN users discussing the same topic
If you would like further assistance, let me know. I have successfully done this in VB.NET in various applications of mine.

Exception thrown when opening a Excel spreedsheet from C# stating file is being used by another user

I am currently developing an application that requires an excel spreadsheet, location selected by the user, to be read into a DataTable and then stored in a sql server database.
The application works perfectly in my development environment, however when it is deployed into my production environment an exception is thrown with the following message.
The Microsoft Jet database engine cannot open the file '.xls'. It is already opened exclusively by another user, or you need permission to view its data.
My code to read the excel file is as follows:
OleDbConnection objConn = null;
DataSet objDataset1 = null;
string fileLocation = GetFileLocation();
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + fileLocation
+ ";" + "Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(sConnectionString);
objConn.Open(); //This is where the exception is thrown
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
DataTable dt = objDataset1.Tables["XLData"];
Note the application and excel file are on different servers in the same domain.
Digging around various forumns and knowledge bases it would appear that the exception is thrown when the "user" does not have permission to use the file. Although not recommended permissions on the file have been set to Full Access for all users.
Apart from file permissions what else could cause this exception to be thrown?
Not sure if this is helpful but...
The error:
The Microsoft Jet database engine
cannot open the file '.xls'.
seems to point to a file named .xls, as opposed to something named, say myspreadsheet.xls or prodfile.xls.
It may be a long shot, but can you do some debugging to make sure that the file Jet is trying to open actually exists? It may be that the filename is not being constructed properly, for some reason.
Here's a few answers to the same root problem:
Read from Excel using OleDb in a Windows Service?
kill the previous opened excel process before starting the new process for reading the excel.it will solve your problem.
i wrote one function you can call it before opening the xls.
private void kill_excel_process()
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
}

Categories