Monday 25 March 2013

Export to pdf and export to excel in sharepoint webaparts using itextsharp


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

using System.Collections;
using System.Drawing.Imaging;
using System.Web;
using System.Text;  


protected void btnExportToPdf_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = (DataTable)ViewState["GeneralTask"];
                BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
                iTextSharp.text.Font font = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

                //Create a dummy GridView CssClass="grdStyle"
                GridView GridView1 = new GridView();
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.CssClass = "grdStyle";


                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=ActivityExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        GridView1.RenderControl(hw);
                        StringReader sr = new StringReader(sw.ToString());
                        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                        pdfDoc.SetPageSize(PageSize.A4.Rotate());
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                        pdfDoc.Open();
                        htmlparser.Parse(sr);
                        pdfDoc.Close();
                        Response.Write(pdfDoc);
                        Response.End();
                    }
                }

            }
            catch { }
        }

        protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = (DataTable)ViewState["GeneralTask"];
                GridView GridView1 = new GridView();
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();

                Response.Buffer = true;

                Response.AddHeader("content-disposition", string.Format("attachment;filename=ActivityReport.xls"));
                Response.Charset = "";

                Response.Cache.SetCacheability(HttpCacheability.NoCache);

                Response.ContentType = "application/vnd.xls";
                using (StringWriter stringWrite = new System.IO.StringWriter())
                {
                    using (HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite))
                    {
                        GridView1.RenderControl(htmlWrite);
                        Response.Write(stringWrite.ToString());

                        Response.Flush();
                        Response.Clear();
                        Response.End();
                    }
                }

            }
            catch { }
        }

Find the dll's here :---Itextsharp 5.3.0.0

No comments:

Post a Comment