Manually Add Header in CsvHelper.CsvWriter

后端 未结 2 849
终归单人心
终归单人心 2021-02-13 06:58

I\'m using CsvHelper class to write rows in DataTable to a csv file. The code works but I can\'t get it to write the headers.

How can I add the

2条回答
  •  既然无缘
    2021-02-13 07:03

    The easiest way is to use

    writer.WriteHeader();
    

    More complete example:

    using (StreamWriter sw = new StreamWriter(@"C:\output.csv"))
    {
      using (CsvWriter writer = new CsvWriter(sw))
      {
         writer.WriteHeader();
    
         writer.WriteRecord(yourRecordVariable);
      }
    }
    

    As was posted here on Google groups by the author Josh Close.

提交回复
热议问题