RadControls for ASP.NET AJAX RadGrid is able to export your data to Word and Excel using HTML/CSS-based format. It is not the native binary format used by Microsoft Office but it is a popular way to export data and it is also officially supported by Microsoft:
Microsoft® Office HTML and XML Reference
Styles / Appearance
Due to the fact that this format is based on standard HTML/CSS it is quite straightforward to control
the appearance of the output by applying CSS tags/classesto the cells/rows/tables, etc.
ExportCellFormating / ExcelExportCellFormatting events
In order to aid the developers, we exposed the ExportCellFormatting event. It fires for each cell in each data item in RadGrid.
Note |
|---|
Please note that the ExcelExportCellFormatting event (Excel-specific) is marked as obsolete as from RadControls for ASP.NET AJAX Q1 2011.
|
There are two important members exposed by the ExportCellFormattingEventArgs:
CopyC#
GridDataItem item = e.Cell.Parent as GridDataItem;
CopyVB.NET
Dim item As GridDataItem = TryCast(e.Cell.Parent, GridDataItem)
CopyC#
GridColumn column = e.FormattedColumn as GridColumn;
string columnName = column.UniqueName;
CopyVB.NET
Dim column As GridColumn = TryCast(e.FormattedColumn, GridColumn)
Dim columnName As String = column.UniqueName
HTMLExporting event
The purpose of this event is to allow the developer to insert global styles (CSS) or configuration options (XML) to the exported file.
A possible application for this event is to enable the grid lines for the current worksheet:
CopyC#
protected void RadGrid1_HTMLExporting(object sender, GridHTMLExportingEventArgs e)
{
e.Styles.Append("body { border:solid 0.1pt #CCCCCC; }");
}
CopyVB.NET
Protected Sub RadGrid1_HTMLExporting(ByVal sender As Object, ByVal e As GridHTMLExportingEventArgs) Handles RadGrid1.HTMLExporting
e.Styles.Append("body { border:solid 0.1pt #CCCCCC; }")
End Sub
Styling rows/cells
Thanks to the ExportCellFormatting event it is really easy to apply custom styles to the rows/cells. The following code-snippet demonstrates how to style the alternating items:
CopyC#
protected void RadGrid1_ExportCellFormatting(object source, ExportCellFormattingEventArgs e)
{
GridDataItem item = e.Cell.Parent as GridDataItem;
if (item.ItemType == GridItemType.AlternatingItem)
item.Style["background-color"] = "#359AFF";
else
item.Style["background-color"] = "#2D62FF";
}
CopyVB.NET
Protected Sub RadGrid1_ExportCellFormatting(ByVal source As Object, ByVal e As ExportCellFormattingEventArgs) Handles RadGrid1.ExportCellFormatting
Dim item As GridDataItem = TryCast(e.Cell.Parent, GridDataItem)
If item.ItemType = GridItemType.AlternatingItem Then
item.Style("background-color") = "#359AFF"
Else
item.Style("background-color") = "#2D62FF"
End If
End SubSometimes the developer needs to highlight the negative values (for example: -1, -5, -10.5) - this could be achieved in the same event handler:
CopyC#
protected void RadGrid1_ExportCellFormatting(object source, ExportCellFormattingEventArgs e)
{
if (e.FormattedColumn.UniqueName == "MyColumn" && double.Parse(e.Cell.Text) < 0)
e.Cell.Style["background-color"] = "#FA2020";
}
CopyVB.NET
Protected Sub RadGrid1_ExportCellFormatting(ByVal source As Object, ByVal e As ExportCellFormattingEventArgs) Handles RadGrid1.ExportCellFormatting
If e.FormattedColumn.UniqueName = "MyColumn" AndAlso Double.Parse(e.Cell.Text) < 0 Then
e.Cell.Style("background-color") = "#FA2020"
End If
End Sub
Using ItemCreated/ItemDataBound
These events are usable in different scenarios such as applying styles to items other than GridDataItem
They are not as convenient as the ExportCellFormatting event because the developer should use flag to distinguish whether the current item/cell is being exported or displayed.
Please keep in mind that if you don't use IgnorePaging="true"RadGrid will be exported directly and the ItemCreated/ItemDataBound events won't be fired.
CopyC#
bool isExport = false;
void Button1_Click(object sender, EventArgs e)
{
isExport = true;
RadGrid1.MasterTableView.ExportToExcel();
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridHeaderItem && isExport)
e.Item.Style["background-color"] = "#EEAAEC";
}
CopyVB.NET
Private isExport As Boolean = False
Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
isExport = True
RadGrid1.MasterTableView.ExportToExcel()
End Sub
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridHeaderItem AndAlso isExport Then
e.Item.Style("background-color") = "#EEAAEC"
End If
End Sub
When IgnorePaging="false" you should rebind RadGrid manually, otherwise this approach won't work. The above code needs only one change - put RadGrid1.Rebind() after the point where you set the flag.
XML Options
XmlOptions property allows you to set different options of the exported file.
The XML element contains one or more sub-elements. These include DocumentProperties which is valid for Word and Excel,
WordDocument which can be used when exporting to Word, and finally ExcelWorkbook which applies to Microsoft Excel.
Below you could find a list with the supported sub-elements of the DocumentProperties.
DocumentProperties | Represents |
|---|
<o:Subject> </o:Subject> | Left-aligned data |
<o:Author> </o:Author> | String value specifying the author's name. |
<o:Keywords> </o:Keywords> | Comma-separated string values listing the document’s keywords. |
<o:Description> </o:Description> | Represents the document description. |
<o:LastAuthor> </o:LastAuthor> | The account name of the last person who made changes to the file. |
<o:Category> </o:Category> | The category of the document. |
<o:Manager> </o:Manager> | Manager’s name. |
<o:Company> </o:Company> | String value holding the name of the company. |
A list of supported Word properties could be found here.
A list of supported Excel properties could be found here.
Example:
CopyC#
protected void RadGrid1_HTMLExporting(object sender, GridHTMLExportingEventArgs e)
{
e.XmlOptions = @"<xml>
<o:DocumentProperties>
<o:Category>Reports</o:Category>
<o:Manager>Kate Dresen</o:Manager>
<o:Company>Adventure Works</o:Company>
</o:DocumentProperties>
</xml>";
}
CopyVB.NET
Protected Sub RadGrid1_HTMLExporting(sender As Object, e As GridHTMLExportingEventArgs)
e.XmlOptions = "<xml><o:DocumentProperties><o:Category>Reports</o:Category><o:Manager>Kate Dresen</o:Manager><o:Company>Adventure Works</o:Company></o:DocumentProperties></xml>"
End Sub
Hiding columns
You can use the HideStructureColumns property to hide GridRowIndicatorColumn, GridExpandColumn and GridGroupSplitterColumn. For the other columns types, you can use the following approach:
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.GetColumn("C2").Visible = false;
RadGrid1.MasterTableView.ExportToWord();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
RadGrid1.MasterTableView.GetColumn("C2").Visible = False
RadGrid1.MasterTableView.ExportToWord()
End Sub
Hiding items
There are two common ways to hide an item.
CopyC#
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.ExportToWordCommandName)
RadGrid1.MasterTableView.Items[2].Visible = false;
}
CopyVB.NET
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand
If e.CommandName = RadGrid.ExportToWordCommandName Then
RadGrid1.MasterTableView.Items(2).Visible = False
End If
End Sub
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.Items[2].Visible = false;
RadGrid1.MasterTableView.ExportToWord();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
RadGrid1.MasterTableView.Items(2).Visible = False
RadGrid1.MasterTableView.ExportToWord()
End Sub
CopyC#
bool isWordExport = false;
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.ExportToWordCommandName)
isWordExport = true;
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (isWordExport && e.Item.ItemIndex == 2)
e.Item.Visible = false;
}
CopyVB.NET
Private isWordExport As Boolean = False
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
If e.CommandName = RadGrid.ExportToWordCommandName Then
isWordExport = True
End If
End Sub
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)
If isWordExport AndAlso e.Item.ItemIndex = 2 Then
e.Item.Visible = False
End If
End Sub
Resizing/Aligning Columns
There are various ways to set the width of a given column.
CopyASPX
<telerik:GridBoundColumn ... HeaderStyle-Width="20px" />
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.MasterTableView.GetColumn("C1").HeaderStyle.Width = Unit.Pixel(20);
RadGrid1.MasterTableView.ExportToExcel();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
RadGrid1.MasterTableView.GetColumn("C1").HeaderStyle.Width = Unit.Pixel(20)
RadGrid1.MasterTableView.ExportToExcel()
End Sub
CopyC#
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridHeaderItem && isExport)
{
foreach (TableCell cell in e.Item.Cells)
cell.Style["width"] = "20px";
}
}
CopyVB.NET
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridHeaderItem AndAlso isExport Then
For Each cell As TableCell In e.Item.Cells
cell.Style("width") = "20px"
Next
End If
End Sub
Alignment
You can specify the horizontal alignment, using the text-alignCSS attribute. Please note that it is not possible to apply this attribute to the whole header row - you should set it to each cell (TH) separately. The aforementioned limitation concerns only to the header items.
Unsupported scenarios
There are several limitations that you should have in mind:
embedded images are not supported
exporting invisible RadGrid - to avoid problems with missing content, you should temporary show RadGrid before export
OpenOffice, AbiWord, etc doesn't support this standard so they won't show the files properly
in theory all Microsoft Office versions from 2000 up might work, although we don't guarantee that any version, prior to 2003 will display the Office HTML formats as expected