I am using OLEDB to read excel file into datatable. But the problem is, some values are missing(Empty). In my excel sheet one column datatype is General, it has mixed values like string and integers. Most of the cell values are integers. Why OLEDB is skipping string values.
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + "; Extended Properties=\"Excel 12.0;IMEX=1\";";
OleDbCommand myAccessCommand = new OleDbCommand();
myAccessCommand.CommandText = "Select * from [" + sheetName + "]";
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myDataAdapter.Fill(myDataSet);
Check following link and see points under "RESOLUTION":
http://support.microsoft.com/kb/194124
Please see point 2 NOTE.
Setting IMEX=1 is entirely dependent on your registry settings. By default, first 8 rows are checked to determine the data type. IMEX=1 can give unpredictable behaviors, such as skipping string values. There is also one small workaround for this problem. Just add single quote (') before every cell value in excel. Every cell will be treated as string.
Add IMEX=1 to the connection string as below:
string con = string.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" + #"Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", fileName);
I do have a xls file with a column Call_Date of DateTime Format.
I am trying to read this file and put it in datatable with connection string property HDR = No ie. i wanna no header row for datatable that reads it.
The first row that i am getting in datatable is the names of columns in xls except call_date having empty string in first row.
I can understand OLEDB might try to read it as datetime format and put it as empty string with type = "System.DBNull" if can't parse it as datetime.
But i need to be have this call_date column name in datatable first row like others.
I can't change the format in xls. can i do it while reading in c# or something else.
My code so far
mCon.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;data source=" + openFileDialog1.FileName + ";Extended Properties=\"Excel 12.0;HDR=NO\";");
strSelectQuery = "SELECT TOP 20 * FROM [Sheet1$]";
if (mCon.State == ConnectionState.Closed)
{
mCon.Open();
}
DataAdapter = new System.Data.OleDb.OleDbDataAdapter(strSelectQuery, mCon);
DataAdapter.Fill(mDTable);
mCon.Close();
I have solved it myself after spending hours on searching this topic on internet.
I just modified my connection string to allow OLEDB to read excel file with multiple datatype entry in one column by adding IMEX =1 in extended properties.
My connection string now looks as
mCon.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;data source=" + openFileDialog1.FileName + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX = 1\";");
Ok, I start off by importing an excel file and then save the data into Data table and then putting it into Gridview. I wanted to copy the column header name into a row cell and it was giving error that Input string was not is a correct format. I tried changing the column data type and it works. But the data copied is different to the original. Here is the code I am using:
DataTable dtf1 = new DataTable();
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
adapter.FillSchema(dtf1, SchemaType.Source);
dtf1.Columns[6].DataType = typeof(string);
adapter.Fill(dtf1);
dtf1.Rows[0][6] = dtf1.Columns[6].ColumnName.ToString();
The original data is a postcode number "3210" but it is copying it as something "F7".
I found the solution here OleDB & mixed Excel datatypes : missing data. The problem was when excel was importing data it was ignoring numbers. Changing the connection string solved the problem.
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ServerPath + ";Extended Properties='Excel 8.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text'");
Previously it was
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ServerPath + ";Extended Properties='Excel 8.0;IMEX=1'");
I have written a piece of code that reads excel data in data table through ado.net. Now i have seen a strange behavior, That the column data type in ms excel is specified as general, but if the 1st row of the excel of that particular column contains some text, ado.net is unable to read numbers in that particular column and if 1st row contains some numeric information ado.net is unable to read textual data of that particular column. here is the code i am using to read data from excel.
string excelConString = #"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=D:\A.xls;
Extended Properties=""Excel 8.0;HDR=YES;""";
var oleDbConnection = new OleDbConnection(excelConString);
var cmd = oleDbConnection.CreateCommand();
cmd.CommandText = "select * from [WorkSheet$] where ID>=1500";
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Any help would be appreciated.
Regards
Ahsan Iqbal
change your connectionstring to
string excelConString = #"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=D:\A.xls;
Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text"""
I know this topic is done to death but I am at wits end.
I need to parse a csv. It's a pretty average CSV and the parsing logic has been written using OleDB by another developer who swore that it work before he went on vacation :)
CSV sample:
Dispatch Date,Master Tape,Master Time Code,Material ID,Channel,Title,Version,Duration,Language,Producer,Edit Date,Packaging,1 st TX,Last TX,Usage,S&P Rating,Comments,Replace,Event TX Date,Alternate Title
,a,b,c,d,e,f,g,h,,i,,j,k,,l,m,,n,
The problem I have is that I get various errors depending on the connection string I try.
when I try the connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source="D:\TEST.csv\";Extended Properties="text;HDR=No;FMT=Delimited"
I get the error:
'D:\TEST.csv' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
When I try the connection string:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TEST.csv;Extended Properties=Excel 12.0;
or the connection string
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TEST.csv;Extended Properties=Excel 8.0;
I get the error:
External table is not in the expected format.
I am considering throwing away all the code and starting from scratch. Is there something obvious I am doing wrong?
You should indicate only the directory name in your connection string. The file name will be used to query:
var filename = #"c:\work\test.csv";
var connString = string.Format(
#"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""",
Path.GetDirectoryName(filename)
);
using (var conn = new OleDbConnection(connString))
{
conn.Open();
var query = "SELECT * FROM [" + Path.GetFileName(filename) + "]";
using (var adapter = new OleDbDataAdapter(query, conn))
{
var ds = new DataSet("CSV File");
adapter.Fill(ds);
}
}
And instead of OleDB you could use a decent CSV parser (or another one).
Alternate solution is to use TextFieldParser class (part of .Net framework itself.) https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser
This way you do not have to rely on other developer who has gone for holidays. I have used it so many times and have not hit any snag.
I have posted this from work (hence I cannot post an example snippet. I will do so when I go home this evening).
It seems your first row contains the column names, so you need to include the HDR=YES property, like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TEST.csv;Extended Properties="Excel 12.0;HDR=YES";
Try the connection string:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TEST.csv;Extended Properties=\"Excel 8.0;IMEX=1\""
var s=#"D:\TEST.csv";
string dir = Path.GetDirectoryName(s);
string sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";