Export to HTML
RELATED VIDEOS | |
---|---|
Exporting to HTML with RadGridView for WinForms In this video, you will learn how to export RadGridView to the HTML file format. |
Overview
This method offers excellent export performance and creates an HTML formatted file, which can be opened in a browser or MS Word.
The HTML export functionality is located in the TelerikData.dll assembly. You need to include the following namespaces in order to access the types contained in TelerikData:
- Telerik.WinControls.Data
- Telerik.WinControls.UI.Export
Exporting Data
Initialize ExportToHTML object
Before running export to HTML, you have to initialize the ExportToHTML class. The constructor takes one parameter: the RadGridView that will be exported:
ExportToHTML initialization
ExportToHTML exporter = new ExportToHTML(this.radGridView1);
Dim exporter As ExportToHTML = New ExportToHTML(Me.RadGridView1)
File extension
This property allows for changing the default (*.htm) file extension of the exported result file
Setting the file extension
exporter.FileExtension = "";
exporter.FileExtension = ""
Hidden columns and rows option
You can choose one of the three options below which will allow you to have a different behavior for the hidden column/rows. You can choose these options by HiddenColumnOption and HiddenRowOption properties:
ExportAlways
DoNotExport (default)
ExportAsHidden
Please note that some browsers do not support hidden columns and if you open exported file you could see the hidden columns. To make sure that the hidden columns or rows in the exported HTML file will not be included, you should set HiddenColumnOption or HiddenRowOption property to DoNotExport:
Setting the HiddenColumnOption
exporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport;
exporter.HiddenColumnOption = Telerik.WinControls.UI.Export.HiddenOption.DoNotExport
Columns Width options
There are three options for the column widths. First option is to have columns with fixed width, dependent on the size of the columns in the actual RadGridView that is being exported. This is the default way the exporter works. Second option is to set a fixed width for all the columns. The columns will then take this width while keeping their aspect ratio. To set a fixed size for all columns use the FitWidthSize option of the exporter.
Setting FitWidthSize
exporter.FitWidthSize = 1000;
exporter.FitWidthSize = 1000
The third option is to set the column widths to be auto calculated depending on the content of the cells in the column. For this option you can use the AutoSizeColumns property.
Setting AutoSizeColumns
exporter.AutoSizeColumns = true;
exporter.AutoSizeColumns = True
If you use this option you need to control how the cells are sized. To do that you can use the CellWhiteSpace option of the HTMLCellElement
which controls how white spaces are handled. The possible values are.
Normal (Defalut)
NoWrap
Pre
PreLine
PreWrap
Exporting Visual Settings
Using the ExportToHTML class allows you to export the visual settings (themes) to the HTML file. ExportToHTML also provides a visual representation of the alternating row color. This feature works only if EnableAlternatingRow property is set to true. Note that it does not transfer the alternating row settings that come from control theme. RadGridView will also export the conditional formatting to the HTML file. The row height is exported with the default DPI transformation (60pixels = 72points). You can enable exporting visual settings through the ExportVisualSettings property. By default the value of this property is false.
Setting ExportVisualSettings
exporter.ExportVisualSettings = true;
exporter.ExportVisualSettings = True
HTML Table Caption
You can specify the table caption through TableCaption property.
Setting the SheetName
exporter.TableCaption = "Table";
exporter.TableCaption = "Table"
Summaries export option
You can use SummariesExportOption property to specify how to export summary items. There are four options to choose from:
ExportAll (default)
ExportOnlyTop
ExportOnlyBottom
DoNotExport
Setting the SummariesExportOption
exporter.SummariesExportOption = SummariesOption.DoNotExport;
exporter.SummariesExportOption = SummariesOption.DoNotExport
RunExport method
Exporting data to HTML is done through the RunExport method. This method accepts the following parameter:
- FileName - the name of the exported file.
Consider the code sample below:
Export to HTML format
string fileName = "c:\\ExportedData.htm";
exporter.RunExport(fileName);
Dim fileName As String = "c:\\ExportedData.htm"
exporter.RunExport(fileName)
Events
1. HTMLCellFormating: It gives access to a single cell’s HTML element that allows you to make additional formatting for every HTML cell related to the exported RadGridView:
Handling the HTMLCellFormatting event
void exporter_HTMLCellFormatting(object sender, Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs e)
{
if (e.GridColumnIndex == 1 && e.GridRowInfoType == typeof(GridViewDataRowInfo))
{
e.HTMLCellElement.Value = "Test value";
e.HTMLCellElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.Orange));
}
}
Private Sub exporter_HTMLCellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Export.HTML.HTMLCellFormattingEventArgs)
If e.GridColumnIndex = 1 AndAlso e.GridRowInfoType.Equals(GetType(GridViewDataRowInfo)) Then
e.HTMLCellElement.Value = "Test value"
e.HTMLCellElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.Orange))
End If
End Sub
2. HTMLTableCaptionFormatting: this event can be used to make an additional formatting on the HTML table caption element. You can access TableCaptionElement through event’s arguments and apply a valid HTML format.
Handling the HTMLTableCaptionFormatting event
void exporter_HTMLTableCaptionFormatting(object sender, Telerik.WinControls.UI.Export.HTML.HTMLTableCaptionFormattingEventArgs e)
{
e.TableCaptionElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.Red));
e.TableCaptionElement.Styles.Add("font-size", "200%");
e.TableCaptionElement.Styles.Add("color", ColorTranslator.ToHtml(Color.White));
e.TableCaptionElement.Styles.Add("font-weight", "bold");
e.CaptionText = "My Table Caption";
}
Private Sub exporter_HTMLTableCaptionFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.Export.HTML.HTMLTableCaptionFormattingEventArgs)
e.TableCaptionElement.Styles.Add("background-color", ColorTranslator.ToHtml(Color.Red))
e.TableCaptionElement.Styles.Add("font-size", "200%")
e.TableCaptionElement.Styles.Add("color", ColorTranslator.ToHtml(Color.White))
e.TableCaptionElement.Styles.Add("font-weight", "bold")
e.CaptionText = "My Table Caption"
End Sub