RadControls for ASP.NET AJAX
Supported formats
Telerik RadGrid can export your data to the following formats:
Format | Sub-format | Inner type | Command name | Predefined constants | Server-side method | Client-side method |
PDF | n/a | binary | ExportToPdf | RadGrid.ExportToPdfCommandName | ExportToPdf() | ExportToPdf() |
Excel | HTML | HTML | ExportToExcel | RadGrid.ExportToExcelCommandName | ExportToExcel() | ExportToExcel() |
Excel | ExcelML | XML | ExportToExcel | RadGrid.ExportToExcelCommandName | ExportToExcel() | ExportToExcel() |
Word | n/a | HTML | ExportToWord | RadGrid.ExportToWordCommandName | ExportToWord() | ExportToWord() |
CSV | n/a | text | ExportToCsv | RadGrid.ExportToCsvCommandName | ExportToCSV() | ExportToCsv() |
Common properties and events
In addition to the export format's specific properties, the ExportSettings group exposes several
common properties:
As the name says, this property is helpful when you want to export only the data - e.g. to exclude the controls
from the exported file.
When you enable IgnorePaging, RadGrid will rebind before export
in order to fetch all the data from your datasource.
By default, the exported file will be handled by the program associated with the appropriate file type.
If you prefer to give the user the option to choose whether to save, open (inline) or cancel you should enable this property.
Note |
|---|
Even if you set OpenInNewWindow="false", that doesn't guarantee that the file will be opened inside
the browser window. The way the exported file will be displayed inline depends on the
OS/browser settings.
The end-user could manage the file extensions with programs like NirSoft's FileTypesMan. For browsers, other than Internet Explorer, you should use the built-in settings.
|
This is helpful when you want to give a predefined name for your file. Please note that the file name can't be longer
than 256 symbols.
Unicode names are not supported out-of-the-box for Internet Explorer6 and 7. Of course you can manually encode the file name and it will be shown
properly in the "Save" dialog (OpenInNewWindow="true").
HttpUtility.UrlEncode("unicode string", System.Text.Encoding.UTF8);
Note |
|---|
Internet Explorer ignores the FileName property when
OpenInNewWindow is set to false.
|
Removes the structure columns - GridRowIndicatorColumn, GridExpandColumn
as well as the first GridGroupSplitterColumn. Note that this property will affect only the first
level in hierarchical RadGrids.
CopyASPX
<ExportSettings
HideStructureColumns="true"
ExportOnlyData="true"
IgnorePaging="true"
OpenInNewWindow="true">
</ExportSettings>
Note |
|---|
You can receive this error if the file name is longer than 256 symbols: '<filename>.<extension>' could not be found. Check the spelling of the file name, and verify that the
file location is correct. |
This property will help avoid exporting already formatted values which
would cause Microsoft Excel to treat them as string values.Enabling this functionality will result in rebinding the control
before exporting. Note that even if both IgnorePaging and
SuppressColumnDataFormatStrings are enabled, RadGrid will rebind only once.
As the name says, this property gives you the ability to apply the item styles to the exported file.
OnGridExporting event
This event is useable in many scenarios when you want to modify the output file - for example you may want to add some custom
information above RadGrid or when you want to remove/add/replace parts of the content.
The only limitation applies to the PDF export because by the time the
OnGridExporting event is raised, the PDF is already generated.
For more information, please visit the dedicated PDF format topic which introduces the
OnPdfExporting event that is designed specifically for this format.
Below is the barebone logic for OnGridExporting event:
CopyC#
protected void RadGrid1_GridExporting(object source, GridExportingArgs e)
{
switch (e.ExportType)
{
case ExportType.Excel:
break;
case ExportType.ExcelML:
break;
case ExportType.Word:
break;
case ExportType.Csv:
break;
case ExportType.Pdf:
break;
}
}
CopyVB.NET
Protected Sub RadGrid1_GridExporting(ByVal source As Object, ByVal e As GridExportingArgs)
Select Case e.ExportType
Case ExportType.Excel
Exit Select
Case ExportType.ExcelML
Exit Select
Case ExportType.Word
Exit Select
Case ExportType.Csv
Exit Select
Case ExportType.Pdf
End Select
End Sub
Common scenarios
Caution |
|---|
Exporting with client-side binding is not supported, RadGrid should be bound
server-side, otherwise you will receive an empty file.
|
Exporting a large amount of data
We strongly recommend not to export large amounts of data since there is a chance to encounter an exception
(Timeout or OutOfMemory) if more than one user tries to export the same
data simultaneously. RadGrid is not suitable for such scenarios and therefore we suggest that you limit
the number of columns and rows. Also it is important to note that the hierarchy and the nested controls have a considerable effect
on the performance in this scenario.
Export with custom paging
When you have custom paging enabled for your grid, you need to set the PageSize property of the grid
to be equal to the VirtualItemCount in order to export all records successfully with
IgnorePaging set to true. Here is an example:
CopyC#
protected void Button1_Click(object sender, EventArgs e)
{
RadGrid1.PageSize = RadGrid1.MasterTableView.VirtualItemCount;
RadGrid1.ExportSettings.IgnorePaging = true;
RadGrid1.ExportSettings.OpenInNewWindow = true;
RadGrid1.MasterTableView.ExportToExcel();
}
CopyVB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
RadGrid1.PageSize = RadGrid1.MasterTableView.VirtualItemCount
RadGrid1.ExportSettings.IgnorePaging = True
RadGrid1.ExportSettings.OpenInNewWindow = True
RadGrid1.MasterTableView.ExportToExcel()
End Sub
Export from AJAX-enabled RadGrid
Nested grids / Exporting multiple RadGrids
Export with client-side binding
RadGrid doesn't support exporting with client-side binding.
To be able to export in such scenario, you should bind RadGrid on the server.
Export in SharePoint webpart
Export in web farm
If you receive "Invalid ViewState" or similar exception in web farm scenario, you can try to copy the
machinekey from your web.config to the
machine.config of the web server.
Export over SSL
You might receive the following error message when using the export feature over SSL and
Internet Explorer:
"Internet Explorer cannot download 'file' from 'server'.
Internet Explorer was not able to open this Internet site. The requested site is either
unavailable or cannot be found. Please try again later."
|
In order to prevent this error add the following lines just before the exporting:
CopyC#
RadGrid1.Page.Response.ClearHeaders();
RadGrid1.Page.Response.Cache.SetCacheability(HttpCacheability.Private);
CopyVB.NET
RadGrid1.Page.Response.ClearHeaders()
RadGrid1.Page.Response.Cache.SetCacheability(HttpCacheability.[Private])
Obsolete methods
The following methods are marked as obsolete as of RadGrid v4.6 (part of RadControls
for ASP.NET)
server-side:
ExportToWord(fileName, dataOnly, ignorePaging)
ExportToExcel(fileName, dataOnly, ignorePaging)
ExportToWord2007(fileName, dataOnly, ignorePaging)
ExportToExcel2007(fileName, dataOnly, ignorePaging)
client-side:
ExportToWord(fileName)
ExportToExcel(fileName)
For real-life example, review our online demo.