How To Export GridView with Images to Excel, Word and PDF Formats in ASP.Net?

How To Export GridView with Images to Excel, Word  and PDF Formats in ASP.Net?

In this post, you will learn How To Export GridView with Images to Excel, Word  and PDF Formats in ASP.Net.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"

Font-Names = "Arial" >

<Columns>

    <asp:BoundField DataField = "ID" HeaderText = "ID"

     ItemStyle-Height = "150" />

    <asp:BoundField DataField = "FileName" HeaderText = "Image Name"

     ItemStyle-Height = "160"/>

    <asp:TemplateField  ItemStyle-Height = "150" ItemStyle-Width = "180">

        <ItemTemplate>

            <asp:Image ID="Image1" runat="server"

             ImageUrl = '<%# Eval("FilePath", GetUrl("{0}")) %>' />

        </ItemTemplate>

    </asp:TemplateField>

</Columns>

</asp:GridView>

 

protected  string GetUrl(string imagepath)
{
    string[] splits = Request.Url.AbsoluteUri.Split('/');
    if (splits.Length  >= 2)
    {
        string url = splits[0] + "//";
        for (int i = 2; i < splits.Length - 1; i++)
        {
            url += splits[i];
            url += "/";
        }
        return url +  imagepath;
    }
    return imagepath;
}

Download in Excel

 

private void ExcelExport()

{

    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();

}

 

Download in Word

private void WordExport()

{

    Response.Clear();

    Response.Buffer = true;

    Response.AddHeader("content-disposition",

      "attachment;filename=GridViewExport.doc");

    Response.Charset = "";

    Response.ContentType = "application/vnd.ms-word ";

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.AllowPaging = false;

    GridView1.DataBind();

    GridView1.RenderControl(hw);

    Response.Output.Write(sw.ToString());

    Response.Flush();

    Response.End();

}

Download PDF format

using the iTextSharp library, click here

private void PDFExport()

{

    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(); 

}

 

2021-10-03