Set excel filename dynamically

3 posts, 1 answers
  1. Danny
    Danny avatar
    37 posts
    Member since:
    May 2012

    Posted 15 Mar 2017 Link to this post

    I have a grid with the export to excel functionality turned on:

     

    @(Html.Kendo().Grid<IncompleteCOATests>()
                              .Name("IncompleteCOATestsGrid")
                              .HtmlAttributes(new { style = "width: 980px" })
                              .Columns(columns =>
                              {
                                  columns.Bound(c => c.UnitId).Title("Unit Id");
                                  columns.Bound(c => c.ParentId).Title("Parent Id");
                                  columns.Bound(c => c.Qparam).Title("QParam");
                                  columns.Bound(c => c.TestAlias).Title("Test Name");                                                    
                              })
                              .ToolBar(tools => tools.Excel())
                              .Excel(excel => excel
                                        .FileName("Missing Tests.xls")
                                        .Filterable(true)
                                        .ProxyURL(Url.Action("Excel_Export_Save", "Grid")))
                              .NoRecords(x => x.Template(@ResourceMessages.NoRecordFoundForConsignee))
                              .AutoBind(false)
                              .Pageable(pageable => pageable
                                  .Enabled(false)
                                  .Refresh(true)
                                  .PageSizes(false))
                              .DataSource(dataSource => dataSource
                                  .Ajax()
                                  .Events(events => events.Error("GlobalModule.onError"))
                                  .Read(read => read.Url(Url.HttpRouteUrl("ActionApi", new { controller = "PoPsApi", action = "GetIncompleteCOATests" })).Type(HttpVerbs.Get).Data("IncompleteCoAsModule.getLoadOrderItem")))
     
    )

     

    It databinds on a client-side button press

    export function showMissingTests(e) {
        $('#IncompleteCOATestsGrid').data("kendoGrid").dataSource.read();
    }

     

    Is it possible to set the excel.Filename property client side? 

    When/how in the page lifecyle would I do that? 

     

     

     

  2. Answer
    Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    1080 posts

    Posted 17 Mar 2017 Link to this post

    Hi Danny,

    To set the name of the Kendo UI Grid Excel export dynamically, add an event handler to the excelExport event. The generated workbook is available for modifications, including the filename at this point:

    http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#events-excelExport

    The API reference above includes a runnable sample which shows how to change the name but I extended it a little for more convenience at:

    http://dojo.telerik.com/AJOXo

    Kind Regards,
    Alex Hajigeorgieva
    Telerik by Progress
    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.
  3. Danny
    Danny avatar
    37 posts
    Member since:
    May 2012

    Posted 17 Mar 2017 in reply to Alex Hajigeorgieva Link to this post

    @(Html.Kendo().Grid<IncompleteCOATests>()
                              .Name("IncompleteCOATestsGrid")
                              .HtmlAttributes(new { style = "width: 980px" })
                              .Columns(columns =>
                              {
                                  columns.Bound(c => c.UnitId).Title("Unit Id");
                                  columns.Bound(c => c.ParentId).Title("Parent Id");
                                  columns.Bound(c => c.Qparam).Title("QParam");
                                  columns.Bound(c => c.TestAlias).Title("Test Name");
                              })
                              .ToolBar(tools => tools.Excel())
                              .ToolBar(tools => tools.Custom().Text("<span class='glyphicon glyphicon-arrow-left'></span> Back To Incomplete CoAs").HtmlAttributes(new { id = "backToResultsBtn"}))
                              .Excel(excel => excel
                                        .Filterable(true)
                                        .ProxyURL(Url.Action("Excel_Export_Save", "Grid")))
                              .Events(events => events.ExcelExport("IncompleteCoAsModule.incompleteCOATestsGridOnExcelExport"))
                              .NoRecords(x => x.Template(@ResourceMessages.NoRecordFoundForConsignee))
                              .AutoBind(false)
                              .Pageable(pageable => pageable
                                  .Enabled(false)
                                  .Refresh(true)
                                  .PageSizes(false))
                              .DataSource(dataSource => dataSource
                                  .Ajax()
                                  .Events(events => events.Error("GlobalModule.onError"))
                                  .Read(read => read.Url(Url.HttpRouteUrl("ActionApi", new { controller = "PoPsApi", action = "GetIncompleteCOATests" })).Type(HttpVerbs.Get).Data("IncompleteCoAsModule.getLoadOrderItem")))
     
    )

     

    Added client events: 

    export function incompleteCOATestsGridOnExcelExport(e) {
            e.workbook.fileName = orderId.trim() + "-" + orderItemNumber + " Load ID: " + loadId + " Missing Tests ";
        }

     

    This got it done, thanks for the quick reply!

     

     

     

Back to Top