-1

Brief description of what I want to do : I am exporting Data from a gridview to Excel, which works fine, but there are certain columns hidden on the gridview, that are not hidden in the extract(Used some code that I found while doing research). So I want to compare the Column names to the Control Text value to know which one to not include in the Header that is being exported. Hope this makes sense. So here is my Code :

Dim sgv As GridView = CType(ContentPlaceHolder_body.FindControl("SummaryGridView"), GridView)
sgv.AllowPaging = False
sgv.DataBind()

sExportFileName = Path.GetFileName(Request.PhysicalPath)
        sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"

Export(sExportFileName, sgv)

Public Shared Sub Export(ByVal fileName As String, ByVal gv As GridView)
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fileName))
    HttpContext.Current.Response.ContentType = "application/ms-excel"
    Dim sw As StringWriter = New StringWriter
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    Dim HeaderName
    Dim sRowData As String = ""
    '  Create a form to contain the grid
    Dim table As Table = New Table
    table.GridLines = gv.GridLines
    '  add the header row to the table

    If (Not (gv.HeaderRow) Is Nothing) Then
        PrepareControlForExport(gv.HeaderRow, gv)
        table.Rows.Add(gv.HeaderRow)
    End If
    '  add each of the data rows to the table
    For Each row As GridViewRow In gv.Rows
        PrepareControlForExport(row)
        table.Rows.Add(row)
    Next
    '  add the footer row to the table
    If (Not (gv.FooterRow) Is Nothing) Then
        PrepareControlForExport(gv.FooterRow, gv)
        table.Rows.Add(gv.FooterRow)
    End If
    '  render the table into the htmlwriter
    table.RenderControl(htw)
    '  render the htmlwriter into the response
    HttpContext.Current.Response.Write(sw.ToString)
    HttpContext.Current.Response.End()
End Sub

' Replace any of the contained controls with literals

Private Shared Sub PrepareControlForExport(ByVal control As Control, ByVal gv As GridView)
    Dim i As Integer = 0

    Do While (i < control.Controls.Count)
        Dim current As Control = control.Controls(i)
        If (TypeOf current Is LinkButton) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, LinkButton).Text))
        ElseIf (TypeOf current Is ImageButton) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, ImageButton).AlternateText))
        ElseIf (TypeOf current Is HyperLink) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, HyperLink).Text))
        ElseIf (TypeOf current Is DropDownList) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, DropDownList).SelectedItem.Text))
        ElseIf (TypeOf current Is CheckBox) Then
            control.Controls.Remove(current)
            control.Controls.AddAt(i, New LiteralControl(CType(current, CheckBox).Checked))
            'TODO: Warning!!!, inline IF is not supported ?
        ElseIf (TypeOf current Is DataControlFieldHeaderCell) Then

''''''''''''SO WHAT I WANT TO DO HERE IS COMPARE THE 'CURRENT' CONTROL TEXT VALUE TO THE gv.Columns(k).HeaderText VALUE - IF THEY MATCH, THEN REMOVE

For k As Integer = 0 To gv.Rows.Count - 1
                If (current text value) = gv.Columns(k).HeaderText Then
                    If Not gv.Columns(k).Visible Then
                        control.Controls.Remove(current)
                        control.Controls.AddAt(i, New LiteralControl(CType(current, DataControlFieldHeaderCell).Text))
                    End If
                End If

            Next
        End If
        If current.HasControls Then
            JSInternal_Report_DateOnlyRefined.PrepareControlForExport(current, gv)
        End If
        i = (i + 1)
    Loop
End Sub

Hope this makes sense.

Thanks in advance for the help.

AxleWack
  • 1,801
  • 1
  • 19
  • 51
  • I should add that I am running this from a MasterPage. Gridview is on the page using the masterpage – AxleWack Jul 13 '15 at 11:58

1 Answers1

0

Found a solution.

Scenario : I have a master page with an export button which exports data from a gridview on a page using the masterpage.

In the Page_Load event of the page with the Gridview, i used the following code :

Dim myMasterPage As MasterPageName = Page.Master

Dim exportButton As System.Web.UI.WebControls.Button = myMasterPage.FindControl("ButExportExcel")

If (exportButton IsNot Nothing) Then 
    AddHandler exportButton.Click, AddressOf Me.ButExportExcel_Click
End If

I then created the public sub:


Private Sub ButExportExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim sMethod = "ButExportExcel_Click"
    Dim sErrorMessage = ""
    Dim sExportFileName As String = ""

    Try

        sExportFileName = Path.GetFileName(Request.PhysicalPath)
        sExportFileName = sExportFileName.Substring(0, sExportFileName.Length - 5) & ".xls"

        Response.Clear()
        Response.AddHeader("content-disposition", "attachment; filename=" & sExportFileName)
        Response.ContentType = "application/vnd.xls"
        Dim WriteItem As System.IO.StringWriter = New System.IO.StringWriter()
        Dim htmlText As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(WriteItem)
        SummaryGridView.AllowPaging = False
        'Dim dtSupplier As DataTable = CType(ViewState("dtSupplier"), DataTable)
        'SummaryGridView.DataSource = dtSupplier
        SummaryGridView.DataBind()
        SummaryGridView.RenderControl(htmlText)
        Response.Write(WriteItem.ToString())
        Response.End()

    Catch ex As Exception



    End Try
End Sub

Then adding the below :

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
End Sub

This unfortunately means it needs to go onto every page that you want a gridview to be exported from, but it did the job for me. This only exports the data in the gridview.

I hope this helps anyone who is sitting with the same problem.

AxleWack
  • 1,801
  • 1
  • 19
  • 51