Avatar of SirReadAlot
SirReadAlot
 asked on

Export gridview contents to PDF with c#

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

C#ASP.NET

Avatar of undefined
Last Comment
Kumaraswamy R

8/22/2022 - Mon
niralshah

ASKER CERTIFIED SOLUTION
imdadhusen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SirReadAlot

ASKER
okay I will try all solns
Kumaraswamy R

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes