Hi kai,
You can directly change the header row text but you cant directly hide columns and rows which you dont want to display so you need to do workaround like below.
Please go through the code carefully you will get to know where exactly you need to make the changes according to your need.
VB.Net
Protected Sub btnExportCSV_Click(sender As Object, e As EventArgs)
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv")
Response.Charset = ""
Response.ContentType = "application/text"
GridView1.AllowPaging = False
'changing the header text.
GridView1.Columns(0).HeaderText = "SpecimenIdentifier"
'hiding the header cell to be displayed in csv file.
GridView1.Columns(1).HeaderText = ""
'hiding the rows cells to be displayed in csv file.
GridView1.Columns(1).Visible = False
GridView1.DataBind()
Dim sb As New StringBuilder()
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
If Not String.IsNullOrEmpty(GridView1.Columns(k).HeaderText) Then
sb.Append(GridView1.Columns(k).HeaderText + ","c)
End If
Next
'append new line
sb.Append(vbCr & vbLf)
For i As Integer = 0 To GridView1.Rows.Count - 1
For k As Integer = 0 To GridView1.Columns.Count - 1
'add separator
If Not String.IsNullOrEmpty(GridView1.Rows(i).Cells(k).Text) Then
sb.Append(GridView1.Rows(i).Cells(k).Text + ","c)
End If
Next
'append new line
sb.Append(vbCr & vbLf)
Next
Response.Output.Write(sb.ToString())
Response.Flush()
Response.[End]()
End Sub
C#
protected void btnExportCSV_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
GridView1.AllowPaging = false;
GridView1.Columns[0].HeaderText = "SpecimenIdentifier"; //changing the header text.
GridView1.Columns[1].HeaderText = ""; // hiding the header cell to be displayed in csv file.
GridView1.Columns[1].Visible = false; //hiding the rows cells to be displayed in csv file.
GridView1.DataBind();
StringBuilder sb = new StringBuilder();
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
if (!string.IsNullOrEmpty(GridView1.Columns[k].HeaderText))
{
sb.Append(GridView1.Columns[k].HeaderText + ',');
}
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
for (int k = 0; k < GridView1.Columns.Count; k++)
{
//add separator
if (!string.IsNullOrEmpty(GridView1.Rows[i].Cells[k].Text))
{
sb.Append(GridView1.Rows[i].Cells[k].Text + ',');
}
}
//append new line
sb.Append("\r\n");
}
Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
}