Click here to Skip to main content
Click here to Skip to main content

Save a telerik grid to Excelwith filtering

, 20 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to save a silverlight telerik grid content to excel

Introduction

This is a quick function to allow you to export a Telerik data grid content to Excel with option to use or ignore the on-screen filtering applied.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using Telerik.Windows.Controls;

using System.Linq;
using System.Collections.Generic;

namespace HFAdminCentral2011
{

    /// <summary>
    /// Single global class to take care of Excel export related functionality 
    /// </summary>
    public static class ExcelUtilities
    {

        /// <summary>
        /// Exports the grid passed in to the given filename, as an excel file
        /// </summary>
        /// <param name="gridToexport" />
        /// The telerik datagrid that is the source of the data
        /// 
        /// <param name="filename" />
        /// The full path filename to save the file as (this can be changed by the user)
        /// 
        /// <param name="exportAsFiltered" />
        /// If true, export as filtered on screen - otherwise export all underlying data
        /// 
        public static void ExportTelerikGridToExcel(Telerik.Windows.Controls.RadGridView gridToexport,
            string filename,
            bool exportAsFiltered)
        {

            string extension = "xls";
                SaveFileDialog dialog = new SaveFileDialog()
                {
                    DefaultFileName = filename ,
                    DefaultExt = extension,
                    Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
                    FilterIndex = 1
                };
                if (dialog.ShowDialog() == true)
                {
                    using (Stream stream = dialog.OpenFile())
                    {
                        GridViewExportOptions options = new GridViewExportOptions()
                            {
                                Format = ExportFormat.ExcelML,
                                ShowColumnHeaders = true,
                                ShowColumnFooters = false,
                                ShowGroupFooters = false
                            };

                        if (exportAsFiltered)
                        {
                            options.Items = ((System.Collections.IEnumerable)gridToexport.Items);
                        }

                        gridToexport.Export(stream,
                         options);
                    }
                }

        }



    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Duncan Edwards Jones
Software Developer (Senior)
Ireland Ireland
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150326.1 | Last Updated 20 May 2014
Article Copyright 2014 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid