I'm trying to read data from an excel file. Full code below. The first few lines are junk, so skip them using the following
"SELECT * From [" + SheetName + "] WHERE [F3] <> ''";
I want to read the code to a datatable and still keep the headers. which appear after the junk lines.
The problem is that when I filter our the junk lines using the WHERE clause above, the datatable column titles come out as F1,F2 etc
In my connection string I do specify I want the headers
HDR=Yes.
If I remove the WHERE clause from the SELECT, it works as I expect.
Please advise
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
conStr = String.Format(conStr, FilePath, "Yes");
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
//Get the name of First Sheet
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = ListSheets.SelectedValue;
connExcel.Close();
//Read Data from First Sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "] WHERE [F3] <> ''";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
