Click here to Skip to main content
14,775,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have the following codes which will save all the records in datatable into a csv file. But when the records are saved, it will also include the header into the csv file. How can i remove the header from the csv file? Below are my codes:

string targetDes = TargetFolder + "1000101_20100203_192220_Employee_Transfer_Import_36" + ".csv";
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(targetDes, sb.ToString());
Posted
Updated 19-Dec-19 2:38am

1 solution

remove the will do
IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900