Export Multiple GridView data into excel but GridView is not getting exported
EXCEL FILE DOWNLOADING EMPTY
I follow your below reference
Exporting Multiple GridViews To Excel SpreadSheet in ASP.Net
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
PrepareForExport(GVdaily);
PrepareForExport(GVManual);
Table tb = new Table();
TableRow tr1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Controls.Add(GVdaily);
tr1.Cells.Add(cell1);
TableCell cell3 = new TableCell();
cell3.Controls.Add(GVManual);
TableCell cell2 = new TableCell();
cell2.Text = " ";
if (rbPreference.SelectedValue == "2")
{
tr1.Cells.Add(cell2);
tr1.Cells.Add(cell3);
tb.Rows.Add(tr1);
}
else
{
TableRow tr2 = new TableRow();
tr2.Cells.Add(cell2);
TableRow tr3 = new TableRow();
tr3.Cells.Add(cell3);
tb.Rows.Add(tr1);
tb.Rows.Add(tr2);
tb.Rows.Add(tr3);
}
tb.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
private void PrepareForExport(GridView Gridview)
{
Gridview.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value);
Gridview.DataBind();
//Change the Header Row back to white color
Gridview.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
for (int k = 0; k < Gridview.HeaderRow.Cells.Count; k++)
{
Gridview.HeaderRow.Cells[k].Style.Add("background-color", "green");
}
for (int i = 0; i < Gridview.Rows.Count; i++)
{
GridViewRow row = Gridview.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
for (int j = 0; j < Gridview.Rows[i].Cells.Count; j++)
{
row.Cells[j].Style.Add("background-color", "#C2D69B");
}
}
}
}