Click here to Skip to main content
15,889,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I have the gridview with asp:image in template column to showing the RAG status. I need to export gridview data with the image into excel.

The image is not displaying in the excel.

Regards
Sheik
Posted
Comments
sriman.ch 21-Dec-11 0:57am    
How you are binding the Image url ?

 
Share this answer
 
Hi,

Try the following also

XML
<asp:TemplateField ItemStyle-CssClass="rownum" HeaderText="Images">
                    <ItemTemplate>
                        <asp:Image ID="imgTest" runat="server" ImageUrl='<%#Server.MapPath("~/Styles/Images/Desert.jpg")%>' />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" Width="10%" />
                </asp:TemplateField>



Code Behind:

string attachment = "attachment; filename=" + Server.MapPath(FileName); ;
            Response.AddHeader("content-disposition", attachment);
            //Response.TransmitFile(Server.MapPath(FileName));
            switch (Format)
            {
                case "Excel":
                    Response.ContentType = "application/vnd.ms-excel";
                    //"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;
                case "Word":
                    Response.ContentType = "application/vnd.word";
                    break;
                           }

            StringWriter stw = new StringWriter();
            HtmlTextWriter htextw = new HtmlTextWriter(stw);
            gvEmployee.RenderControl(htextw);
            StreamReader sr = new StreamReader(Server.MapPath("../Styles/grid.css"));
            string s = sr.ReadToEnd();
            Response.Write("<HEAD><STYLE>");
            Response.Write(s.ToString());
            Response.Write("</STYLE></HEAD>");
            Response.Write(stw.ToString());
            Response.End();
            sr.Close();


The above code is for exporting gridview with css. Bind your image url correctly

Hope this helps.
 
Share this answer
 
refer this link to solve your problem...

http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx
 
Share this answer
 
write it on your .aspx page :
VB
<asp:ImageButton ID="btnExportToExcelAbv" ImageUrl="~/Images/export-to-excel.gif"
                                            runat="server" OnClick="btnExportToExcel_Click" />
<br />
<div id="dvPrintXLFull" runat="server">
<asp:gridview id="grd1" runat="server" xmlns:asp="#unknown"></asp:gridview>
</div>


and code behind is after binding the grid :

C#
protected void btnExportToExcel_Click(object sender, ImageClickEventArgs e)
    {
GridExportToExcel();
}

public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for the
    }

  
    private void GridExportToExcel()
    {
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename= Stock_Ledger.xls");
        Response.Charset = "";
        
        Response.ContentType = "application/vnd.xls";
        
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        
        #region ExcelExportCode

        dvPrintXLFull.RenderControl(htmlWrite);
        /
        Response.Write(stringWrite.ToString());
        Response.End();
        #endregion
    }

Hope this will help you.
Don't forget to mark as answer if it helps. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900