Search This Blog

Sunday, March 16, 2014

Export Data In Asp.Net : GridView To MsWord(.doc),GridView To Ms Excel(.xls),GridView To PDF(.PDF)


We can use ASP.Net GridView Control to export the displayed data in different file format,

  • Ms Word(.doc)
  • Ms Excel(.xls)
  • Pdf(.pdf)
To use this take a GridView control on your ASP.Net Page. Bind the GridView Control to your desired table, to bind the GridView contro see my related post in ADO.Net Tag. Then Add three buttons to export
gridview data and set the property as below:
  1. Text=Export To Word- Id=btnExportToWord
  2. Text=Export To Excel Id=btnExportToExcel
  3. Text=Export to PDF Id=btnExportToPDF
To Export the gridview data to MicroSoft Word

Write the code on Click Event of btnExportToWord as below:

Response.AddHeader("content-disposition","attachment;filename=Export.doc");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/vnd.word";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        // Create a form to contain the grid        HtmlForm objfrm = new HtmlForm();
        gv.Parent.Controls.Add(objfrm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(gv);
        frm.RenderControl(htmlWrite);
        //GridView1.RenderControl(htw);        Response.Write(stringWrite.ToString());

        Response.End(); 


To Export the gridview data to Microsoft Excel

string attachment = "attachment; filename=ExportFile.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // Create a form to contain the grid        HtmlForm objfrm = new HtmlForm();
        gv.Parent.Controls.Add(objfrm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(gv);
        frm.RenderControl(htw);
        //GridView1.RenderControl(htw);        Response.Write(sw.ToString());        Response.End(); 



To Export the gridview data to PDF

To export Gridivew to PDF this dll can be used. We can download the dll from the below given Url

Link : 
http://sourceforge.net/projects/itextsharp/

add the given namespace

using iTextSharp.text;using iTextSharp.text.pdf;using iTextSharp.text.html;using iTextSharp.text.html.simpleparser;

after downloading add reference of this dll to the project then write the code on click event of btnExportPdf:

Response.ContentType = "application/pdf";
Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition""attachment;filename=Export.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();HtmlTextWriter hw = new HtmlTextWriter(sw);HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);HTMLWorker htmlparser = new HTMLWorker(pdfDoc);PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();











No comments:

Post a Comment