How to remove Excel file headers C#, Oledb - c#

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.

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

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

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

Export into excel file without headers c# using Oledb

I'm using OleDB and I want to export my objects into excel table. Each row in the sheet will be one of my objects. The problem is that I don't know how to insert data when there's no column headers in the sheet.
This one:
commandString = "Insert into [Sheet1$] values('test1', 'test2')"
throws this exception:
Number of query values and destination fields are not the same.
My connection string is:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filename+";Extended Properties='Excel 8.0;HDR=No'"
If the connection string contains HDR=NO then the Jet OLE DB provider automatically names the fields for you (F1 for the first field, F2 for the second field, and so on).
I will try to change your query in this way
commandString = "Insert into [Sheet1$] (F1, F2) values('test1', 'test2')"
this works only after you have created the excel file and have something inserted in the first two cells of the first row in Sheet1
You need to specify which values you are writing, since you don't use an HDR - just use the cells.
The Error "number of query values" simplies implies that - there are no fields assigned to the values supplied.
Update: #Steve was right with the Fields (F1,F2,etc), and the code below does work here:
OleDbConnection Cn = new OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=No\"", #"D:\test.xls"));
Cn.Open();
OleDbCommand Com = new OleDbCommand("INSERT INTO [Sheet1$](F1,F2) VALUES('test3','test4');", Cn);
Com.ExecuteNonQuery();
Cn.Close();

Categories

Resources