We help IT Professionals succeed at work.
Get Started

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

560 Views
Last Modified: 2013-11-08
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

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE