Read Xls file having Date Format Column - c#

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

Related

OleDB Simple CSV Import

The Code below prompts the user to select a csv file, and then parses the data into the datatable based on column names. So if the first row of the CSV file shows "Col1" and the Table has a "Col1" column, all of the data in the CSV file under Col1 is put in as rows filling under the DataTables Col1.
If the column names in the CSV don't match up with anything in the DataTable however it throws an "External table is not in the expected format." error.
I want to change the system so that it uploads in a much less intelligent way. and then I'll give the user the option to assign columns manually in a WPF window.
Does anyone know how to make this just upload the raw data? Disregarding column names and just putting everything in the first column of the CSV into the first column of the DataTable?
try
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog().Value)
Console.Out.WriteLine(openFileDialog1.FileName);
string pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + openFileDialog1.FileName + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=2\"";
OleDbConnection conn = new OleDbConnection(pathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + "sheet1" + "$]", conn);
myDataAdapter.Fill(displayTable);
} catch (Exception e)
{
MessageBox.Show("Error! : " + e.Message);
Console.Out.WriteLine(e.Message);
}

Date column Error while importing excel in c#

I am reading a simple excel file into my code . excel has 2 column : inputDate[Date] and OutputDate[Date] ..
After importing Excel ; 2nd Column value is not coming properly ..it is coming like 42005 ,42006 instead of date .. 2nd column also include holidays , which I Needed as output
here is my code
var fileName = string.Format("D:\\HolidayMaster", Request.PhysicalApplicationPath, Guid.NewGuid().ToString().Replace("-", string.Empty));
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data
source={0}; Extended Properties='Excel 8.0;;HRD=YES;IMEX=1'", fileName);
var adapter = new OleDbDataAdapter("SELECT * From
[Sheet1$] ", connectionString);
var adapter = new OleDbDataAdapter("SELECT * From [Sheet1$]",connectionString);
DataSet dsExcel = new DataSet();
adapter.Fill(dsExcel, "HolidayMaster");
This is the issue only for 2nd column ..What I am missing ?If this is not the proper way . what is the right way to get holidays and date as well
ADO.NET chooses data type based on the majority of the values in the column. That's why it is converting your 2nd column. In your case, B5 confuses ADO.NET
Try this (code not tested):
Change this line:
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data
source={0}; Extended Properties='Excel 8.0;HRD=YES;IMEX=1'", fileName);
To:
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1';", fileName);

Data is missing while reading excel file using OLEDB

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

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