Kendo Grid Export to Excel -All Pages not working

15,244

Solution 1

Figured out what it was I was doing wrong. The issue wasn't with the Excel issue, it was with my grid in general. I was tying the grid to a List that was part of my ViewModel, which was being populated on page load. I should have instead left the data blank and only specified the type of the grid. That way the Read Action fetches the data when the grid loads AND when the excel is generated. The new code should look like this:

@(Html.Kendo().Grid<CloudUsages>()
.Name("PCloudUsages")
.ToolBar(toolbar =>
{
    toolbar.Excel().HtmlAttributes(new { @class = "toolbar-field" });
})
.Columns(columns =>
{
    columns.Bound(c => c.ProjectCode).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains").ShowOperators(false)));
    columns.Bound(c => c.ProjectName).Title("ProjectName").Filterable(ftb => ftb.Cell(cell => cell.Operator("contains").ShowOperators(false)));
})
.Pageable(p => p.ButtonCount(5).PageSizes(new int[] { 10, 20, 50, 100 }))
.Filterable(ftb => ftb.Mode(GridFilterMode.Row))
.Sortable()
.Excel(excel => excel.FileName("CloudUsages.xlsx").Filterable(true).ProxyURL(Url.Action("ExportExcel", "Admin")).AllPages(true))
.DataSource(source => source
    .Ajax()
     .Model(m => m.Id(itm => itm.ProjectName))
     .Read(read => read.Action("PCloudUsages_Read", "Admin").Data("GetDates"))
     .Sort(sort => sort.Add(itm => itm.ProjectName).Descending())
     )
)

Solution 2

I landed on this page for the search "Kendo mvc grid export to excel not working" But the scenario here is different. For people like me I am posting the possible solution

You must be missing out the jszip.min.js file reference

<script src="~/Scripts/kendo/jszip.min.js"></script>

Solution 3

Use @(Html.Kendo().Grid<TYPE>() instead of @(Html.Kendo().Grid(Model.CloudUsage). This way you can still define columns based on the properties of the type you used, this is also an advantage over @(Html.Kendo().Grid<dynamic>() if you know what your type will be.

Share:
15,244
Matt Kagan
Author by

Matt Kagan

Updated on June 27, 2022

Comments

  • Matt Kagan
    Matt Kagan almost 2 years

    I have a pageable and filterable grid which I set up to export to excel using the new Kendo Grid Excel feature. However, even when I set AllPages to be true I only get the first 10 results, no matter what I set the pagesize to. Removing the Pageable attribute gives me the full reults. Anyone else have problems with this?

    Here's the setup for my grid.

    @(Html.Kendo().Grid(Model.CloudUsage)
    .Name("PCloudUsages")
    .ToolBar(toolbar =>
    {
        toolbar.Excel().HtmlAttributes(new { @class = "toolbar-field" });
    })
    .Columns(columns =>
    {
        columns.Bound(c => c.ProjectCode).Filterable(ftb => ftb.Cell(cell => cell.Operator("contains").ShowOperators(false)));
        columns.Bound(c => c.ProjectName).Title("ProjectName").Filterable(ftb => ftb.Cell(cell => cell.Operator("contains").ShowOperators(false)));
    })
    .Pageable(p => p.ButtonCount(5).PageSizes(new int[] { 10, 20, 50, 100 }))
    .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
    .Sortable()
    .Excel(excel => excel.FileName("CloudUsages.xlsx").Filterable(true).ProxyURL(Url.Action("ExportExcel", "Admin")).AllPages(true))
    .DataSource(source => source
        .Ajax()
         .Model(m => m.Id(itm => itm.ProjectName))
         .Read(read => read.Action("PCloudUsages_Read", "Admin").Data("GetDates"))
         .Sort(sort => sort.Add(itm => itm.ProjectName).Descending())
         )
    )
    

    And the controller method

    public ActionResult ExportExcel(string contentType, string base64, string fileName)
    {
        var fileContents = Convert.FromBase64String(base64);
    
        return File(fileContents, contentType, fileName);
    }
    

    Edit: I have noticed that changing the "pageSize" attribute of the datasource changes the number of rows in the excel file. So it seems that it always produces the excel file the size of the Datasource Pagesize, no matter if the AllPages is set to true, or what the pagesize is set to on the grid.

  • AlexFreitas
    AlexFreitas over 9 years
    I still don't know what you did differently because this code is exactly the same as what you've posted previously. I'm having the same issue and my code is very similar to yours. When I click on the Export to Excel, nothing happens :(
  • Matt Kagan
    Matt Kagan over 9 years
    @AlexFreitas - my Issue wasn't that nothing worked - it was that no matter what page size I set it always unloaded only the first 10 rows into excel. The reason why this was happening is that I was tying the grid to a specific object in my model which was only being fetched on load and not changed when the page size changed.
  • roberth
    roberth about 9 years
    I landed on this page because I had the same problem: I did bind the Grid to the Model and also used the Datasource Read-action. The solution was to not bind to the model. You can do that by declaring the Grid this way: "@(Html.Kendo().Grid<dynamic>()"
  • AliRıza Adıyahşi
    AliRıza Adıyahşi about 8 years
    Changed line @(Html.Kendo().Grid(Model.CloudUsage) -> @(Html.Kendo().Grid<CloudUsages>()
  • Jared Beach
    Jared Beach almost 8 years
    What version does it depend on? 3.0 does not seem to work and neither did 2.0
  • Jared Beach
    Jared Beach almost 8 years
    2.4.0 .. figured it out
  • DanO
    DanO almost 5 years
    It's easy to miss the extra script reference in the demo! demos.telerik.com/aspnet-core/grid/excel-export
  • Vince
    Vince over 2 years
    Thank you! This helped me. I got my jszip from cloudflare.com cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js