Excel export with dynamic names

5 posts, 0 answers
  1. Joe
    Joe avatar
    220 posts
    Member since:
    May 2012

    Posted 17 Jan 2018 Link to this post

    Is there a way to specify a dynamic name for the export to Excel function?  The FileName() method takes a string, but I want the name to reflect certain filter conditions within the grid, and need to change the name of the exported file.  Ideally, I'd like to be able to call a javascript function that would build the name dynamically.  Any way to accomplish this?
  2. Stefan
    Admin
    Stefan avatar
    2913 posts

    Posted 19 Jan 2018 Link to this post

    Hello, Joe,

    The desired result can be achieved on the excelExport event by programmatically setting a new value of the exported workbook name:

    https://docs.telerik.com/kendo-ui/api/javascript/ui/grid/events/excelexport#excelexport

    https://dojo.telerik.com/oDeya

    I hope this is helpful.

    Regards,
    Stefan
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Joe
    Joe avatar
    220 posts
    Member since:
    May 2012

    Posted 19 Jan 2018 in reply to Stefan Link to this post

    Thanks.  Now I just have to work out how this works for the MVC component.
  4. Joe
    Joe avatar
    220 posts
    Member since:
    May 2012

    Posted 19 Jan 2018 in reply to Joe Link to this post

    I just don't know what I'm supposed to do with this.  After my grid Reads it's data (AutoBind = false), I call this little segment:

    $("#licenseGrid").data("kendoGrid").bind("excelExport", function(e) {
              e.workbook.filename = colorModel.get("SelectedPeo") + "_LicenseRegistration.xlsx";
    });

     

    And when my grid loads, I click the excel button, it's not coming up with this name at all, it's just coming up as plain Export.xlsx.  So how do I use this with MVC?  

  5. Alex Hajigeorgieva
    Admin
    Alex Hajigeorgieva avatar
    1037 posts

    Posted 23 Jan 2018 Link to this post

    Hi, Joe,

    The approach for adding an excelExport event handler to an already initialized grid is correct. The issue comes from the casing of the filename property.

    Change it to camel case as below:

    $("#licenseGrid").data("kendoGrid").bind("excelExport", function(e) {
      e.workbook.fileName = colorModel.get("SelectedPeo") + "_LicenseRegistration.xlsx";
    });

    Alternatively, you may use the Razor syntax to attach the vent handler in the configuration of the grid. 

    @(Html.Kendo().Grid<ProjectName.Models.ProductViewModel>()
      .Events(events => events
       .ExcelExport("onExcelExport")
     
    <script>
    function onExcelExport(e){
       e.workbook.fileName = colorModel.get("SelectedPeo") + "_LicenseRegistration.xlsx";
    }
    </script>

    Kind Regards,
    Alex Hajigeorgieva
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top