There are some conditional columns which i need to skip while reading csv using datatable.
For example: My csv file has headers like Emp_Id, Emp_Name,Dept , Role , Addree and Salary. I want to skip Emp_Name and Role column while reading csv file and import the rest.
Emp_id Emp_Name Dept Role Address Salary
I am using below code to read csv file without any column skip But now i want to skip some columns: List listOfCol = new List(); DataTable dt = new DataTable();
for (int i = 1; i <= colCount; i++)
{
string colName = XlRange.Cells[1, i].Value2.ToString();
DataColumn dataColumn = new DataColumn(colName);
dt.Columns.Add(dataColumn);
listOfCol.Add(colName);
}
for (int i = 2; i <= rowCount; i++)
{
DataRow dataRow = dt.NewRow();
for (int j = 1; j <= colCount; j++)
{
if (XlRange.Cells[i, j] != null && XlRange.Cells[i, j].Value2 != null)
{
dataRow[listOfCol[j - 1].ToString()] = XlRange.Cells[i, j].Value2.ToString();
}
else
{
dataRow[listOfCol[j - 1].ToString()] = "";
}
}
dt.Rows.Add(dataRow);
}
DataTableas opposed to removing those columns after loading all the data?