Home All Groups Group Topic Archive Search About
Author
7 Mar 2005 7:21 PM
Mike Lord via .NET 247
Hi all,

This site and all its users have been a huge help, and now I need a little help.

I've got a page with a DataGrid on it that is formatted and beautiful the way I like it, and I'm trying to export the DataGrid to excel, but not having any luck. On this particular page I'm not using a code behind page, so I've the put the export routine in a Sub that's called by a button to export the data. I'm not getting any errors, but when you click the button the entire page goes blank.

Here's the Sub:

Sub DataGridToExcel(s as object, e as eventargs)
    Response.Clear()
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Dim stringWrite as New System.IO.StringWriter()
    Dim htmlWrite as New System.Web.UI.HtmlTextWriter(stringWrite)
    dim dg As New DataGrid()
    dg = dgrdApproval
    dg.Gridlines = Gridlines.None
    dg.HeaderStyle.Font.Bold = True
    dg.DataBind()
    dg.RenderControl(htmlWrite)
    Response.Write(stringWrite.ToString)
    Response.End()
End Sub

Any ideas why this doesn't open a "Save As" dialog?

--------------------------------
From: Mike Lord

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>ogV1/WSo1Uiy8P4UdOn0vQ==</Id>

Author
7 Mar 2005 8:59 PM
Elton Wang
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????.??????????????????????????>????????????????????????????????????????????????????????????????????????>
Author
8 Mar 2005 1:11 PM
Elton Wang
??????????????????????????????????????????????????????????????????????????????.?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
Author
8 Mar 2005 1:17 PM
Etlon Wang
Something very weird.

>-----Original Message-----
>Hi all,
>
>This site and all its users have been a huge help, and
now I need a little help.
>
>I've got a page with a DataGrid on it that is formatted
and beautiful the way I like it, and I'm trying to export
the DataGrid to excel, but not having any luck. On this
particular page I'm not using a code behind page, so I've
the put the export routine in a Sub that's called by a
button to export the data. I'm not getting any errors, but
when you click the button the entire page goes blank.
Show quoteHide quote
>
>Here's the Sub:
>
>Sub DataGridToExcel(s as object, e as eventargs)
>    Response.Clear()
>    Response.Charset = ""
>    Response.ContentType = "application/vnd.ms-excel"
>    Dim stringWrite as New System.IO.StringWriter()
>    Dim htmlWrite as New System.Web.UI.HtmlTextWriter
(stringWrite)
>    dim dg As New DataGrid()
>    dg = dgrdApproval
>    dg.Gridlines = Gridlines.None
>    dg.HeaderStyle.Font.Bold = True
>    dg.DataBind()
>    dg.RenderControl(htmlWrite)
>    Response.Write(stringWrite.ToString)
>    Response.End()
>End Sub
>
>Any ideas why this doesn't open a "Save As" dialog?
>
>--------------------------------
>From: Mike Lord
>
>-----------------------
>Posted by a user from .NET 247 (http://www.dotnet247.com/)
>
><Id>ogV1/WSo1Uiy8P4UdOn0vQ==</Id>
>.
>
Author
9 Mar 2005 1:29 AM
Ken Cox [Microsoft MVP]
The Web interface acts up sometimes and inserts question marks instead of
the real text.


Show quoteHide quote
"Etlon Wang" <anonym***@discussions.microsoft.com> wrote in message
news:5a7701c523e1$239f1890$a401280a@phx.gbl...
> Something very weird.
Author
9 Mar 2005 5:04 PM
ajamrozek
I think there needs to the Repsonse.AppendHeader() line.
I'm storing the data for my datagrid in the http cache, but you should
be able to get it from where ever, anyway here's my code for doing
this:
Private Sub ExcelOut()

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

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

'get your dataset however you wish, this how i get mine
        Dim dsAPL As DataSet =
QwestProcurementAPL.DataAccess.DataSet("APL", , True), _
            ds As DataSet = New DataSet
        ds = dsAPL.Clone
        ds.Merge(dsAPL)

'if there's any filter or sort to add, do so
        If Not Session("Filter") Is Nothing Then
            ds.Tables(0).DefaultView.RowFilter = Session("Filter")
        End If

        If Not Session("sort") Is Nothing Then
            ds.Tables(0).DefaultView.Sort = Session("Sort")
        End If

'my datagrid is built based on a listbox of fields to display, so I
basically
'conform my new dataset here to the fields that the listbox has
selected

        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

'here's the IO code

        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.
        Response.Write(tw.ToString())

        ' End the response.
        Response.End()

        ds.Dispose()
        ds = Nothing

    End Sub

HTH