Solved, vb - Export to excel

Posted on 2010-09-15
Last Modified: 2012-06-22
I found the following code on the web to export the contents of an gridview to Excel.
It works fine, but how can I make also export the text from other controls? Eg. a label that sits above my gridview and a table and a textbox that sits below the gridview?
Partial Class exportToExcel

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        '// Disable paging

        GridView1.AllowPaging = False

        '// exluded columns arraylist

        Dim defaultExcludedColumns As New ArrayList()

        '// Always exclude these columns


        '// Send to base Excel export method 

        ExportGridView(GridView1, "Excel-Name", defaultExcludedColumns)

        '// Rebind with paging enabled

        GridView1.AllowPaging = True

    End Sub

    '/// <summary>

    '/// Export GridView data to Excel.

    '/// </summary>

    '/// <param name="grdView">GridView control to export.</param>

    '/// <param name="filename">Filename of excel spreadsheet.</param>

    '/// <param name="excludedColumnList">ArrayList of columns to exlude.</param>

    Sub ExportGridView(ByVal grdView As GridView, ByVal filename As String, ByVal excludedColumnList As ArrayList)

        '// Clear response content & headers




        '// Add header

        Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls")

        Response.Charset = String.Empty


        Response.ContentType = "application/vnd.xls"

        '// Create stringWriter

        Dim stringWrite As New System.IO.StringWriter()

        '// Create HtmlTextWriter

        Dim htmlWrite As New HtmlTextWriter(stringWrite)

        '// Remove controls from Column Headers

        If (grdView.HeaderRow IsNot Nothing And grdView.HeaderRow.Cells IsNot Nothing) Then

            Dim ct As Integer

            For ct = 0 To grdView.HeaderRow.Cells.Count - 1 Step ct + 1

                ' Save initial text if found

                Dim headerText As String = grdView.HeaderRow.Cells(ct).Text

                ' Check for controls in header

                If grdView.HeaderRow.Cells(ct).HasControls() Then

                    ' Check for link button

                    If grdView.HeaderRow.Cells(ct).Controls(0).GetType().ToString() = "System.Web.UI.WebControls.DataControlLinkButton" Then

                        ' link button found, get text

                        headerText = (CType(grdView.HeaderRow.Cells(ct).Controls(0), LinkButton)).Text

                    End If

                    ' Remove controls from header


                End If

                ' Reassign header text

                grdView.HeaderRow.Cells(ct).Text = headerText


        End If

        '// Remove footer

        If (grdView.FooterRow IsNot DBNull.Value) Then

            grdView.FooterRow.Visible = False

        End If

        '// Remove unwanted columns (header text listed in removeColumnList arraylist)

        Dim field As DataControlField

        For Each field In grdView.Columns

            If (excludedColumnList.Contains(field.HeaderText)) Then

                field.Visible = False

            End If


        '// Call gridview's renderControl


        '// Write Response to browser



    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)


    End Sub

End Class

Open in new window

Question by:Rick

Accepted Solution

Varghese Jacob earned 500 total points
ID: 33689282
You have to use Response.Write() to put what ever you want before and after the below lines in the code snippet mentioned above.

        '// Write Response to browser

LVL 29

Expert Comment

by:Kumaraswamy R
ID: 33690213
LVL 13

Author Closing Comment

ID: 33691550
That's right. I remember doing this about 2 yrs. ago with Windows forms.   Thanks!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now