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

How to exclude columns during export

11 Answers 3283 Views
Grid
This is a migrated thread and some comments may be shown as answers.
J
Top achievements
Rank 1
J asked on 18 Sep 2017, 11:54 AM

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!!

 

11 Answers, 1 is accepted

Sort by
0
Accepted
Georgi
Telerik team
answered on 21 Sep 2017, 09:14 AM
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.
0
Michael
Top achievements
Rank 1
answered on 18 Jan 2018, 04:18 PM
@Georgi Is thee any other options?
0
Georgi
Telerik team
answered on 22 Jan 2018, 10:45 AM
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.
0
Jamie
Top achievements
Rank 2
answered on 27 Feb 2018, 03:59 PM
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.
0
Jamie
Top achievements
Rank 2
answered on 27 Feb 2018, 04:01 PM

[quote]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.[/quote]

 

 

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

0
Daniel Blendea
Top achievements
Rank 1
answered on 07 Oct 2020, 12:55 PM

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.

0
Daniel Blendea
Top achievements
Rank 1
answered on 07 Oct 2020, 03:37 PM

Something like this should be implemented:

 

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

 

0
Georgi
Telerik team
answered on 08 Oct 2020, 12:52 PM

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/.

0
Daniel Blendea
Top achievements
Rank 1
answered on 12 Oct 2020, 09:38 AM

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.

0
Georgi
Telerik team
answered on 15 Oct 2020, 06:41 AM

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).

0
Daniel Blendea
Top achievements
Rank 1
answered on 15 Oct 2020, 07:44 AM

Great! That solves the problem.

Thank you!

Tags
Grid
Asked by
J
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Michael
Top achievements
Rank 1
Jamie
Top achievements
Rank 2
Daniel Blendea
Top achievements
Rank 1
Share this question
or