Home All Groups Group Topic Archive Search About

problem exporting datagrid to excel

Author
25 Mar 2005 6:45 PM
Dave Bailey via DotNetMonster.com
I am using the following code to export a datagrid to excel from an asp
page:

private void exportButton_Click(object sender, System.EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            hw.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Html);
            this.ClearControls(reportGrid);
            reportGrid.RenderControl(hw);
            hw.RenderEndTag();
            Response.Write(tw);
            Response.End();
        }

When I run this code I only get the first column returned to the
spreadsheet.

The is no other control on the datagrid so I am at a loss.  Any suggestions
would be appreciated.

Thanks,

Dave

--
Message posted via http://www.dotnetmonster.com

Author
25 Mar 2005 9:38 PM
ajamrozek
I'm not too sure how to solve your problem.
I store the dataset, filters, sorts and pageindex of the datagrid in
httpcache and session vars and then create a new datagrid using those
stored objects.  I then use that for my Excel output.
It's in vb (easily translatable to c#, but I'm not gonna do it for you,
sorry)

Private Sub ExcelOut()

            Page.Response.Clear()
            Page.Response.AppendHeader("Content-Disposition",
"attachment; filename=")
            Page.Response.ContentType = "application/vnd.ms-excel"
            'Response.ContentType = "text/csv"

            ' Remove the charset from the Content-Type header.
            Page.Response.Charset = ""

            Dim dsAPL As DataSet =
MyCustomClass.DataAccess.SQLDataSet(_strDataContext, "dbo." &
_strDataContext & "_GET", True), _
                ds As DataSet = New DataSet
            ds = dsAPL.Clone
            ds.Merge(dsAPL)

            If Not Page.Session(_strDataContext & "_Filter") Is Nothing
Then
                ds.Tables(0).DefaultView.RowFilter =
Page.Session(_strDataContext & "_Filter")
            End If

            If Not Page.Session(_strDataContext & "_Sort") Is Nothing
Then
                ds.Tables(0).DefaultView.Sort =
Page.Session(_strDataContext & "_Sort")
            End If

            Dim oItem As ListItem
            For Each oItem In lstDisplayFields.Items
                If Not oItem.Selected Then
                    ds.Tables(0).Columns.Remove(oItem.Value)
                End If
            Next

            Dim tw As New System.IO.StringWriter

            Dim hw As New System.Web.UI.HtmlTextWriter(tw)

            Dim dgGrid As New DataGrid

            dgGrid.DataSource = ds.Tables(0).DefaultView

            dgGrid.DataBind()

            ' Get the HTML for the control.
            dgGrid.RenderControl(hw)

            ' Write the HTML back to the browser.
            Page.Response.Write(tw.ToString())

            ' End the response.
            Page.Response.End()

            ds.Dispose()
            ds = Nothing

        End Sub