Solved

Export gridview contents to PDF with c#

Posted on 2010-11-12
5
1,284 Views
Last Modified: 2012-05-10
Hi Experts,

just trying to achieve the above with these codes

but i get a message saying "Adobe Reader could not open the file because it is either not a supported file type or the file has been damaged"

Do I need an extract component installed on my machine?

thanks

public static void ExportPDF(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/pdf;";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();
                //  include the gridline settings

                table.GridLines = gv.GridLines;
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
    #endregion


========================================================= 

protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.rdoStudentListExportOptions.SelectedIndex == 1)
        {
            //  the user wants all rows exported, turn off paging
            //  and rebing the grid before sending it to the export  utility
            this.gvAcademy.AllowPaging = false;
            this.gvAcademy.DataBind();
        }
        else if (this.rdoStudentListExportOptions.SelectedIndex == 2)
        {
            //  the user wants just the first 2000,
            //  adjust the PageSize and rebind
            this.gvAcademy.PageSize = 2000;
            this.gvAcademy.DataBind();
        }
        //  pass the grid that for exporting ...
        GridViewExportUtil.ExportPDF("HE_STUDENTS.pdf", this.gvAcademy);
    }

Open in new window

0
Comment
Question by:SirReadAlot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Expert Comment

by:niralshah
ID: 34119164
0
 
LVL 1

Accepted Solution

by:
imdadhusen earned 500 total points
ID: 34119197
Hi

Use following code to Export Gridview to PDF
Order.aspx contain Gridview
attached itextsharp.dll is a free dll
following code in HTMLtoPDF.aspx.cs to generate PDF
// Create instance of a new document
            Document doc = new Document(PageSize.A4, 10, 10, 50, 50);
            // Change   the content type to application/pdf !Important
            HttpContext.Current.Response.ContentType = "application/pdf";
            // Get   Instance of pdfWriter to be able to create 
            // the document in the OutputStream
            PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);
            // Create   Style Sheet
            StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            //--styles.LoadTagStyle("ol", "leading", "16,0");
            doc.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Style.css"));
            // Open the   document to be able to write to it
            doc.Open();
            //--styles.LoadTagStyle("li", "face", "garamond");
            //--styles.LoadTagStyle("span", "size", "8px");
            //--styles.LoadTagStyle("body", "font-family", "times new roman");
            //--styles.LoadTagStyle("body", "font-size", "10px");
            // Parse   html to PDF understandable objects
            var objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(Server.MapPath("Order.aspx"), Encoding.Default), styles);
            for (int k = 0; k < objects.Count; k++)
            {
                doc.Add((IElement)objects[k]);
            }
            // Close   the document, rendering it to the browser
            doc.Close();

Open in new window

itextsharp.dll
0
 

Author Comment

by:SirReadAlot
ID: 34119206
okay I will try all solns
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34317310
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC - Views 3 38
C# class library debugging - Breakpoint will not hit. 8 30
Need help with a query 14 36
Set the FileVersion of a website dll 1 21
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question