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;\"";
Related
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.
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.
I'm using OleDB to import data into grid from text file with extension ".K$$".
Here's some example code:
FileInfo file = new FileInfo(filename);
string connectionString = "";
OleDbDataAdapter adapter;
OleDbConnection con;
connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + file.DirectoryName + ";Extended Properties=\"Text;Format=TabDelimited;\"";
con = new OleDbConnection(connectionString);
con.Open();
adapter = new OleDbDataAdapter(String.Format("SELECT * FROM {0} ", file.Name), con);
adapter.Fill(MyDataTable);
when executing the Fill method it throws the exception. What's wrong with the FROM clause? Thanks
EDIT:
Ok, after some tests I found out that the problem is with the "$" symbols. Maybe it's some reserved symbol ?
Also, if I rename the extension to ".txt" the file got loaded into the grid but it only have 1 column , which means it can't see that there're tabs in the rows.
Another issue is that when I change the file extension to something different than ".txt" (for ex. ".tx") the Fill method throws exception "Cannot update. Database or object is read-only".
OK, I just tried creating an example.K$$, and then tried to connect to it using the same provider as stated through Server Explorer in Visual Studio 2010. Its an Unrecognised format.
I don't think this will ever work.
You may need to look at connecting via a different provider or method.
I think You should look at this link :-
EDIT :
http://www.codeproject.com/Articles/6737/Fill-a-DataSet-from-delimited-text-files
It will allow you to read your txt file into a datable correctly.
Try checking the path of the filename if its correct
Check that neither the directory, nor the filename, contain spaces. If they do, you'll need to escape/quote them.
You'll also need to quote the filename if it contains an extension since the . is not a valid character in the FROM clause. Try FROM [{0}] (though this may not be the correct quoting character for the OleDbDataAdapter).
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.
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