protected void btnExport_Click(object sender, EventArgs e) { Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=AverageResolution.xls"); Response.ContentType = "application/excel"; System.IO.StringWriter sw = new System.IO.StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GrdReport.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); }
protected void btnExportExcel_Click(object sender, EventArgs e) { // Clear all content output from the buffer stream Response.ClearContent(); // Specify the default file name using "content-disposition" RESPONSE header Response.AppendHeader("content-disposition", "attachment; filename=OverAllReport " + DateTime.Now + ".xls"); // Set excel as the HTTP MIME type Response.ContentType = "application/excel"; // Create an instance of stringWriter for writing information to a string System.IO.StringWriter stringWriter = new System.IO.StringWriter(); // Create an instance of HtmlTextWriter class for writing markup // characters and text to an ASP.NET server control output stream HtmlTextWriter htw = new HtmlTextWriter(stringWriter); // Set white color as the background color for gridview header row GrdReport.HeaderRow.Style.Add("background-color", "#FFFFFF"); // Set background color of each cell of GridView1 header row foreach (TableCell tableCell in GrdReport.HeaderRow.Cells) { tableCell.Style["background-color"] = "#A55129"; } // Set background color of each cell of each data row of GridView1 foreach (GridViewRow gridViewRow in GrdReport.Rows) { gridViewRow.BackColor = System.Drawing.Color.White; foreach (TableCell gridViewRowTableCell in gridViewRow.Cells) { gridViewRowTableCell.Style["background-color"] = "#FFF7E7"; } } GrdReport.RenderControl(htw); Response.Write(stringWriter.ToString()); Response.End(); }