Home > Forum > ASP.NET > How to export gridview images column to excel file in asp.net?

How to export gridview images column to excel file in asp.net?

by Thamil   on Jun 27, 2013   Category: ASP.NET  |  Views: 10281    |  Points: 5   |  Gold 
I want to export the grdivew data into excel file, in this case gridview containing the image column so need to export data and image column as well.

-question posted on behalf of Umair Kasmani.

Bookmark and Share:
  User Reply  | Ask a question  |   Reply 
  Re :How to export gridview images column to excel file in asp.net?   
by Thamil
on Jun 27 2013 12:31AM
Points : 10
Hello Umair,

Please follow the below steps to resove your issue, here im providing you sample page and code snippets.

Design the aspx page as like below

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewSample.aspx.cs" Inherits="WebApplication1.GridviewSample" EnableEventValidation = "false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Job" HeaderText="Job" />
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:ImageField DataImageUrlField="Image" HeaderText="Image">


<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Code behind part

protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
GridView1.DataSource = GetTable();
static DataTable GetData()
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Job", typeof(string));
table.Columns.Add("Location", typeof(string));

table.Rows.Add("JP", "XXX", "QQQQ", "http://localhost:4832/images/JP.png");
table.Rows.Add("HP", "TTT", "AAAA", "http://localhost:4832/images/HP.png");
table.Rows.Add("SQ", "YYY", "HHHH", "http://localhost:4832/images/SQ.png");
table.Rows.Add("XS", "EEE", "UUUU", "http://localhost:4832/images/XS.png");
return table;

protected void Button1_Click(object sender, EventArgs e)
Response.AddHeader("content-disposition", "attachment; filename=test.xls");
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
public override void VerifyRenderingInServerForm(Control control)


In the above code, i have created the dataTable and loaded the data then binded to gridview control. In the gridview control added the ImageField to display the images. Final part is export to excel, using gridview rendercontrol, get html content of gridview and write into output stream.

Recent Post

Latest Posts