I have been having a little trouble exporting a datagrid into an excel file. The issue at hand is that there is a column in my datagrid that is set up as a LinkButton in a TemplateColumn. When I expot the datagrid into excel I get ever column except for the template column. It just leaves that column blank. I was wondering what I could do to get the column to export into the excel file.
Here is the code for the template column
<asp:TemplateColumn HeaderText="Category" SortExpression="CTGY_NBR">
<HeaderStyle Width="6%"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton Runat="server" ID="lnkCategory"
CommandName="CatDrillDown" CommandArgument='<%#
container.dataitem("CTGY_NBR") %>' >
<%# container.dataitem("CTGY_NBR") %>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
Here is my export function:
Private Sub ImgbtnExport_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImgBtnExport.Click
Dim objExport As New clsDS()
Try
If dg.Visible = True Then
objExport.ExportToExcel(dg, Response, "Weekly Store OOS by Category")
Else
objExport.ExportToExcel(dgCtgy, Response, "Weekly Store OOS by Category")
End If
Catch ex As Exception
Handleerror(ex)
Finally
objExport = Nothing
End Try
End Sub
Public Sub ExportToExcel(ByVal dg As DataGrid, _
ByVal aResponse As HttpResponse, _
Optional ByVal asFilename As String = "")
Try
aResponse.Clear()
aResponse.ContentType = "application/vnd.ms-excel"
aResponse.AddHeader("Content-Disposition", "attachment;filename=" & asFilename & ".xls;")
aResponse.Charset = ""
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
dg.GridLines = GridLines.Both
Me.ClearControls(dg)
dg.HeaderStyle.Font.Bold = True
dg.RenderControl(hw)
dg.GridLines = GridLines.Both
aResponse.Write(tw.ToString())
aResponse.Flush()
aResponse.Close()
Catch err As Exception
Throw err
End Try
End Sub