Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a problem to export the two Gridview data in to Excel sheet

Solution..?
Posted

 
Share this answer
 
here,i got the solution ..

public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnexport_Click(object sender, ImageClickEventArgs e)
{
try
{

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);


if (GridView2.Rows.Count!=0)
{
PrepareForExport(GridView2);
}
if (GridView4.Rows.Count!=0)
{
PrepareForExport(GridView4);

}

Table tb = new Table();

TableRow tr1 = new TableRow();

TableCell cell1 = new TableCell();

cell1.Controls.Add(GridView2);

tr1.Cells.Add(cell1);

TableCell cell3 = new TableCell();

cell3.Controls.Add(GridView4);

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 = @" .textmode { mso-number-format:\@; } ";

Response.Write(style);

Response.Output.Write(sw.ToString());

Response.Flush();

}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
Response.End();

}




}


protected void PrepareForExport(GridView Gridview)
{

// Gridview.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value);

GridView2.AllowPaging = false;
GridView4.AllowPaging = false;
btnview_Click1(this, null);
// 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");

}

}

}

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900