Data is missing while reading excel file using OLEDB - c#

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);

Related

Read Xls file having Date Format Column

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\";");

Cell values getting truncated while excel upload

I am using following code for excel upload
OleDbConnection sSourceConnection;
string properties = "Excel 8.0; HDR=NO; IMEX=1;";//properties set for connection to excel
string sSourceConstr = #"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + filePath + ";Extended Properties=\"" + properties + "\"";
sSourceConnection = new OleDbConnection(sSourceConstr);//creating the OLEDB connection
try
{
//select statement to select data from the first excel sheet
string sql = string.Format("Select * FROM [{0}]", "Sheet1$");
//commands to fill the dataset with excel data
OleDbDataAdapter excelAdapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
sSourceConnection.Open();
excelAdapter.SelectCommand = command;
excelAdapter.Fill(dSet, EXCEL_DATA);
I have to upload around 300 records. One column has some text comments. The length of comments varies from 10 chars to 1000 chars. But all of comments above 255 length are getting truncated in that column.
I have used this post Excel cell-values are truncated by OLEDB-provider to change a registry setting, but it didn't work.
I have also tried everything mentioned in the post OleDB & mixed Excel datatypes : missing data, still nothing works.
I was using ACE engine so the correct place to update the registery is
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel\TypeGuessRowsHKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel\TypeGuessRows
For Microsoft Office 2010-2013-2016/365
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel
And better is to scan for the text ‘TypeGuessRows’ and when you find it, in combination with Excel, set its value to 0. We’ve found another important location for this behavior at this location
\HKEY_LOCAL_MACHINE \SOFTWARE\Microsoft\Office \ClickToRun\REGISTRY \MACHINE\Software \Wow6432Node \Microsoft \Office\16.0\Access Connectivity Engine\Engines\Excel.

excel data to datagridview through dataset using OLE skips the initial blank rows and blank columns

I am using OLE related API's for reading data to DataSet and later to view in DataGridView. The source is an excel file. If the first row or column is empty than that row or column is getting skipped. I wish to read the data irrespective of empty/has data. Other rows/columns even if its empty its working fine, but only starting is missing.
I read about ColumnHeader and tried changing connectionString (HDR=NO) and others but nothing worked out.
Any additional things need to be specified while calling the API? One to one mapping with the excel column and the DataGridView column is missing because of skipping this initial blank rows/columns.
Anything needs to be added/modified to this OleDbDataAdapter parameter, which actually reads the data and fills to dataset:
OleDbDataAdapter cmd = new System.Data.OleDb.OleDbDataAdapter(
"select * from [" + worksheetName + "$]", con);
Yes, I am setting some required information and calling a method to read data from each sheet. I am not doing any action on the rows/columns except reading them.
OleDbConnection con = new System.Data.OleDb.OleDbConnection(connectionString);
System.Data.DataTable getWorksheetData(OleDbConnection con, OleDbDataAdapter cmd)
{
con.Open();
System.Data.DataSet excelDataSet = new DataSet();
cmd.Fill(excelDataSet);
con.Close();
return excelDataSet.Tables[0];
}
My connection string is:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileName + "; Extended Properties=\"Excel 12.0 Xml;HDR=NO;Mode=Read;ReadOnly=True;\"";

Changing the datatype of column in datatable changes the cell text

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'");

How to remove Excel file headers C#, Oledb

Sting TempFileLocation="Filelocation";
Sting tempfilename ="FileName";
Sting TabName ="TabName$";
string xConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +TempFileLocation+ tempfilename +".xls;Extended Properties='Excel 8.0;HDR=YES'";
var conn = new OleDbConnection(xConnStr);
string ColumnName ="[columename] varchar(255)"
conn.Open();
var cmd = new OleDbCommand("CREATE TABLE [" + TabName + "] (" + ColumnName + ")", conn);
cmd.ExecuteNonQuery();
conn.Close();
Any one know hoe to delete Excel file column headers.
To soluation for this
Visit Cant create Excel file using OLEDB C#
I used temporary headers. after create the Excel i need to delete all temporary headers. Please anyone know how to do it please let me know . Thanks
If you mean you don't want the temporary column names you created in excel, you can simply set HDR=NO in your connection string. This property is whether or not you want the first row in the excel worksheet as your column titles; if set to no then your columns will all be named F1, F2, F3 etc. instead.

Categories

Resources