Telerik UI 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 UI for ASP.NET AJAX Q1 2011.

There are two important members exposed by the ExportCellFormattingEventArgs:

  • Cell - this is a reference to the current TableCell. You can use it to apply specific CSS style or to gain access to the GridDataItem object:

  • FormattedColumn - this property returns an object of type GridColumn. It helps to distinguish to which column the current cell belongs to.

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:

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:

Sometimes the developer needs to highlight the negative values (for example: -1, -5, -10.5) - this could be achieved in the same event handler:

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.

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:

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:

Note

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.

Hiding items

There are two common ways to hide an item.

  • Directly - in the button handler when exporting from a button or on ItemCommand when exporting from the built-in buttons

  • On ItemCreated / ItemDataBound - this approach should be used when IgnorePaging="true" or when you call RadGrid.Rebind before exporting.

Resizing/Aligning Columns

There are various ways to set the width of a given column.

  • declarative approach:

CopyASPX
<telerik:GridBoundColumn ... HeaderStyle-Width="20px" />
  • in code-behind - before export:

  • in code-behind - on TH elements

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