Link to home
Start Free TrialLog in
Avatar of westdh
westdhFlag for United States of America

asked on

asp.net How to change the header column names in the excell file when exporting Gridview to excel

How to change the header column names in the excell file when exporting Gridview to excel. I would like to change the header names in Excel
to...
 
ID
Company Name
Attendee Name

instead of the following..

intRegistrationID strName strRegistrationUserName
178 Petersen Construction Services Perry Petersen
179 CALTROP Corporation Beth Power

Private Sub GetSelections_Click(ByVal sender As Object, ByVal e As System.EventArgs)  Handles GetSelections.Click
  
        Dim rowCount As Integer = 0 
        Dim gridSelections As New StringBuilder() 
	Dim dt As New DataTable()
	dt.Columns.Add("intRegistrationID")
	dt.Columns.Add("strName")
        dt.Columns.Add("strRegistrationUserName")
     
    'Loop through GridView, and determine which CheckBox controls have been selected. 
      For Each row As GridViewRow In uxItemDetailGrid.Rows
        ' Access the CheckBox
         Dim cb As CheckBox = row.FindControl("myCheckbox")
         If cb.Checked = True Then
           '  rowCount += 1 
           '  response.write("test1: " + rowCount.ToString())

            Dim i As Integer = row.RowIndex
      	    Dim lbl As Label = CType(uxItemDetailGrid.Rows(i).FindControl("lblintRegistrationID"), Label)
      	    Dim lbl1 As Label = CType(uxItemDetailGrid.Rows(i).FindControl("lblstrName"), Label)
            Dim lbl2 As Label = CType(uxItemDetailGrid.Rows(i).FindControl("lblUserName"), Label)

      	    Dim dr As DataRow = dt.NewRow()
      	    dr("intRegistrationID") = Convert.ToString(lbl.Text)
      	    dr("strName") = Convert.ToString(lbl1.Text)
            dr("strRegistrationUserName") = Convert.ToString(lbl2.Text)

      	    dt.Rows.Add(dr)

        End If
      Next

	Dim uxItemDetailGrid1 As New GridView()
	uxItemDetailGrid1.DataSource = dt
	uxItemDetailGrid1.DataBind()
	Response.Clear()
	Response.Buffer = True
	Response.ContentType = "application/ms-excel"
	Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", "selectedrows"))
	Response.Charset = ""
	Dim stringwriter = New StringWriter()
	Dim htmlwriter As New HtmlTextWriter(stringwriter)
	uxItemDetailGrid1.RenderControl(htmlwriter)
	Response.Write(stringwriter.ToString())
	Response.End()

    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pradyahuja
pradyahuja
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of westdh

ASKER

Thanks Munch