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.
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:
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:
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:
Sometimes the developer needs to highlight the negative values (for example: -1, -5, -10.5) - this could be achieved in the same event handler:
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.
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.
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.
String value specifying the author's name.
Comma-separated string values listing the document’s keywords.
Represents the document description.
The account name of the last person who made changes to the file.
The category of the document.
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.
You can use the HideStructureColumns property to hide GridRowIndicatorColumn, GridExpandColumn and GridGroupSplitterColumn. For the other columns types, you can use the following approach:
From Q2 2013 we introduced a new propery named Exportable. This property allows you to
choose whether a certain column should be included in the exported file or not. By setting this property to
false the related column will be excluded from the exported file. Its default value is true.
There are two common ways to hide an item.
There are various ways to set the width of a given column.
<telerik:GridBoundColumn ... HeaderStyle-Width="20px" />
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.
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