This is a migrated thread and some comments may be shown as answers.

Export to excel using the Fielname and not the Heading Text

3 Answers 117 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Johan
Top achievements
Rank 1
Johan asked on 25 Jan 2011, 10:35 AM
Hi,

Is it possible when exporting to excel to use the column's field name instead of the heading text?

In other words, we would like to use a more user friendly heading text for the columns in the grid but when exporting it should use the field name instead. The default behavior is to export using the column's heading text.

Thanks

3 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 25 Jan 2011, 11:48 AM
Hello,

As far as I'm aware, there is no built in way of doing this, but if you want to export the FieldName rather than the Head Text for the column you could use the following method

1: Define a structure for holding column info
Public Structure ColumnInfo
    Public Sub New(ByVal header As String, ByVal field As String)
        Me.Field = field
        Me.Header = header
    End Sub
    Public Property Header As String
    Public Property Field As String
End Structure

2: Set up the export settings
Dim exporter As ExportToExcelML = New ExportToExcelML(Me.RadGridView1)
exporter.ExportVisualSettings = True
exporter.HiddenRowOption = HiddenOption.DoNotExport
exporter.SheetName = "My Report"

3: Rename the column headers to the field name
Dim list As New List(Of ColumnInfo)
For Each col As GridViewDataColumn In Me.RadGridView1.Columns
    list.Add(New ColumnInfo(col.HeaderText, col.FieldName))
Next
For Each col As GridViewDataColumn In Me.RadGridView1.Columns
    If String.Equals(col.FieldName, col.HeaderText) = False Then
        col.HeaderText = col.FieldName
    End If
Next

4: Run the export and put the Header Text back again
' run export before renaming header again
exporter.RunExport("C:\MyDoc..xls")
For Each col As GridViewDataColumn In Me.RadGridView1.Columns
    For Each ci As ColumnInfo In list
        If String.Equals(ci.Field, col.FieldName) Then
            col.HeaderText = ci.Header
            Exit For
        End If
    Next
Next

Hope that helps
Richard
0
Johan
Top achievements
Rank 1
answered on 25 Jan 2011, 12:12 PM
Thanks Richard, I think I'm going to go with your suggestion!
0
Richard Slade
Top achievements
Rank 2
answered on 25 Jan 2011, 12:14 PM
You're welcome. Glad it helped
Richard
Tags
GridView
Asked by
Johan
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Johan
Top achievements
Rank 1
Share this question
or