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

Joined
Mar 27, 2008
Messages
2
Reaction score
0
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
Try
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)
myConnection.Open()
GridView1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
GridView1.DataBind()
myConnection.Close()
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.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment;filename=Pricelist.xls;")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Me.EnableViewState = False
Controls.Add(frm)
GridView1.RowStyle.Height = Unit.Pixel(50)
frm.Controls.Add(GridView1)
frm.RenderControl(oHtmlTextWriter)



Response.Write(oStringWriter.ToString)

Response.End()
End Sub





</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
</head>
<body>
<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" />
<Columns>
<asp:BoundField HeaderText="Category" DataField="nameCategory" />
<asp:ImageField
HeaderText="Picture"
AlternateText="product picture"
DataImageUrlField="locationPicture"
>

</asp:ImageField>

</Columns>
</asp:GridView>

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

</html>

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

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top