Hello nilu,
you need to:
1. Get Data from gridview
2. Supply it to the target system(i.e. pdf, word or excel) in their formats.
Check the below example to export it to excel
private void ExportToExcel()
{
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(stringWriter);
try
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=test1.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Col"));
DataRow dr = dt.NewRow();
dr["Col"] = "some value";
dt.Rows.Add(dr);
GridView GridView1 = new GridView();
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.RenderControl(htw);
Response.Write(stringWriter.ToString());
Response.End();
}
catch (Exception ex)
{
}
}
you can change the content type to Word if you want to export it to word.
There are lots of tutorial providing information on how to create PDF from a html with iTextSharp. like:
http://forums.asp.net/t/1199774.aspx/1?Convert+html+to+pdf+using+iTextSharp[
^]
so create a pdf from the html returned from stringwriter and write it to the output of response with proper contenttype.
Things to remember:
Gridview1.RenderControl(..) will throw an exception if GridView1 is added to the web page you must create gridview on the fly.
hope this will help.
Thanks,
Hemant