OleDB Simple CSV Import - c#

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

Related

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

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

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