Click here to Skip to main content
15,036,145 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
dear friends
i am new to this site
i just wanted to know the code to export gridview data to pdf,word ,excel in c#2.0 asp.net
it is better if u can help me with using iTextSharp dll in project
Posted
Updated 29-Apr-11 9:43am
v2

Navigate Given link
link1[^]
link2[^]
   
Comments
Orcun Iyigun 29-Apr-11 17:20pm
   
Good link, I was about to give the first link as a refence but you are a bit faster than me. 5ed..
RaviRanjanKr 30-Apr-11 0:15am
   
Thanks :)
nilu16 1-May-11 3:05am
   
thanks for code
it all working for word and excel
but for pdf, pdf is coming but with blank page
i populate gridview with sqldatasource properties
so don't think my gridview is blank
it is populated and all r coming in word and excell format
but in pdf format not coming


please check ur code or tell me how to do



thanks for support
V B B Ramoji Rao 23-Aug-12 1:26am
   
Thanks,Is it possible to removing last row of gridview when exporting to word document?
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";
                //Create data source
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Col"));
                DataRow dr = dt.NewRow();
                dr["Col"] = "some value";
                dt.Rows.Add(dr);
                //Create a grid view
                GridView GridView1 = new GridView();
                GridView1.DataSource = dt;
                GridView1.DataBind();
                //Write the html on stringWriter with htmltextwriter
                GridView1.RenderControl(htw);
                //stringWriter.Tostring() will return html generated from gridview1
                Response.Write(stringWriter.ToString());
                Response.End();
            }
            catch (Exception ex)
            {
                //Log the exception
            }
        }


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
   

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