New to Kendo UI for jQueryStart a free 30-day trial

Export Grid to Excel with the RadSpreadStreamProcessing Library

Environment

ProductProgress® Kendo UI® Grid for jQuery
ProductRadSpreadStreamProcessing

Description

We have been using the excel export from the Kendo UI Grid (client-side) which has been working very well when we have small datasets.

We now have a larger data set and the client-side export timesout so we need to use server-side (possibly radspreadprocessing library) to generate an excel spreadsheet but it needs to be based on the current view (i.e. with all client-side filters applied) - are you able to have an example where this can be demonstrated?

Solution

As one of our DevCraft Complete clients, you could take advantage of the DataSourceRequest/DataSourceResult classes from the Kendo.Mvc.dll. Set the data source as type aspnetmvc-ajax so it can be used in the MVC context and process the server paging, grouping, aggregating, filtering and sorting with the ToDataSourceResult() extension method.

  1. Include the kendo.aspnetmvc.min.js script in the _Layout.cshtml
  2. Add a custom toolbar template to trigger the server-side export
  3. Add the custom click function and post a request to the server-side method
  4. Create a helper model that can deserialize the column settiings on the server
  5. Include the server-side dependencies - Kendo.Mvc.dll, Telerik.Documents.SpreadsheetStreaming.dll, Telerik.Windows.Zip.dll
tab-Index.cshtml
    <script id="template" type="text/x-kendo-template">
        <a class="k-button" href="\#" onclick="return toolbar_click()"><span class="k-icon k-i-excel"></span>Export To Excel</a>
    </script>

    toolbar: [{ template: kendo.template($("#template").html()) }],
    dataSource: {
        type: "aspnetmvc-ajax",
        transport: {
            read: {
                url: "/Grid/Orders",
                type: "POST"
            }
        },
        pageSize: 10,
        serverPaging: true,
        serverSorting: true,
        serverFiltering: true,
        serverGrouping: true,
        serverAggregates: true,
        schema: {
            data: "Data",
            total: "Total"
        },
    }

    function toolbar_click() {
        var grid = $("#grid").data("kendoGrid");

        var exportOptions = {
            format: "XLSX",
            title: "Export",
            createUrl: "/Grid/Export",
            downloadUrl: "/Grid/Download"
        }

        // Get the current filter/group/sort options of the grid data source

        var options = grid.getOptions().dataSource;

        // modify it to have no pageSize for allPages Excel Export

        delete options.pageSize;
        delete options.page;

        // change the request url and send the columns of the grid and other data. 
        // NB! The data property name must match the parameter in the controller, e.g. "columns", "title"

        options.transport.read = {
            type: "GET",
            url: exportOptions.createUrl,
            data: {
                columns: JSON.stringify(grid.columns),
                title: exportOptions.title,
                format: exportOptions.format
            }
        }

        // create a new data source instance with the new options
        var dataSource = new kendo.data.DataSource(options);

        // make a read request to create the Export in GridController/Export and store it in the Session
        // in the promise callback of the read() method, change the location of the window so you can download the generated file 
        dataSource.read().then(function () {
            window.location.replace(kendo.format("{0}?format={1}&title={2}",
                exportOptions.downloadUrl,
                exportOptions.format,
                exportOptions.title));
        });

        return false;
    }
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support