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.
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
{
public static class ExcelUtilities
{
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