0

I want to read a csv file where header row starts from specific column name i.e “name”

The CSV file format looks like this:

v3,vf,gf--Not a Header row 1
v1,c,z1,e--Not a Hera  row 2
name,q1,q2,q3- Header Row- row 3-because name is here
a,0,1,2-Data

I want to read where “name” column exists - so I want to skip the first 2 rows, but we don't know before “name” how many row will come it may 2 or 3 or n

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MANISH
  • 25
  • 7

2 Answers2

0

Read line-by-line, and just throw away the lines until you reach the header row you want. Then, parse the remaining rows.

IEnumerable<string[]> ReadDataLines(string csv) {   
    using(var reader = new StringReader(csv))
    using(var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader)) {
        parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
        parser.SetDelimiters(",");

        // Until we find the header row, just throw away all the rows
        while(!parser.EndOfData) {
            var line = parser.ReadLine();
            if(line != null && line.TrimStart().StartsWith("name")) {
                break;
            }
        }       
        // The rest of the input is data rows
        while(!parser.EndOfData) {
            yield return parser.ReadFields();
        }       
    }
}

Here, I use Microsoft.VisualBasic.FileIO.TextFieldParser to parse the CSV (see Reading CSV files using C#). You can of course use any technique you want to parse the CSV, the same general approach can be used.

gnud
  • 77,584
  • 5
  • 64
  • 78
0
con = new SqlConnection(strcon);

string[] arrFileName;

if (files != null && files.Length > 0)
{
    arrFileName = new string[files.Length];
    con.Open();

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@FileName", "");

    int Result = cmd.ExecuteNonQuery();
    cmd.Dispose();
    con.Close();

    for (int i = 0; i < files.Length; i++)
    {
        string FileName = string.Empty;
        string filepath = files[i];

        FileName = filepath.Split('\\').Last();

        ///////////Logic For Import File
        using (StreamReader sr = new StreamReader(filepath))
        {
            string line = sr.ReadLine();

            string[] value = line.Split(',');
            DataTable dt = new DataTable();
            DataRow row;

            foreach (string dc in value)
            {
                dt.Columns.Add(new DataColumn(dc));
            }

            dt.Columns.Add(new DataColumn("FileName"));
            dt.Columns.Add(new DataColumn("Date"));

            while (!sr.EndOfStream)
            {
                value = sr.ReadLine().Split(',');

                row = dt.NewRow();
                row.ItemArray = value;
                row[9] = FileName;
                row[10] = DateTime.Now;

                dt.Rows.Add(row);
            }

            SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
            bc.DestinationTableName = "InvoiceData";
            bc.BatchSize = dt.Rows.Count;

            con.Open();
            bc.BulkCopyTimeout = 2000;
            bc.WriteToServer(dt);
            bc.Close();
            con.Close();
        }

        //////Logic For Delete File From Folder if you want
        File.Delete(filepath);
    }

    files = new string[] { };
    txtFilePath.Text = null;

    MessageBox.Show("Files Imported Successfully!!");
}
else
{
    MessageBox.Show("Please Browse The File!!");
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459