RadGrid telerik problems with export to excel

gruber picture gruber · Jan 17, 2012 · Viewed 42.8k times · Source

I added folowing line to my MasterTableView:

<CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true" ShowExportToCsvButton="true" />

But when I click on that, loading div appears and then data on the grid is refreshed and nothing else happens. There is no "save file" window.

Am I missing anything ?

Answer

Niran picture Niran · Jan 19, 2012

Add the below code before the Grid:

<script type="text/javascript">
        function onRequestStart(sender, args)
        {
            if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 ||
                    args.get_eventTarget().indexOf("ExportToWordButton") >= 0 ||
                    args.get_eventTarget().indexOf("ExportToCsvButton") >= 0)
            {
                args.set_enableAjax(false);
            }
        }
    </script>

    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <ClientEvents OnRequestStart="onRequestStart" />
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

For the code behind add the below one:

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName ||
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToWordCommandName ||
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToPdfCommandName ||
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
    {
        gridResult.ExportSettings.IgnorePaging = true;
        gridResult.ExportSettings.OpenInNewWindow = true;
        if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName)
            gridResult.MasterTableView.ExportToExcel();
        else if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToWordCommandName)
            gridResult.MasterTableView.ExportToWord();
        else if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName)
            gridResult.MasterTableView.ExportToCSV();
        else if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToPdfCommandName)
            gridResult.MasterTableView.ExportToPdf();

    }
}

reference: http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/exporting/defaultcs.aspx