I'm exporting a GridView to Excel with the following code:
protected void export_OnClick(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </style> ";
// Let's hide all unwanted stuffing
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
// Let's bind data to GridView
BindGrid();
//Change the color back to white
GridView1.HeaderRow.Style.Add("background-color", "#ffffff");
//Apply color to the header
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#e0e0e0");
}
// Let's output the Gridview
Response.Clear();
Response.ContentType = "application/vnd.xls";
Response.AddHeader("content-disposition", "attachment;filename=" + reportid + ".xls");
StringWriter swriter = new StringWriter();
HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
GridView1.RenderControl(hwriter);
Response.Write(style);
Response.Write(swriter.ToString());
Response.End();
}
Now I need to append a Name on the first row, Date & Time on the second row and possibly some other information on the third row. I will also like to leave a row between the actually GridView data and this information. Is this possible? If so, would you be so kind and provide some examples on how can I accomplish this. Thank you.
Just go ahead and add it. Add a carriage return after each to make Excel advance to the next row (use tabs to advance to the next cell).
Response.Write(style);
Response.Write("Pretty Excel Export File\n");
Response.Write("Generated on " + DateTime.Now.ToShortDateString() + "\n\n");
Response.Write(swriter.ToString());
Response.End();