How to export images in a gridview to Excel for Offline use?

Mar 27, 2008
Reaction score
Dear all,

I'm not a very experienced ASP.NET programmer, although I can find my way in it a bit. Anyway I'm asking for your help, & hoping to find it here. For my company I need to do some tests before I will start with this ASP.NET project.

I'm currently testing with a gridview with 2 columns, one for the category name & one for the picture next to it. I get this data from a MySQL server. Everything works fine, the export is working to excel.I can see both columns with a picture next to it.

Now the reason I have to test this, is that this will be a sheet for our sales people to take with them to our customers. The problem with this is that when the Excel file can't have any connection with the server or the location where the images are stored, it won't display the pictures (broken link with a blank picture & a red cross). I have done some research & haven't found any solution yet, people who asked the same question but never had an answer...

The thing I would like to know is that if there is any solution where the pictures from the datagrid can be embedded in Excel during the Export from the Gridview to Excel, so they are also offline visible. The picture location is stored in this case with <img src... in the MySQL DB.

Below you can find the code I'm using:

<%@ Import Namespace="System.Data.Odbc" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Runtime.InteropServices.Marshal" %>
<%@ Import Namespace="Microsoft.Office.Interop.Excel" %>
<%@ Import Namespace="System.Drawing" %>

<script language="VB" runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ConnectionString As String = "DRIVER={MYSQL ODBC 3.51 Driver}; SERVER=localhost;DATABASE=asp;UID=root;PWD=;OPTION= 1 + 2 + 8 + 32 + 2048 + 16384"
Dim CommandText As String = "Select * from tblCategory"
Dim myConnection As New OdbcConnection(ConnectionString)
Dim myCommand As New OdbcCommand(CommandText, myConnection)
GridView1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
Catch ex As Exception

End Try
End Sub

Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim frm As HtmlForm = New HtmlForm()
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
Response.ContentType = "application/"
Response.AddHeader("Content-Disposition", "attachment;filename=Pricelist.xls;")
Response.Charset = ""
Me.EnableViewState = False
GridView1.RowStyle.Height = Unit.Pixel(50)


End Sub

<html xmlns="">
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<asp:BoundField HeaderText="Category" DataField="nameCategory" />
AlternateText="product picture"



<br />
<asp:Button ID="btnExcel" runat="server" OnClick="btnExcel_Click" Text="Export to Excel" />
<br />


I'm looking forward to your answers & hoping to find a solution from the experts on this board.

Thanks in advance
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Latest member

Latest Threads