Hi Rodney,
Assuming that you are using the latest version of the Telerik controls, RadGrid can export the MultiColumn headers out of the box and no additional coding is required.
You can try the following code snippets to see it in action:
ASP Markup code containing 3 buttons for the different export formats, and one RadGrid that has Multi Column headers enabled.
<
h3
>Export</
h3
>
<
telerik:RadButton
ID
=
"RadButton1"
runat
=
"server"
Skin
=
"Default"
Text
=
"XLSX"
OnClick
=
"RadButton1_Click"
></
telerik:RadButton
>
<
telerik:RadButton
ID
=
"RadButton2"
runat
=
"server"
Skin
=
"Default"
Text
=
"Html"
OnClick
=
"RadButton2_Click"
></
telerik:RadButton
>
<
telerik:RadButton
ID
=
"RadButton3"
runat
=
"server"
Skin
=
"Default"
Text
=
"Biff"
OnClick
=
"RadButton3_Click"
></
telerik:RadButton
>
<
br
/>
<
br
/>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
Width
=
"800px"
Skin
=
"Default"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
>
<
MasterTableView
AutoGenerateColumns
=
"False"
DataKeyNames
=
"OrderID"
>
<
CommandItemSettings
ShowExportToExcelButton
=
"true"
ShowAddNewRecordButton
=
"false"
ShowRefreshButton
=
"false"
/>
<
ColumnGroups
>
<
telerik:GridColumnGroup
HeaderText
=
"Group 1"
Name
=
"Group1"
></
telerik:GridColumnGroup
>
<
telerik:GridColumnGroup
HeaderText
=
"Group 2"
Name
=
"Group2"
></
telerik:GridColumnGroup
>
<
telerik:GridColumnGroup
HeaderText
=
"Group 3"
Name
=
"Group3"
ParentGroupName
=
"Group2"
></
telerik:GridColumnGroup
>
</
ColumnGroups
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"OrderID"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter OrderID column"
HeaderText
=
"OrderID"
ReadOnly
=
"True"
SortExpression
=
"OrderID"
UniqueName
=
"OrderID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridDateTimeColumn
DataField
=
"OrderDate"
DataType
=
"System.DateTime"
ColumnGroupName
=
"Group1"
FilterControlAltText
=
"Filter OrderDate column"
HeaderText
=
"OrderDate"
SortExpression
=
"OrderDate"
UniqueName
=
"OrderDate"
>
</
telerik:GridDateTimeColumn
>
<
telerik:GridNumericColumn
DataField
=
"Freight"
DataType
=
"System.Decimal"
ColumnGroupName
=
"Group2"
FilterControlAltText
=
"Filter Freight column"
HeaderText
=
"Freight"
SortExpression
=
"Freight"
UniqueName
=
"Freight"
>
</
telerik:GridNumericColumn
>
<
telerik:GridBoundColumn
DataField
=
"ShipName"
FilterControlAltText
=
"Filter ShipName column"
HeaderText
=
"ShipName"
ColumnGroupName
=
"Group2"
SortExpression
=
"ShipName"
UniqueName
=
"ShipName"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"ShipCountry"
FilterControlAltText
=
"Filter ShipCountry column"
HeaderText
=
"ShipCountry"
ColumnGroupName
=
"Group3"
SortExpression
=
"ShipCountry"
UniqueName
=
"ShipCountry"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C# - Code behind containing the methods required by the Markup.
// XLSX
protected
void
RadButton1_Click(
object
sender, EventArgs e)
{
RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.Xlsx;
RadGrid1.ExportToExcel();
}
// HTML
protected
void
RadButton2_Click(
object
sender, EventArgs e)
{
RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.Html;
// HTML is the default value
RadGrid1.ExportToExcel();
}
// BIFF
protected
void
RadButton3_Click(
object
sender, EventArgs e)
{
RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.Biff;
RadGrid1.ExportToExcel();
}
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = OrdersTable();
}
private
DataTable OrdersTable()
{
DataTable dt =
new
DataTable();
dt.Columns.Add(
new
DataColumn(
"OrderID"
,
typeof
(
int
)));
dt.Columns.Add(
new
DataColumn(
"OrderDate"
,
typeof
(DateTime)));
dt.Columns.Add(
new
DataColumn(
"Freight"
,
typeof
(
decimal
)));
dt.Columns.Add(
new
DataColumn(
"ShipName"
,
typeof
(
string
)));
dt.Columns.Add(
new
DataColumn(
"ShipCountry"
,
typeof
(
string
)));
dt.PrimaryKey =
new
DataColumn[] { dt.Columns[
"OrderID"
] };
for
(
int
i = 0; i < 4; i++)
{
int
index = i + 1;
DataRow row = dt.NewRow();
row[
"OrderID"
] = index;
row[
"OrderDate"
] =
new
DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
row[
"Freight"
] = index * 0.1 + index * 0.01;
row[
"ShipName"
] =
"Name "
+ index;
row[
"ShipCountry"
] =
"Country "
+ index;
dt.Rows.Add(row);
}
return
dt;
}
Kind regards,
Attila Antal
Progress Telerik
Get
quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers.
Learn More.