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=AcknowledgementPosingDetails " + 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 GrdAcknowledgement.HeaderRow.Style.Add("background-color", "#FFFFFF"); // Hide Particular Header and Cell of gridview foreach (GridViewRow row in GrdAcknowledgement.Rows) { GrdAcknowledgement.HeaderRow.Cells[6].Visible = false; row.Cells[6].Visible = false; } // Set background color of each cell of GridView1 header row foreach (TableCell tableCell in GrdAcknowledgement.HeaderRow.Cells) { tableCell.Style["background-color"] = "#2873F0"; } // Set background color of each cell of each data row of GridView1 foreach (GridViewRow gridViewRow in GrdAcknowledgement.Rows) { gridViewRow.BackColor = System.Drawing.Color.White; foreach (TableCell gridViewRowTableCell in gridViewRow.Cells) { gridViewRowTableCell.Style["background-color"] = "#FFFFFF"; } } GrdAcknowledgement.RenderControl(htw); Response.Write(stringWriter.ToString()); Response.End(); }
public void GetAcknowledgementPosingAction() { try { obj = new AcknowledgementBL(); DataTable dt = obj.GetAcknowledgementPosingActionBL(Session["UserId"].ToString()); if (dt.Rows.Count > 0) { GrdAcknowledgement.DataSource = dt; GrdAcknowledgement.DataBind(); } else { GrdAcknowledgement.DataSource = dt; GrdAcknowledgement.DataBind(); } } catch (Exception ex) { LogError(ex); } }