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