private void ExportGridToExcel() { //GridClaimData.AllowPaging = true; //this.ClaimDataGrid(); //GridClaimData.ForeColor = System.Drawing.Color.Black; GridClaimData.RowStyle.ForeColor = System.Drawing.Color.Black; GridClaimData.HeaderStyle.ForeColor = System.Drawing.Color.Black; Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=ExportGridData.xls"); Response.ContentType = "File/Data.xls"; StringWriter StringWriter = new System.IO.StringWriter(); HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter); GridClaimData.RenderControl(HtmlTextWriter); Response.Write(StringWriter.ToString()); Response.End(); }
protected void btnExportExcel_Click(object sender, EventArgs e) { try { if (GridClaimData.Rows.Count > 0) { //btnExportExcel.Visible = true; Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); GridClaimData.RowStyle.ForeColor = System.Drawing.Color.Black; GridClaimData.HeaderStyle.ForeColor = System.Drawing.Color.Black; //To Export all pages GridClaimData.AllowPaging = false; //this.BindGrid(); if (DropDownList1.SelectedIndex == 1) { btnEmployeeGenerateReports_Click(sender, e); } else if (DropDownList1.SelectedIndex == 2) { btnclaimDateGenerateReports_Click(sender, e); } else if (DropDownList1.SelectedIndex == 3) { btnStatusGenerateReport_Click(sender, e); } foreach (TableCell cell in GridClaimData.HeaderRow.Cells) { cell.BackColor = GridClaimData.HeaderStyle.BackColor; } foreach (GridViewRow row in GridClaimData.Rows) { row.BackColor = Color.White; foreach (TableCell cell in row.Cells) { if (row.RowIndex % 2 == 0) { cell.BackColor = GridClaimData.AlternatingRowStyle.BackColor; } else { cell.BackColor = GridClaimData.RowStyle.BackColor; } cell.CssClass = "textmode"; } } GridClaimData.RenderControl(hw); //style to format numbers to string string style = @"<style> .textmode { } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } } else { if (DropDownList1.SelectedIndex == 1) { pieChartPanel.Visible = false; employeePanel.Visible = true; claimDatePanel.Visible = false; claimStatusPanel.Visible = false; } else if (DropDownList1.SelectedIndex == 2) { pieChartPanel.Visible = false; employeePanel.Visible = false; claimDatePanel.Visible = true; claimStatusPanel.Visible = false; } else if (DropDownList1.SelectedIndex == 3) { pieChartPanel.Visible = false; employeePanel.Visible = false; claimDatePanel.Visible = false; claimStatusPanel.Visible = true; } alertmod.Style.Add("background-color", "#ffc2b3"); alert.Style.Add("background-color", "#ffc2b3"); Label5.ForeColor = System.Drawing.ColorTranslator.FromHtml("black"); Label5.Text = "No records to export"; alert.Visible = true; TextBox1.Text = string.Empty; TextBox3.Text = string.Empty; } } catch (Exception ex) { alertmod.Style.Add("background-color", "#ffc2b3"); alert.Style.Add("background-color", "#ffc2b3"); //Label2.ForeColor = System.Drawing.ColorTranslator.FromHtml("Red"); Label5.ForeColor = System.Drawing.ColorTranslator.FromHtml("black"); //Label2.Text = "Failure!"; Label5.Text = "Employee has no claims"; alert.Visible = true; } }