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