This is a migrated thread and some comments may be shown as answers.

Excel Export Is Not Working with grid

4 Answers 1395 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Olivier
Top achievements
Rank 1
Olivier asked on 25 Jul 2017, 09:55 AM

I

<div class="panel-body">
                        @(Html.Kendo().Grid<CdsAdmin.ViewModels.TravelPreferenceVM>()
                              .Name("gridTravelPreference")
                              .Columns(columns =>
                              {
                                  columns.Bound(c => c.TPF_ID).Hidden();
                                  columns.Bound(c => c.TPO_ID).Hidden();
                                  columns.Bound(c => c.TPF_NAME).Filterable(true).Width(200);
                                  columns.Bound(c => c.TPF_DESC).Filterable(false).Width(250);
                                  columns.Bound(c => c.TPF_STATUS).ClientTemplate("# if (TPF_STATUS == 1 ) {# Active #} else {# Inative #}#").Filterable(false).Width(100);
                                  columns.Bound(c => c.TPF_PREFERENCE).ClientTemplate("# if (TPF_PREFERENCE == 1 ) {# <i class='fa fa-heart'></i><i class='fa fa-heart'></i> #} " +
                                      "else if (TPF_PREFERENCE == 2 ) {# <i class='fa fa-heart'></i><i class='fa fa-heart'></i><i class='fa fa-heart'></i>  #} " +
                                      "else if (TPF_PREFERENCE == 3 ) {# <i class='fa fa-stars'></i> Exclusive  #} " +
                                      "else if (TPF_PREFERENCE == 0 ) {# Neutral #} " +
                                      "else if (TPF_PREFERENCE == -1 ) {# Hidden #} " +
                                      "else {# TPF_PREFERENCE #}#").Filterable(false).Width(200);
                                  columns.Bound(c => c.HTL_CD).Filterable(false);
                                  columns.Bound(c => c.HCH_ID).Filterable(false);
                                  columns.Command(command => { command.Edit().Text(" "); command.Destroy().Text(" "); }).Width(150);
                              })
                              .ToolBar(toolbar =>
                              {
                                  toolbar.Template(@<text>
                            <a class='k-button k-button-icontext k-grid-excel' href='#'><span class='k-icon k-i-excel'></span>Export Excel</a>
                            <a class='k-button k-button-icontext k-grid-pdf' href='#'><span class='k-icon k-i-pdf'></span>Export PDF</a>
                            <a class='k-button k-button-icontext k-grid-add' href='#'><span class='k-icon k-i-add'></span>Add</a>
                            <a class="k-button k-button-icontext" onclick="addMultiHotels()" style="width:200px;"><span class='k-icon k-i-add'></span>Add multi</a>
                                </text>);
                              })
                              .Editable(editing =>
                              {
                                  editing.Mode(GridEditMode.PopUp).TemplateName("PopupEditors/PopupTravelPreference")
                                      .Window(w => w.Title("Management preferred hotels").AutoFocus(true));
                                  editing.DisplayDeleteConfirmation("Are you sure ?");
                              })
                              .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
                              .Pageable(pageable => pageable
                                           .Refresh(true)
                                           .PageSizes(false)
                                           .ButtonCount(10))
                              .Excel(excel => excel.FileName("HotelExcel.xlsx")
                                                   .Filterable(true)
                                                   .AllPages(true)
                                                   .ProxyURL(Url.Action("ExcelExport", "TravelPreference")))
                              .Pdf(pdf => pdf.AllPages()
                                             .AvoidLinks()
                                             .PaperSize("A4")
                                             .Scale(0.8)
                                             .Margin("2cm", "1cm", "1cm", "1cm")
                                             .Landscape()
                                             .RepeatHeaders()
                                             .TemplateId("pdf-template")
                                             .FileName("HotelPdf.pdf")
                                             .ProxyURL(Url.Action("PdfExport", "TravelPreference")))
                              .Scrollable()
                              .Selectable(selectable => selectable.Mode(GridSelectionMode.Multiple)
                                                                  .Type(GridSelectionType.Cell))
                              .Navigatable()
                              .AllowCopy(true)
                              .Resizable(resize => resize.Columns(true))
                              .Reorderable(reorder => reorder.Columns(true))
                              .NoRecords(n => n.Template("Il n'y a pas des hôtels préféré pour cette compagnie."))
                              .HtmlAttributes(new { style = "height:650px;" })
                              .Events(events => events.Edit("onKendoGridEdit"))
                              .DataSource(dataSource => dataSource
                                  .Ajax()
                                  .PageSize(100)
                                  .ServerOperation(true)
                                  .Events(events => events.Error("KendoGridErrorHandler").RequestStart("onRequestStart").RequestEnd("onRequestEnd"))
                                  .Model(model =>
                                  {
                                      model.Id(p => p.TPF_ID);
                                      model.Field(p => p.TPF_ID).Editable(false);
                                      model.Field(p => p.HOTEL).Editable(false);
                                  })
                                  .Create(update => update.Action("Create", "TravelPreference"))
                                  .Read(read => read.Action("Read", "TravelPreference").Data("additionalInfo"))
                                  .Update(update => update.Action("Update", "TravelPreference"))
                                  .Destroy(update => update.Action("Destroy", "TravelPreference"))
                              ))
                    </div>

My controller

   [HttpPost]

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

 

My js 

function additionalInfo() {
    return {
        companyId: CmpId
    }
}

4 Answers, 1 is accepted

Sort by
0
Olivier
Top achievements
Rank 1
answered on 25 Jul 2017, 10:01 AM

I forgot my read action and i think the problem is when i use server pagination with a read action with addionaldata

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public ActionResult Read([DataSourceRequest]DataSourceRequest request, int companyId)
        {
            List<TravelPreferenceVM> lstVM = new List<TravelPreferenceVM>();
            if (companyId > 0)
                lstVM = travelPreferenceService.GetByCompany(companyId);
            return Json(lstVM.ToDataSourceResult(request));
        }

0
Stefan
Telerik team
answered on 27 Jul 2017, 05:28 AM
Hello Olivier,

Thank you for providing the code.

After inspecting it, it looks good and it should work as expected. Still, in order to determine what may be causing the issue, please share with us what is happening when the Excel export button is clicked? For example, is there any JavaScript error in the console, or if the ExcelExport action is hit in the controller.

Also, providing a fully runnable example will be very helpful as it will allow us to inspect the application and provide a suggestion best suited for it.

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Olivier
Top achievements
Rank 1
answered on 27 Jul 2017, 08:21 AM

Hello Stephan,

Thank you for your reply but i forgot to post the solution. The problem was from the length of JSON sended to the grid for serialisation, I delete the field from my object (ViewModel) and it works. But i have a question, How we handle long Json-String? 

0
Stefan
Telerik team
answered on 31 Jul 2017, 05:23 AM
Hello Olivier,

Thank you for the clarification.

If the JSON length is too large, I can assume that there is an error on the server.

I can suggest checking the following questions on StackOverflow as they demonstrate how to change the JSON max length and provide more information on the different JSON length limitations that can be hit:

https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config/1151993#1151993

https://stackoverflow.com/questions/1262376/is-there-a-limit-on-how-much-json-can-hold

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Rajiv
Top achievements
Rank 1
commented on 16 Apr 2024, 07:47 AM

pls help on this 

 

Neli
Telerik team
commented on 18 Apr 2024, 04:47 AM

Hi Rajiv,

As far as I understand the issue you are facing is related to the Kendo React, thus I would suggest submitting your question in the dedicated forum:

https://www.telerik.com/forums/kendo-ui-react

If this is not the case and you have questions related to the Kendo UI for jQuery components, please let us know.
Regards,
Neli

Tags
Grid
Asked by
Olivier
Top achievements
Rank 1
Answers by
Olivier
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or