Formatting Exports from Telerik RadGrid (PDF,Word, Excel)

4 posts, 1 answers
  1. Answer
    Ken Lassesen
    Ken Lassesen avatar
    37 posts
    Member since:
    Nov 2008

    Posted 23 Jun 2010 Link to this post


    RadControls version

    2010 (should work with earlier versions)

    .NET version

    .Net4 (should work with earlier)    

    Visual Studio version

    VS2010 -- as Web Site so earlier versions should be able to load    

    programming language


    browser support

    all browsers supported by RadControls

    I have been spending a bit of time trying to get a simple and elegant way to (re-)format an export from a RadGrid. The Css Styles (especially if you do not use the built-in skins) are not carried through to the export. It is possible to customize the export with a little creativity.
    The export creates XHTML which is then passed to the appropriate engine to produce the download file. The solution (once you find it) is pretty clean. It would be nice to have this engine directly exposed (I can think of many re-uses!) and documented.... wish list..
    First, I created a Css.Resx file which contains the following items:
    • GridItem
    • GridItemCell
    • GridHeaderItem
    • GridHeaderItemCell
    • GridItem
    • GridItemCell
    Each of these items contain a definition in classic style sheet format.
    • color:#0000FF;
    • font-size:1.2em;
    So you can now define the appearance of each of these six regions (you can do more regions using the same pattern).
    Setting up to detect the Export
    Telerik examples typically shows the use of an independent button which then sets a variable. We use a similar pattern but make use of the built-in commands and a property on the RadGrid. This is done by adding two event handlers (with supporting code) as shown below. We use the CssClass as a flag variable. 
    The routines below may be put into a utility library and made universally available.

    We use a function that breaks apart the CSS  into a sorted list that is shown in the code. The function is (reasonably) tolerant of format.  You could use
    • .GridItemCell { whatever }
    instead of just the content shown above.
    There are a few got-cha, for example: 
    • color:Red; does not work.
    • color: #FF0000; does work.
    Those are discovered quickly (if it does not work, try an alternative expression!).  I suspect that the CSS level is not comprehensive – so don’t be surprise if some items do not work! Hopefully Telerik will improve the support here (now that there's an example of customization).

    I would love to see an event added that passes in the generated XHTML just before it goes to the rendering engine so I can modify it when needed.
    With the formatting being in a RESX file, it is easy to modify as needed for different customers. You can modify as few or as many formatting attributes as you wish.
  2. Daniel
    Daniel avatar
    4935 posts

    Posted 30 Jun 2010 Link to this post

    Hello Ken,

    We appreciate your taking the time to submit the code-library. As a sign of gratitude for your efforts, we updated your Telerik points.

    Best regards,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Kashif
    Kashif avatar
    2 posts
    Member since:
    Dec 2010

    Posted 01 Dec 2010 Link to this post

    I have tried several suggestions, from this topic as well, to export custom formatting however still no CSS style is applied on the exported file.  I have tried formatting on Item Created and Data Bound, however the export will the following settings still doesn't format my excel file.  I am also adding custom labels in each cell and would like these values to be printed depending on what is actually visible on the grid (i use CSS to hide and show label within a cell).  If label1 is visible then i want that to be printed in exported report and if label2 is visible then i would need that value (one is percentage, and one is actual count).

    aspx file:




    telerik:RadGrid ID="ViewerGrid" runat="server" ShowHeader="true" ShowFooter="true"



    CssClass="ViewerGridStyle" OnItemDataBound="Grid_DataBound" EnableEmbeddedSkins="false"






    <ExportSettings IgnorePaging="true">



    <Excel FileExtension="xls" />



    <Csv ColumnDelimiter="Comma" RowDelimiter="NewLine" />






    <MasterTableView ShowHeadersWhenNoRecords="true">







    code behind on click event of button:
    grid.ExportSettings.Excel.Format = GridExcelExportFormat.Html;
    grid.ExportSettings.ExportOnlyData = false;
    grid.ExportSettings.HideStructureColumns = true;

    Your help in this matter is greatly appreciated.  I am a bit hesitant to install Office for these operations as Microsoft discourages it. 



  4. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 20 Nov 2012 Link to this post

    I am unable to Download the grid into pdf .Please help me    

    <telerik:RadGrid ID="grd" runat="server"  Width="100%"  AllowSorting="true"
                        Skin="Office2007" BorderWidth="2px" BorderColor=" Silver " PageSize="15"
                        AllowPaging= "true" GridLines="Both" AutoGenerateColumns="False"
                        onpagesizechanged="grdTransactions_PageSizeChanged" OnItemCreated="grdTransactions_ItemCreated" OnItemCommand="grdTransactions_ItemCommand">
                        <PagerStyle Mode="NextPrevAndNumeric" />
                        <ExportSettings IgnorePaging="true" OpenInNewWindow="true">
                <Pdf PageHeight="210mm" PageWidth="297mm" PageTitle="SushiBar menu" DefaultFontFamily="Arial Unicode MS"
                    PageBottomMargin="20mm" PageTopMargin="20mm" PageLeftMargin="20mm" PageRightMargin="20mm">
                        <ItemStyle Height="40px" VerticalAlign="Middle" HorizontalAlign="Center" />
                        <HeaderStyle HorizontalAlign="Center" Font-Size="12px" Font-Bold="true" />
                        <AlternatingItemStyle Height="40px" VerticalAlign="Middle" HorizontalAlign="Center" />
                        <MasterTableView TableLayout="Auto" DataKeyNames="TransactionID">
                                <telerik:GridBoundColumn DataField="TransactionID" HeaderText="TransactionID" UniqueName="TransactionID" Visible="false"/>
                                <telerik:GridBoundColumn DataField="DebitAmount" HeaderText="Debit Amount" HeaderStyle-Width="10%" UniqueName="DebitAmount"/>
                                <telerik:GridBoundColumn DataField="CreditAmount" HeaderText="Credit Amount" HeaderStyle-Width="10%" UniqueName="CreditAmount" />

     <br />
                   <asp:Button ID="DownloadPDF" runat="server" Width="100%" CommandName="ExportToPdf" Text="DownloadToPdf" OnClick="BtnDownloadPDF"

    and in .cs file

      public void BtnDownloadPDF(object sender,EventArgs e)
            bool isExport = false;
            isExport = true;
            grdTransactions.ClientSettings.Scrolling.UseStaticHeaders = false;
Back to Top