You must Sign In to post a response.
  • Category: ASP.NET

    How to Export Records to Pdf or Excel. With Images

    Hi ,Developers
    i am creating a College based application.
    now i need to give students details i.e. Stdent_id and student Photo .
    I am using gridview. I am binding all the details in a gridview .
    Without image means i can export xls or Pdf easily.
    but i try to export with images na i can't able to export xls or Pdf.just the page only loading.
    So anyone knows please help me to how i am export Records with images using Asp.Net C#.

    Thanking you,
    Paul.S
  • #765964
    To export image in gridview we need, the image needs to be present In gridview, so the following code will work as expected
    see below code

    private void Excel_Export()
    {
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition",
    "attachment;filename=GridViewExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
    GridViewRow row = GridView1.Rows[i];
    //Apply text style to each Row
    row.Attributes.Add("class", "textmode");
    }
    GridView1.RenderControl(hw);

    //style to format numbers to string
    string style = @"<style> .textmode { mso-number-format:\@; } </style>";
    Response.Write(style);
    Response.Output.Write(sw.ToString());
    Response.Flush();
    Response.End();
    }

    for more details switch to below link
    http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765984
    Hi Paul,

    If you want to export gridview data into pdf/ excel with images, refer below link this might be helpful to you.

    http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765987
    How you are trying to export the PDF. Are you using some 3rd part tools?
    You can go for open XML. There are lot of options.
    Following is the link for dowload it.

    https://msdn.microsoft.com/en-us/library/office/bb448854.aspx

    Following are the sample code using Open XML

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition",
    "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.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();

    By Nathan
    Direction is important than speed

  • #766016
    Here's a different approach to export from GridView to PDF or Excel. You can print from GridView to a virtual printer. You can then create an Excel or PDF file from the print job. Here are some helpful links:
    http://www.codeproject.com/Articles/1004187/Extend-Existing-Applications-with-the-LEADTOOLS-Vi

    http://www.aspsnippets.com/Articles/Export-GridView-To-WordExcelPDFCSV-in-ASP.Net.aspx


Sign In to post your comments