C# exporting CSV with incorrect column format
I am exporting a gridview full of data to a CSV file using this code:
protected void ExportToCSV(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "text/csv";
//To Export all pages.
GridView1.AllowPaging = false;
this.BindGrid();
StringBuilder sb = new StringBuilder();
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
//Append data with separator.
sb.Append(cell.Text + ',');
}
//Append new line character.
sb.Append("\r\n");
foreach (GridViewRow row in GridView1.Rows)
{
foreach (TableCell cell in row.Cells)
{
//Append data with separator.
sb.Append(cell.Text + ',');
}
//Append new line character.
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}
When I open the csv file in Excel are display all column of GridView except these GridView columns:
<asp:TemplateField
HeaderText="N."
ItemStyle-CssClass="ddl_Class_new"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#String.Format("{0:N0}",Container.DataItemIndex + 1)%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="El"
ItemStyle-CssClass="ddl_Class_new"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div>
<asp:Label
runat="server"
ID="lbEl"
CssClass="ddl_Class_new"
Text='<%# String.Concat(Eval("_COD_1"), " - ", Eval("_COD__2"))%>'></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
Can you help me?