locked
Exporting Gridview to Excel RRS feed

  • Question

  • User-511995934 posted

    I am attempting to export a gridview's data to an excel spreadsheet. I have found all kinds of examples online. So far, every example I've tried gives me the following error:

    "Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."

    yet....the gridview IS inside a form tag with the runat="server" set correctly.

    any ideas?

    here's the source for the page:

    and here's the error:

    Wednesday, October 10, 2012 4:34 PM

Answers

  • User622904130 posted

    you need to call this function first like such i believe:

            gv.AllowPaging = False
            gv.DataBind()
    
            PrepareGridViewForExport(gv)
            gv.RenderControl(htmlWrite)
    
            Response.Write(stringWrite.ToString())
            Response.End()
    
            Response.Write(stringWrite.ToString())
            Response.End()

     

      Private Sub PrepareGridViewForExport(ByVal gv As Control)
            Dim lb As New LinkButton()
            Dim l As New Literal()
            Dim name As String = [String].Empty
            Dim i As Integer = 0
            While i < gv.Controls.Count
                If TypeOf gv.Controls(i) Is LinkButton Then
                    l.Text = (TryCast(gv.Controls(i), LinkButton)).Text
                    gv.Controls.Remove(gv.Controls(i))
                    gv.Controls.AddAt(i, l)
                ElseIf TypeOf gv.Controls(i) Is DropDownList Then
                    l.Text = (TryCast(gv.Controls(i), DropDownList)).SelectedItem.Text
                    gv.Controls.Remove(gv.Controls(i))
                    gv.Controls.AddAt(i, l)
                ElseIf TypeOf gv.Controls(i) Is CheckBox Then
                    l.Text = If((TryCast(gv.Controls(i), CheckBox)).Checked, "True", "False")
                    gv.Controls.Remove(gv.Controls(i))
                    gv.Controls.AddAt(i, l)
                End If
                If gv.Controls(i).HasControls() Then
                    PrepareGridViewForExport(gv.Controls(i))
                End If
                System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
            End While
        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 13, 2012 3:38 PM

All replies

  • User-595745061 posted

    Instead of exporting Gridview to excel, you should export the datasource for gridview to excel..

    Wednesday, October 10, 2012 5:46 PM
  • User-1378764701 posted

    Hi,,

    refer the below link..

    http://csharpdotnetfreak.blogspot.com/2011/10/export-gridview-to-excel.html

    Regards

    Saturday, October 13, 2012 1:55 PM
  • User-1378764701 posted

    Hi,,

    refer the below link..

    http://csharpdotnetfreak.blogspot.com/2011/10/export-gridview-to-excel.html

    Regards

    Saturday, October 13, 2012 1:55 PM
  • User622904130 posted

    you need to call this function first like such i believe:

            gv.AllowPaging = False
            gv.DataBind()
    
            PrepareGridViewForExport(gv)
            gv.RenderControl(htmlWrite)
    
            Response.Write(stringWrite.ToString())
            Response.End()
    
            Response.Write(stringWrite.ToString())
            Response.End()

     

      Private Sub PrepareGridViewForExport(ByVal gv As Control)
            Dim lb As New LinkButton()
            Dim l As New Literal()
            Dim name As String = [String].Empty
            Dim i As Integer = 0
            While i < gv.Controls.Count
                If TypeOf gv.Controls(i) Is LinkButton Then
                    l.Text = (TryCast(gv.Controls(i), LinkButton)).Text
                    gv.Controls.Remove(gv.Controls(i))
                    gv.Controls.AddAt(i, l)
                ElseIf TypeOf gv.Controls(i) Is DropDownList Then
                    l.Text = (TryCast(gv.Controls(i), DropDownList)).SelectedItem.Text
                    gv.Controls.Remove(gv.Controls(i))
                    gv.Controls.AddAt(i, l)
                ElseIf TypeOf gv.Controls(i) Is CheckBox Then
                    l.Text = If((TryCast(gv.Controls(i), CheckBox)).Checked, "True", "False")
                    gv.Controls.Remove(gv.Controls(i))
                    gv.Controls.AddAt(i, l)
                End If
                If gv.Controls(i).HasControls() Then
                    PrepareGridViewForExport(gv.Controls(i))
                End If
                System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
            End While
        End Sub

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 13, 2012 3:38 PM