Question Exporting Datagrid to Excel Problem

Mosteck

Active member
Joined
Nov 23, 2008
Messages
31
Location
Toronto
Programming Experience
3-5
I'm having a problem exporting my datagrid to an Excel file.

The code I'm using is:

VB.NET:
Protected Sub ExportToExcelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExportToExcelButton.Click

        If InvTotGridView.Rows.Count + 1 < 65536 Then

            Dim FileName As String = "attachment;ReportInvStatus - " & Now.ToString("yyyy/MM/dd") & ".xls"
            Dim tw As New StringWriter()
            Dim hw As New System.Web.UI.HtmlTextWriter(tw)
            Dim frm As HtmlForm = New HtmlForm()
            Page.Response.ContentType = "application/vnd.ms-excel"
            Page.Response.AddHeader("content-disposition", FileName = " & filename & """)
            Page.Response.Charset = ""
            Page.EnableViewState = False
            frm.Attributes("runat") = "server"
            Controls.Add(frm)

            frm.Controls.Add(InvTotGridView)
            frm.RenderControl(hw)
            Response.Write(tw.ToString())
            Response.End()

        Else

            ExportErrorMsgLabel.Visible = True
            ExportErrorMsgLabel.Text = "TOO MUCH DATA TO EXPORT..  UNABLE TO COMPLETE REQUEST"

        End If

    End Sub

When I open the excel spreadsheet, I'm getting the following:

<form name="ctl01" method="post" action="ReportInvStatus.aspx" id="ctl01" runat="server">
<div>
<input type="hidden" name="__VIEWSTATE" id="
__VIEWSTATE" value="" />
</div>
<div>

</div></form>

What's wrong with this? I can't see it.....
 
Anyone? I've search everywhere trying to find a solution to this. The same code works on another page, but not on this one....

Please help.
 
Back
Top