We have an asp.net web application that we want to export a GridView that includes an image column to Excel. Our code works fine but now we added an image control and we want the actual picture to export. Below is our GridView and code that is working but I am not sure how to code for the ImageButton control (we are open to using just an Image control if that would work better but I think the concept is the same. If I export the URL for the image it works fine but does not export the actual image.
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlPartInventory" AutoGenerateColumns="False" DataKeyNames="InventoryID" Visible="True">
<Columns>
<asp:BoundField DataField="InventoryID" HeaderText="InventoryID" InsertVisible="False" ReadOnly="True" SortExpression="InventoryID" />
<asp:BoundField DataField="PartNumber" HeaderText="PartNumber" SortExpression="PartNumber" />
<asp:BoundField DataField="PartDescription" HeaderText="PartDescription" SortExpression="PartDescription" />
<asp:BoundField DataField="VehicleYear" HeaderText="VehicleYear" SortExpression="VehicleYear" />
<asp:BoundField DataField="VehicleMake" HeaderText="VehicleMake" SortExpression="VehicleMake" />
<asp:BoundField DataField="VehicleModel" HeaderText="VehicleModel" SortExpression="VehicleModel" />
<asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
<asp:BoundField DataField="SellingPrice" HeaderText="SellingPrice" SortExpression="SellingPrice" />
<asp:BoundField DataField="PartCost" HeaderText="PartCost" SortExpression="PartCost" />
<asp:BoundField DataField="PartQuality" HeaderText="PartQuality" SortExpression="PartQuality" />
<asp:BoundField DataField="QualityText" HeaderText="QualityText" ReadOnly="True" SortExpression="QualityText" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="RepairOrderID" HeaderText="RepairOrderID" SortExpression="RepairOrderID" />
<asp:CheckBoxField DataField="Available" HeaderText="Available" SortExpression="Available" />
<asp:BoundField DataField="PartNotes" HeaderText="PartNotes" SortExpression="PartNotes" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="IBtnPhoto" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim img As ImageButton = e.Row.FindControl("IBtnPhoto")
Dim strurl As String = ConfigurationManager.AppSettings("appurl")
Dim strurlPhy As String = Server.MapPath("~/Photos/_Parts/") & DataBinder.Eval(e.Row.DataItem, "InventoryID") & ".jpg"
If File.Exists(strurlPhy) = False Then
img.CssClass = "hide"
Else
img.ImageUrl = "../Photos/_Parts/" & DataBinder.Eval(e.Row.DataItem, "InventoryID") & ".jpg"
End If
img.AlternateText = DataBinder.Eval(e.Row.DataItem, "InventoryID")
End If
End Sub
Private Sub LBtnExport_Click(sender As Object, e As EventArgs) Handles LBtnExport.Click
GridView1.Visible = True
GridView1.DataBind()
UtilClass.Export("MABParts.xls", GridView1, "Internal Parts Inventory")
End Sub
Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView, ByVal strTitle As String)
If gv.AllowPaging = True Then
gv.AllowPaging = False
End If
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
'HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Dim sw As StringWriter = New StringWriter
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
' Create a form to contain the grid
Dim table As Table = New Table
table.GridLines = gv.GridLines
If strTitle <> "" Then
strTitle = "<p><b>" & strTitle & "</b></p>"
sw.Write(strTitle)
End If
' add the header row to the table
If (Not (gv.HeaderRow) Is Nothing) Then
UtilClass.PrepareControlForExport(gv.HeaderRow)
table.Rows.Add(gv.HeaderRow)
End If
' add each of the data rows to the table
For Each row As GridViewRow In gv.Rows
UtilClass.PrepareControlForExport(row)
table.Rows.Add(row)
Next
' add the footer row to the table
If (Not (gv.FooterRow) Is Nothing) Then
UtilClass.PrepareControlForExport(gv.FooterRow)
table.Rows.Add(gv.FooterRow)
End If
' render the table into the htmlwriter
table.RenderControl(htw)
' render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString)
HttpContext.Current.Response.End()
End Sub
Private Shared Sub PrepareControlForExport(ByVal control As Control)
Dim i As Integer = 0
Do While (i < control.Controls.Count)
Dim current As Control = control.Controls(i)
If current.Visible Then
If (TypeOf current Is LinkButton) Then
control.Controls.Remove(current)
control.Controls.AddAt(i, New LiteralControl(CType(current, LinkButton).Text))
ElseIf (TypeOf current Is ImageButton) Then
control.Controls.Remove(current)
control.Controls.AddAt(i, New LiteralControl(CType(current, ImageButton).AlternateText))
ElseIf (TypeOf current Is HyperLink) Then
control.Controls.Remove(current)
control.Controls.AddAt(i, New LiteralControl(CType(current, HyperLink).Text))
ElseIf (TypeOf current Is DropDownList) Then
control.Controls.Remove(current)
control.Controls.AddAt(i, New LiteralControl(CType(current, DropDownList).SelectedItem.Text))
ElseIf (TypeOf current Is CheckBox) Then
control.Controls.Remove(current)
control.Controls.AddAt(i, New LiteralControl(CType(current, CheckBox).Checked))
'TODO: Warning!!!, inline IF is not supported ?
End If
If current.HasControls Then
UtilClass.PrepareControlForExport(current)
End If
End If
i = (i + 1)
Loop
End Sub
Hi @David Chase ,
The sample I posted you that I only want to tell you how to convert relative path to full path. And if you have multiple images,you could for loop the gridview row.
Best regards,
Yijing Sun
OK, but how does Excel know that it is an image? In my sample I was exporting the alternate text and I even tried the URL but both resulted in text displaying in Excel.
Hi @David Chase ,
When the Export Button is clicked, the GridView will be rendered as an HTML string which will be later written to Response Stream and ultimately downloaded as Excel file.
If you don't show image in the excel,you may don't written into Response Stream.You could refer to below article:
https://www.aspsnippets.com/Articles/Export-GridView-with-Images-from-database-to-Word-Excel-and-PDF-Formats.aspx
Best regards,
Yijing Sun
Sign in to comment