Hi Waghmare,
I have modified your code please check.
HTML
<div>
<asp:GridView ID="gridInformation" runat="server" AutoGenerateColumns="false" Font-Names="Arial"
OnRowDataBound="gridInformation_RowDataBound">
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="EmployeeID" />
<asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" />
<asp:TemplateField HeaderText="Pics" ItemStyle-Height="160" ItemStyle-Width="180">
<ItemTemplate>
<asp:Image ID="empimage" runat="server" ImageUrl='<%# Eval("FilePath", GetUrl("{0}")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExportWord" CommandArgument="Word" runat="server" Text="Export-Word"
OnClick="Export_Grid" />
<asp:Button ID="btnExportExcel" CommandArgument="Excel" runat="server" Text="Export-Excel"
OnClick="Export_Grid" />
<asp:Button ID="btnExportPDF" CommandArgument="PDF" runat="server" Text="Export-PDF"
OnClick="Export_Grid" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EmployeeId", typeof(int));
dt.Columns.Add("EmployeeName", typeof(string));
dt.Columns.Add("FilePath", typeof(string));
dt.Rows.Add(1, "Peter", "Files/00000001.jpg");
dt.Rows.Add(2, "John", "Files/00000002.jpg");
dt.Rows.Add(18, "Thomas Hardy", "Files/00000018.jpg");
dt.Rows.Add(101, "Christina Berglund", "Files/00000101.jpg");
dt.Rows.Add(1001, "Yang Wang", "Files/00001001.jpg");
gridInformation.DataSource = dt;
gridInformation.DataBind();
}
protected void Export_Grid(object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.CommandArgument)
{
case "Word":
Word_Export();
break;
case "Excel":
Excel_Export();
break;
case "PDF":
PDF_Export();
break;
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
private void Word_Export()
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=EmployeeInfo.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridInformation.AllowPaging = false;
gridInformation.DataBind();
gridInformation.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
catch (Exception)
{
}
}
private void Excel_Export()
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=EmployeeInfo.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridInformation.AllowPaging = false;
gridInformation.DataBind();
for (int i = 0; i < gridInformation.Rows.Count; i++)
{
GridViewRow row = gridInformation.Rows[i];
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
}
gridInformation.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();
}
catch (Exception)
{
}
}
private void PDF_Export()
{
try
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=EmployeeInfo.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gridInformation.AllowPaging = false;
gridInformation.DataBind();
gridInformation.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();
}
catch (Exception)
{
}
}
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;
}
protected void gridInformation_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
}
Screenshot
I hope works for you.