Click here to Skip to main content
15,889,863 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Save a Telerik Grid to Excel with Filtering

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
20 May 2014CPOL 10.1K   2  
How to save a Silverlight Telerik grid content to Excel

Introduction

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

C#
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);
                    }
                }
        }
    }
}

History

  • 20th May, 2014: Initial version

License

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


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
-- There are no messages in this forum --