Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > Grid > Formatting Exports from Telerik RadGrid (PDF,Word, Excel)

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

Feed from this thread
  • Answer Ken Lassesen avatar

    Posted on Jun 23, 2010 (permalink)

    Requirements

    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

    C#

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    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.
    Attached files

    Reply

  • Daniel Daniel admin's avatar

    Posted on Jun 30, 2010 (permalink)

    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

    Reply

  • Kashif avatar

    Posted on Dec 1, 2010 (permalink)

    Hi,
    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"

     

     

    Skin="360Theme">

     

     

    <ExportSettings IgnorePaging="true">

     

     

    <Excel FileExtension="xls" />

     

     

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

     

     

    </ExportSettings>

     

     

    <MasterTableView ShowHeadersWhenNoRecords="true">

     

     

    </MasterTableView>

     

     

    </telerik:RadGrid>

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

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

    thanks,

     

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Code Library / ASP.NET and ASP.NET AJAX > Grid > Formatting Exports from Telerik RadGrid (PDF,Word, Excel)