RadGrid for ASP.NET AJAX

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

Caution

It is expected to receive the following warning when opening the generated xls file in Microsoft Office 2007.

The file you are trying to open, 'Filename.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening this file. Do you want to open the file now?

The cause of this warning message is explained in details in the following blog posts:

MSDN blogs: Excel 2007 Extension Warning On Opening Excel Workbook from a Web Site

Grinn blog: "The file you are trying to open, '[filename]', is in a different format"

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 using inline styles. You can apply CSS tags/classes to the cells/rows/tables, etc.

Caution

Microsoft Word 2003 displays grid lines by default. These lines just shows where the table/cell/row borders are. If you want to remove them, you should choose Table>>Show Gridlines (uncheck this option).

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:

  • 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 ExcelExportCellFormatting 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 useable in different scenarios, for example:

  • to apply styles to items other than GridDataItem
  • to format Word document, since the ExcelExportCellFormatting event is Excel-specific

They are not as convenient as the ExcelExportCellFormatting 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.

Number formats / Formulas

Microsoft Office HTML format allows you to define custom number formats by using the mso-number-format style attribute. Several examples are shown below:

You can see some of the most common custom number formats and their description in the following table:

FormatDescription
\@text
"0\.000"3 decimals
\#\,\#\#0\.000comma separators (and 3 decimals)
"mm\/dd\/yy"Date format
"d\\-mmm\\-yyyy"another date format
Percentpercent

Another interesting feature, supported by the Excel export are the formulas. You can assign the desired formula to a specific cell by applying the formula attribute.

Note

The formula attribute is valid in the context of the Office HTML only. You shouldn't apply this attribute unconditionally in order to prevent validation problems.

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:

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