 |
When setting the FileName in the ExportSettings to encoding that might not be installed on the client machine it is best to encode it before that:
RadGrid1.ExportSettings.FileName = HttpUtility.UrlEncode("Ie Chinese Characters here"); |
Removing command item from the exported document
Since the RadGrid command item will be included by default in the exported Excel/Word file, you may need to remove it and display just the data content in the table. This can be achieved easily by hiding the command item(s) prior to the export action. Below is a sample code snippet:
| VB.NET |
Copy Code |
|
Protected Sub ExportClick(ByVal sender As Object, ByVal e As EventArgs) Me.RadGrid1.ExportSettings.OpenInNewWindow = True Me.RadGrid1.ExportSettings.ExportOnlyData = True Me.RadGrid1.ExportSettings.IgnorePaging = True
Dim commandItem as GridItem
for each commandItem in Me.RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem) commandItem.Visible = False Next
Me.RadGrid1.MasterTableView.ExportToExcel() End Sub |
| C# |
Copy Code |
|
protected void ExportClick(object sender, EventArgs e) { this.RadGrid1.ExportSettings.OpenInNewWindow = true; this.RadGrid1.ExportSettings.ExportOnlyData = true; this.RadGrid1.ExportSettings.IgnorePaging = true;
foreach (GridItem commandItem in this.RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)) { commandItem.Visible = false; }
this.RadGrid1.MasterTableView.ExportToExcel(); } |
 |
If you export from buttons in the CommandItem, then you should use the approach from the #RadGrid formatting and PDF export# section below. Namely, setting global bool variable and using the ItemCreated event for the actual hiding of the CommandItems from the exported file. |
Exporting GridButtonColumn data
The GridButtonColumns in Telerik RadGrid have LinkButtons/PushButtons/ImageButtons in their cells. They are not considered as data,
that is why when you select the second parameter of the ExportToExcel method to be true, those buttons will be stripped from the resulting Excel file.
Here is a workaround solution which will help you present the respective GridButtonColumn in the xls document:
| ASPX/ASCX |
Copy Code |
|
<rad:RadGrid id="RadGrid1" runat="server" EnableAJAX="true"> <MasterTableView AutoGenerateColumns="False"> <Columns> <rad:GridButtonColumn CommandName="MyCommandName" HeaderText="Country" UniqueName= "CountryButton" DataTextField="Country"></rad:GridButtonColumn> <rad:GridBoundColumn Visible="false" UniqueName="CountryData" HeaderText= "Country" DataField="Country"></rad:GridBoundColumn> <rad:GridBoundColumn UniqueName="ContactName" HeaderText="Contact Name" DataField= "ContactName"></rad:GridBoundColumn> </Columns> </MasterTableView> </rad:RadGrid> <br/> <asp:Button id="Button1" runat="server" Text="Export to Excel" /> |
| C# |
Copy Code |
|
private void Button1_Click(object sender, System.EventArgs e) { RadGrid1.MasterTableView.Columns.FindByUniqueName( "CountryButton").Visible = false; RadGrid1.MasterTableView.Columns.FindByUniqueName( "CountryData").Visible = true;
RadGrid1.ExportSettings.ExportOnlyData = true; RadGrid1.ExportSettings.IgnorePaging = true; RadGrid1.ExportSettings.OpenInNewWindow = true;
RadGrid1.MasterTableView.ExportToExcel(); } |
| VB.NET |
Copy Code |
|
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click RadGrid1.MasterTableView.Columns.FindByUniqueName( "CountryButton").Visible = False RadGrid1.MasterTableView.Columns.FindByUniqueName( "CountryData").Visible = True
RadGrid1.ExportSettings.ExportOnlyData = True RadGrid1.ExportSettings.IgnorePaging = True RadGrid1.ExportSettings.OpenInNewWindow = True
RadGrid1.MasterTableView.ExportToExcel() End Sub |
You merely need to hide initially
GridBoundColumn with the same data (setting its
Visible property to
false), then switch its visibility with the one for the
GridButtonColumn, and finally export it to Excel calling Telerik RadGrid's
ExportToExcel method.
The same approach can be applicable for GridHyperLinkColumn, template columns with links/buttons/images in them, etc.
RadGrid formatting and PDF export
Currently RadGrid would not export any external styles in PDF. That means skins will not appear in PDF export. Still, the inline styles are preserved. You can set ItemStyle, AlternatingItemStyle, HeaderStyle, etc. and these settings will be preserved. The next code snippet shows how to further set styles that will be present in the resulting PDF file, IgnorePaging is set to true in order to force RadGrid to rebind and apply the styles.
| C# |
Copy Code |
bool isPdfExport = false; protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) { if (e.CommandName == RadGrid.ExportToPdfCommandName) { isPdfExport = true; RadGrid1.ExportSettings.IgnorePaging = true; } } protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) { if (isPdfExport && e.Item is GridHeaderItem) { GridHeaderItem headerItem = (GridHeaderItem)e.Item; headerItem.Style["font-size"] = "16pt"; headerItem.Style["color"] = "red"; headerItem.Style["background-color"] = "yellow"; headerItem.Style["height"] = "50px"; headerItem.Style["vertical-align"] = "bottom"; foreach (TableCell cell in headerItem.Cells) { cell.Style["text-align"] = "left"; cell.Style["font-weight"] = "bold"; cell.Style["border-color"] = "red"; } } }
|
| VB.NET |
Copy Code |
|
Public IsPdfExport As Boolean = False Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.WebControls.GridCommandEventArgs) Handles RadGrid1.ItemCommand If e.CommandName = RadGrid.ExportToPdfCommandName Then IsPdfExport = True RadGrid1.ExportSettings.IgnorePaging = True End If End Sub Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.WebControls.GridItemEventArgs) Handles RadGrid1.ItemCreated If IsPdfExport AndAlso TypeOf e.Item Is GridHeaderItem Then Dim headerItem As GridHeaderItem = e.Item headerItem.Style("font-size") = "16pt" headerItem.Style("color") = "red" headerItem.Style("background-color") = "yellow" headerItem.Style("height") = "50px" headerItem.Style("vertical-align") = "bottom" For Each cell As TableCell In headerItem.Cells cell.Style("text-align") = "left" cell.Style("font-weight") = "bold" cell.Style("border-color") = "red" Next End If End Sub |
Exporting non-latin characters to PDF.
When exporting non-latin characters to PDF, RadGrid requires that each item has the appropriate font set. The font should support the characters expected to appear in the resulting document. This can be achieved by adding ItemStyle.
| ASPX |
Copy Code |
|
<rad:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" > <ItemStyle Font-Names="Arial" /> </rad:RadGrid> |