1

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);
        }
3
  • 1
    How are you reading the file currently? By "skip" you mean you want to avoid loading those columns/fields into the DataTable as opposed to removing those columns after loading all the data? Commented Apr 22, 2020 at 17:00
  • Currently i am reading all headers (column)without any filteration. Yes, i want to avoid loading those columns into datatable. Commented Apr 22, 2020 at 17:40
  • 1
    Yes, but how are you doing that? Are you parsing the file yourself, or using a library? It'd be helpful if you provided your code so answers could show you how to modify or improve it. Commented Apr 22, 2020 at 18:01

2 Answers 2

3

It depends on how you are already building DataTable.

You may try NuGet package csvHelper

And a class definition that looks like this.

public class Foo {
    public int Id { get; set; }
    public string Name { get; set; }
}

If our class property names match our CSV file header names, we can read the file without any configuration.

using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) {
    var records = csv.GetRecords<Foo>();
}

To select only specific columns use column index

public class Foo { 
    [Index(0)]
    public int Id { get; set; }

    [Index(1)]
    public string Name { get; set; }
 }

Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

Can you put the sample code you are working with so we can assist better. The crude way to do it would be as your read each line of the csv file spilt it by the delimiter which returns an array of your items and you cherry pick by index to add to you datatable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.