How to exclude columns during export

12 posts, 1 answers
  1. J
    J avatar
    6 posts
    Member since:
    Apr 2017

    Posted 18 Sep 2017 Link to this post

    Hello,

    I'm using this code in my grid:

     

       .ToolBar(tools => tools.Excel())
         .Excel(excel => excel
            .FileName("Ci_view.xlsx")
            .Filterable(true)
            .ProxyURL(Url.Action("ExportExcelSave", "Ci"))
            .AllPages(true)

    My question is; how can I exclude some columns from my Excel?

     

    Thanks!!

     

  2. Answer
    Georgi
    Admin
    Georgi avatar
    695 posts

    Posted 21 Sep 2017 Link to this post

    Hi,

    Possible solution is to hide the columns you want to exclude when the excelExport event is fired. The following code block illustrates how to hide a column before the export and then show it back after the export is done:

    var exportFlag = false;
    $("#grid").data("kendoGrid").bind("excelExport", function (e) {
        if (!exportFlag) {
            e.sender.hideColumn(1); // index of the column you want to exclude
            e.preventDefault();
            exportFlag = true;
            setTimeout(function () {
                e.sender.saveAsExcel();
            });
        } else {
            e.sender.showColumn(1);
            exportFlag = false;
        }
    });
     
    Please give it a try and let me know if it works for you.


    Regards,
    Georgi
    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.
  3. Michael
    Michael avatar
    29 posts
    Member since:
    Jun 2015

    Posted 18 Jan 2018 in reply to Georgi Link to this post

    @Georgi Is thee any other options?
  4. Georgi
    Admin
    Georgi avatar
    695 posts

    Posted 22 Jan 2018 Link to this post

    Hello Michael,

    Currently the only solution in order to exclude certain column from the exported document is to hide it.

    If you would like to request the feature to be included out of the box for the Kendo Grid I can suggest submitting a request in our UserVoice portal. This way the users can vote for the feature. Based on the traction the request gathers with the community the developers will consider implementing it in a future release.



    Regards,
    Georgi
    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.
  5. Jamie
    Jamie avatar
    22 posts
    Member since:
    Aug 2012

    Posted 27 Feb 2018 in reply to Georgi Link to this post

    Hi, I added the functionality given and now the workbook is getting renamed to export.xlsx, any idea why the code provided here would change the name of my document? It was named MembersList.xlsx before that code was added.
  6. Jamie
    Jamie avatar
    22 posts
    Member since:
    Aug 2012

    Posted 27 Feb 2018 in reply to Jamie Link to this post

    Jamie said:Hi, I added the functionality given and now the workbook is getting renamed to export.xlsx, any idea why the code provided here would change the name of my document? It was named MembersList.xlsx before that code was added.

     

     

    Nevermind, it works on the live environment, just not staging. Not a dealbreaker. Disregard post. I wish I could delete it lol!

  7. Daniel Blendea
    Daniel Blendea avatar
    43 posts
    Member since:
    Mar 2010

    Posted 07 Oct 2020 in reply to Georgi Link to this post

    Not exactly useful.

    This code hides the column when the user clicks the "Export as Excel" but doesn't show it after the excel export completes.

    So the user must click again on the "Export as Excel" button.

    There should be another event handler to show the column again.

  8. Daniel Blendea
    Daniel Blendea avatar
    43 posts
    Member since:
    Mar 2010

    Posted 07 Oct 2020 in reply to Georgi Link to this post

    Something like this should be implemented:

     

    e.sender.saveAsPDF().then(function(){
        e.sender.showColumn(1);
        exportFlag = false;
    });

     

  9. Georgi
    Admin
    Georgi avatar
    695 posts

    Posted 08 Oct 2020 Link to this post

    Hello Daniel,

    The column is hidden before the export but then after the export, the column is shown back. Please take a look in the else clause.

     

    var exportFlag = false;
    $("#grid").data("kendoGrid").bind("excelExport", function (e) {
        if (!exportFlag) {
            e.sender.hideColumn(1); // index of the column you want to exclude
            e.preventDefault();
            exportFlag = true;
            setTimeout(function () {
                e.sender.saveAsExcel();
            });
        } else {
            e.sender.showColumn(1);
            exportFlag = false;
        }
    });

    For better understanding, please refer to the following sample:

    Note that the second column is not present in the exported file, but still the column is visible after the export.

     

    Regards,
    Georgi
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  10. Daniel Blendea
    Daniel Blendea avatar
    43 posts
    Member since:
    Mar 2010

    Posted 12 Oct 2020 in reply to Georgi Link to this post

    Right,

    Maybe I should explain my case. I don't use the toolbar button to trigger the export to excel, but a different button:

    $("#export-to-excel").on("click", function(e) {
     grid.saveAsExcel();
    });

     

    So the export starts, but then e.sender.saveAsExcel(); triggers the export once again.

    And in my case, it means a second call to the server, to get the data.

    Which is not a good idea, because some customers have a lot of records to export.

    So I need to trigger the excelExport event only once.

  11. Georgi
    Admin
    Georgi avatar
    695 posts

    Posted 15 Oct 2020 Link to this post

    Hi Daniel,

    Then you could hide the column within the click handler of the button which exports the grid. I have modified the dojo to demonstrate the same:

     

    Regards,
    Georgi
    Progress Telerik

    Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive, special prizes, and more, for FREE?! Register now for DevReach 2.0(20).

  12. Daniel Blendea
    Daniel Blendea avatar
    43 posts
    Member since:
    Mar 2010

    Posted 15 Oct 2020 in reply to Georgi Link to this post

    Great! That solves the problem.

    Thank you!

Back to Top