Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gridview export toexcel not working

Status
Not open for further replies.

dbsquared

Programmer
Nov 7, 2002
175
US
Ihave the below code to export to excel which I got from other posts.
However my output to excel only shows the tags and not just the data.
Any help would be appreciated.
Here is my output from excel:
<form name="_ctl5" method="post" action="DatabaseList.aspx" id="_ctl5">
<input type="hidden" name="__VIEWSTATE" id="
__VIEWSTATE" value="" />
<div>
</div></form>

Code:
  Private Sub Export_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Export.Click
        '//export to excel 
        Me.dgDatabase.AllowPaging = False
        Me.dgDatabase.Columns(0).Visible = False
        Me.dgDatabase.Columns(1).Visible = True
        Me.dgDatabase.Columns(2).Visible = True
        Me.dgDatabase.Columns(3).Visible = True
        Me.dgDatabase.AllowSorting = False

        Me.dgDatabase.DataBind()

        ' the excel expot....
        Response.AddHeader("content-disposition", "attachment;filename=Databaselist.xls")

        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False

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

        Dim frm As HtmlForm = New HtmlForm()
        Dim grd As New GridView

        Me.Controls.Add(frm)
        frm.Controls.Add(dgDatabase)

        frm.RenderControl(hw)
        Response.Write(tw.ToString())
        Response.End()

    End Sub

To go where no programmer has gone before.
 
Code:
        Response.AddHeader("content-disposition", "attachment;filename=Databaselist.xls")

        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Response.Buffer = True  '' You were missing this
        Me.EnableViewState = False

and instead of

Code:
        Dim frm As HtmlForm = New HtmlForm()
        Dim grd As New GridView

        Me.Controls.Add(frm)
        frm.Controls.Add(dgDatabase)

        frm.RenderControl(hw)

perhaps you could take the approach I have:

Code:
... my click event
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

        BusinessFunctions.ClearControls(dgReport);
        dgReport.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();

my ClearControls function

Code:
    public static void ClearControls(Control control)
    {
        for (int i = control.Controls.Count - 1; i >= 0; i--)
        {
            ClearControls(control.Controls[i]);
        }

        if (!(control is TableCell))
        {
            if (control.GetType().GetProperty("SelectedItem") != null)
            {
                LiteralControl literal = new LiteralControl();
                control.Parent.Controls.Add(literal);
                try
                {
                    literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
                }
                catch
                {
                }
                control.Parent.Controls.Remove(control);
            }
            else
                if (control.GetType().GetProperty("Text") != null)
                {
                    LiteralControl literal = new LiteralControl();
                    control.Parent.Controls.Add(literal);
                    literal.Text = (string)control.GetType().GetProperty("Text").GetValue(control, null);
                    control.Parent.Controls.Remove(control);
                }
        }
        return;
    }

works like a charm for me.

 
When I change the code to use the clearcontrols

I get the error: Control 'dgDatabase' of type 'GridView' must be placed inside a form tag with runat=server.

Here is my changed code I had the clear controls from when I used it for datagrids.

Code:
Private Sub Export_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Export.Click
    
        Me.dgDatabase.DataBind()

        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Response.Buffer = True
        Me.EnableViewState = False

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

        ClearControls(dgDatabase)
        dgDatabase.RenderControl(hw)
        Response.Write(tw.ToString())
        Response.End()
end sub


 Sub ClearControls(ByVal control As Control)
        Dim i As Integer
        For i = control.Controls.Count - 1 To 0 Step -1
            ClearControls(control.Controls(i))
        Next i

        If TypeOf control Is System.Web.UI.WebControls.Image Then
            control.Parent.Controls.Remove(control)
        End If

        If (Not TypeOf control Is TableCell) Then
            If Not (control.GetType().GetProperty("SelectedItem") Is Nothing) Then
                Dim literal As New LiteralControl
                control.Parent.Controls.Add(literal)
                Try
                    literal.Text = CStr(control.GetType().GetProperty("SelectedItem").GetValue(control, Nothing))
                Catch
                End Try
                control.Parent.Controls.Remove(control)
            Else
                If Not (control.GetType().GetProperty("Text") Is Nothing) Then
                    Dim literal As New LiteralControl
                    control.Parent.Controls.Add(literal)
                    literal.Text = CStr(control.GetType().GetProperty("Text").GetValue(control, Nothing))
                    control.Parent.Controls.Remove(control)
                End If
            End If
        End If
        Return

    End Sub



To go where no programmer has gone before.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top