oldbreader get the text cells as empty - c#

I am trying to read an excel file using oledbreader and oledbconnection the connection string is as follows
#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0;"
when I try reading from the excel file some of the files work fine and give me the data I need but in other cases all columns that have a text value are shown as empty but it takes all the int or double values normally and without problems I looked around and found that some people changed the connection string to this
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + PrmPathExcelFile + #";Extended Properties=""Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text"""
and after that it worked just fine. I tried that way but when I try to open connection I get isam error.
The file I read from is xlsx not xls if that helps ..

use this as connection string,
Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+FilePath+";Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1"
you are trying to import mixed types but while type guessing rows the column is datatype is treated as double, so text values are not coming.Using IMEX=1 solves this problem.

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.

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

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.

Why is the last row not returned on Excel import?

I'm using OleDbConnection with the following connection string:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "'{0}'" + ";Extended Properties='Excel 12.0 xml;'";
I'm importing the Excel file into an OleDbDataReader and then using reader.Read() to read in the rows, as follows:
while (reader.Read())
{
// import row
}
For some reason, the last row of the Excel file is always missing in the import. I've tried it with several different Excel files, but it has never worked. Out of desperation, I've also tried iterating one more time after the loop ends, but it tells me there is no more data. What could possibly be the problem?

Unexpected error from external database driver (22)

Here's my code which is in the beginning of a method for converting .xls file to .csv.
sourceFile="C:\\Users\\myUser\\Desktop\\Folder\\myFile.xls";
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + sourceFile + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
And it crashes on the last line, throwing this exception: Unexpected error from external database driver (22).
I tried removing the IMEX=1 part, but it still didn't work.
What is the problem?
I also had the same issue but I had to rename the spreadsheet to a shorter name, then it worked.(SQL 2012 Dev)
Strangely enough, I replaced the file in another folder and it worked. I have no idea why this is happening.
Try to use Microsoft.Jet.OLEDB.4.0 provider. Also I would advise you to use OleDbConnectionStringBuilder to build OleDbConnectionString:
var oleConnectionStringBuilder = new OleDbConnectionStringBuilder { Provider = "Microsoft.Jet.OLEDB.4.0" };
oleConnectionStringBuilder.DataSource = sourceFile;
oleConnectionStringBuilder.Add("Extended Properties", "Excel 8.0");
oleConnectionStringBuilder.Add("HDR", "No");
It seems that there is something wrong with the Microsoft Excel Driver. Try to execute the program in another PC to see whether this error happens.
Please take a look at this KB article:
http://www.codeproject.com/KB/database/ReadExcel07.aspx . You can use OleDb to connect the Excel file.
I hope this can help you and feel free to follow up after you have tried.
i have fixed this by uninstalling Microsoft access DB Engine Security update.Also Uninstall Service pack3 Update of access DB engine 2007. Hope this will work...
In my case I renamed excel file's sheet name(it was too long), after that it worked.
I'm using :
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FilePath + ";Extended Properties=\"Excel 12.0 Xml; IMEX=1;Importmixedtypes=text;\"";

Mixed Data Type in Excel, IMEX Doesn't Work

I am trying to export xls into datatable. Below is my connection string.
string path = //xls source path
OleDbConnection MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties='Excel8.0;IMEX=1;TypeGuessRows=0;HDR=No;ImportMixedTypes=Text'");
I set IMEX=1 and all the other extended properties as I have to deal with mixed datatypes.
Even though I set the connection like that yet I still produced error.
There are no error messages, but the inconsistent rows (who don't follow the majority datatype are set to null instead).
Can someone tell me what did I miss? Btw, I am using the OleDbDataAdapter & Fill(DataSet) method.
Are you sure that TypeGuessRows=0; and ImportMixedTypes=Text; are working from connection string and should not be modified in registry (HKEY_LOCAL_MACHINE\SOFTWARE\[Wow6432Node\]Microsoft\Jet\4.0\Engines\Excel)? AFAIK this settings are read from registry. ImportMixedTypes=Text is usually by default, but TypeGuessRows=8, and should be set to 0 as in your connection string.

Categories