RadGrid for ASP.NET

Export to Microsoft Excel/Word/PDF/CSV Send comments on this topic.
RadGrid export > Export to Microsoft Excel/Word/PDF/CSV

Glossary Item Box

Only MS Word and MS Excel 2003 or later are supported.
You might receive the following dialog box message when you try to save files with 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 the file. Do you want to open the file now?"
Unfortunately this message cannot be avoided, because RadGrid has no way of knowing that the Office used is 2007. Since Microsoft have implemented some format optimizations which they claim lead to smaller file sizes among other things, Office 2007 tries to be smart and always warn that the document is not being exported in their new format. You can simply ignore it and click the Yes button to open the file. You can then save the file with a .xlsx/.docx extension instead of a .xls/.doc extension and next time you open the file locally, you will not receive this warning.

Telerik RadGrid can export the data to Microsoft Word, Microsoft Excel format, PDF and CSV. This can be done in one of the following ways:

 

Server-side MasterTableView object methods

ExportToWord()

ExportToExcel()

ExportToCsv()

ExportToPdf()

Event bubbling using the following commands

RadGrid.ExportToExcelCommandName

RadGrid.ExportToWordCommandName

RadGrid.ExportToCsvCommandName

RadGrid.ExportToPdfCommandName

Client-side RadGridTable object methods

ExportToWord()

ExportToExcel()

ExportToPdf()

ExportToCsv()



Additionally, you can export the grid data to CSV format (Comma-separated values conventions are discussed in Wikipedia) using the ExportToCSV() RadGrid server-side method.

 

RadGrid now contains a separate <CSV> tag in the exportsettings section. It gives the ability to change the Column and Row delimiters in the exported files. The two properties which give access to these settings are ColumnDelimeter, and RowDelimeter. For both of the settings, the available values are:

   Colon
   Comma
   NewLine
   Semicolon
   Tab
   VerticalBar

The code snippet below demonstrates one possible use of these properties:

ASPX Copy Code
<telerik:RadGrid runat="server" ID="RadGrid1" DataSourceID="AccessDataSource1" GridLines="None">
<
ExportSettings>
<
Pdf FontType="Subset" PaperSize="Letter" />
<
Excel Format="Html" />
<
Csv ColumnDelimiter="Colon" RowDelimiter="NewLine" />
</
ExportSettings>



Finally, you have the option to export the grid content to PDF format using the ExportToPdf() RadGrid server-side method.

You will also need to configure the exporting settings for the grid through the RadGrid.ExportSettings section. The available properties are:

  • FileName - a string specifying the name (without the extension) of the file that will be created. The file extension is automatically added based on the method that is used.
  • ExportOnlyData - determines whether only data will be exported.
  • IgnorePaging - specifies whether all records will be exported or merely those on the current page
  • OpenInNewWindow - open the exported grid in a new instead of the same page

 There is additional sub-category (Pdf) for the ExportSettings dedicated on various configuration settings for the exported .pdf document. Below is a list of them:


AllowAdd

Boolean property which determines whether content adding is allowed

AllowCopy

Boolean property which determines whether content copying is allowed

AllowModify

Boolean property which determines whether content modifications are allowed

AllowPrint

Boolean property which determines whether the pdf document can be printed

PageTitle

Sets the title of the page

PageLeftMargin

Sets the left margin for the pages in the pdf document

PageRightMargin

Sets the right margin for the pages in the pdf document

PageTopMargin

Sets the top margin for the pages in the pdf document

PageBottomMargin

Sets the bottom margin for the pages in the pdf document

PageHeaderMargin

Sets the header margin for the pages in the pdf document

PageFooterMargin

Sets the footer margin for the pages in the pdf document

PageHeight

Sets the height of the pages in the pdf document

PageWidth

Sets the width of the pages in the pdf document

Author

Sets the name of the author of the pdf document

Keywords

Sets the keywords for the pdf document

Subject

Sets the subject of the pdf document

Title

Sets the title of the pdf document

PaperSize

Enumeration which specifies the paper font size. The enumeration values are:
 - Letter (8.5 x 11 inches) - default value
 - Legal (8.5 x 14 inches)
 - Executive (7.25 x 10.5 inches)
 - A4 (210 x 297 mm)
 - A5 (148 x 210 mm)
 - JIS B5 (182 x 257 mm)
 - US Folio (8.5 x 13 inches)
 - No 10 Envelope (4.12 x 9.5 inches)
 - DL Envelope (110 x 220 mm)
 - C5 Envelope (162 x 229 mm)
 - C6 Envelope (114 x 162 mm)
 - ISO B5 (176 x 250 mm)
 - Monarch Envelope (3.87 x 7.5 inches)
 - A6 (105 x 148 mm)
 - Oficio (8.5 x 13.5 inches)

Producer

Specifies the producer of the resulting pdf document

Creator

Specifies the creator of the resulting pdf document


The default PageWidth/Height for the pdf document are Letter page format dimensions.


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:

C# Copy Code
protected void Button1_Click(object sender, EventArgs e)
{
           RadGrid1.PageSize = RadGrid1.MasterTableView.VirtualItemCount;
           RadGrid1.ExportSettings.IgnorePaging = true;
           RadGrid1.ExportSettings.OpenInNewWindow = true;
           RadGrid1.MasterTableView.ExportToExcel();  
}

 

VB.NET Copy Code
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

Note that RadGrid will render as an XHTML table and will convert that table to a PDF document. That requires that the rendered grid output is valid XHTML. If it is not, you will receive an exception that the export cannot be completed.

The most common cause for bad XHTML are symbols like <,>,& that need to be replaced by the correct XHTML entity: <, >, & respectively. Another frequent problem are unclosed tags.

 

The following methods are marked as obsolete as of RadGrid v4.6

  • server-side
    ExportToWord (fileName, dataOnly, ignorePaging)
    ExportToExcel (fileName, dataOnly, ignorePaging)
  • client-side
    ExportToWord (fileName)
    ExportToExcel (fileName)
  • server-side
    ExportToWord2007 (fileName, dataOnly, ignorePaging)
    ExportToExcel2007 (fileName, dataOnly, ignorePaging)

fileName - a string specifying the name (without the extension) of the file that will be created. 

dataOnly - boolean flag which determines whether only data will be exported

ignorePaging - boolean flag which specified whether all records will be exported or merely those on the current page

 For real-life example review the online demo of the product regarding export to Excel/Word.