Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one gridview and one table in an aspx page, that has filled with user details now I need to export gridview and table data to word and excel document based on selection.How can i do it please help me any one.I try to use this code but this code does not work with images , just with text.

try
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + ViewState["fileName"].ToString() + ".doc");
Response.Charset = "";
Response.Buffer = true;
EnableViewState = false;

Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Panel1.RenderControl(htmlWrite);
Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
Response.Write(stringWrite.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}


but this code does not work with images , just with text.
Posted
Updated 15-Jul-13 22:55pm
v2

1 solution

Add a template field to your grid put a checkbox in that template field for selection purpose.

C#
HtmlTable tbl=new HtmlTable();
foreach(GridViewRow row in GridView1.Rows)
{
  if(row.RowType==RowType.DataRow)
  {
     CheckBox chk =row.FindControl("chk") as CheckBox;
     if(chk.Checked)
      {
         // find the labels or text in the the grid here create HtmlRows and Cells here
         HtmlTableRow r=new HtmlTableRow();
         HtmlTableCell c1=new HtmlTableCell();
         HtmlTableCell c2=new HtmlTableCell();
         c1.InnerHtml=((Label)row.FindControl("Label1")).Text;
         c2.InnerHtml=((Label)row.FindControl("Label2")).Text;
         r.Cells.Add(c1);
         r.Cells.Add(c2);
         tbl.Rows.Add(r);
      }

  }

}

HtmlTextWriter htw=new HtmlTextWriter();
stringWriter sw=new stringWriter(htw);
tbl.RenderControl(sw);
// set header  and write to response
 
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