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

Hide column from grid while exporting to excel

1 Answer 878 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rajrup
Top achievements
Rank 1
Rajrup asked on 29 Jul 2019, 11:57 AM

     I want to hide/remove one column when the data exported to excel..But that should be visible in grid. Can anybody please help regarding this ?? Thanks in advance.

 

Here is my whole code :

 

   <div class="container content-xs" style="padding-top:10px">
          <form id="EnableAssForm" action="/admin/EnableAssesmentStatus/" 
           method="post">
            <div class="row">
                @Html.Partial("_AdminSideBar")
                <div class="col-md-9">
                    <div class="panel panel-default">
                        <!-- Default panel contents -->
                        <a title="add new ground" class="btn btn-success btn-sm 
               pull-right showwaiting" href="@Url.Action("Create")"><i class="fa 
                  fa-plus-square fa-margin"></i>Add</a>
                        @(Html.Kendo().Grid<Database.Model.UserSummaryInfo>()
                .Name("Grid")
                .Columns(col =>
                {
                col.Bound(c => c.ApplicationUserId).Hidden();
                col.Bound(c => c.MemberId).Title("Member ID");
                col.Bound(c => c.Visit).Title("Visit");
                col.Bound(c => c.CreatedDate).Title("Visit Start Date");
                col.Bound(c => c.LogInCount).Title("LogIn Count");
                col.Bound(c => c.SurveyStatus).Title(" Survey Status");
               
                col.Bound(c => c.ApplicationUserId).HeaderTemplate(@<text>Action</text>).ClientTemplate("# if(SurveyStatus == 'Did Not Attempt') { #" + "<a  class='btn btn-primary disabled' style='display: none;' href='" + Url.Action("TestDetails", "Admin") + "?id=#= TestSummaryId #&Year=#=Year#'" + " >Details</a>" + "# }else{#" + "<a  class='btn btn-primary enabled' style='width:60px' href='" + Url.Action("TestDetails", "Admin") + "?id=#= ApplicationUserId #&Year=#=Year #&testSummaryId=#=TestSummaryId#'" + ">Details</a>" + "# }#")
                                                                                                                                      .HeaderHtmlAttributes(new { style = "text-align: center;font-size:18px" });
                })
    .ToolBar(toolbar => toolbar.Template(@<text>
                <div class="pull-left index-header">Test Summary</div>
                <button type="button" class="btn btn-primary rounded pull-right margin-right-10" onclick="clearFiter()"><i class="fa fa-times-circle-o margin-right-5"></i> Clear Filter</button>
                <a style="padding-right:5px;" class="k-button-icontext k-grid-excel btn btn-primary pull-right  margin-right-10" href="#"><span class="fa fa-file-excel-o"></span>Export to Excel</a>
    </text>))
    .Excel(excel => excel
    .FileName(DateTime.Now.Date.ToShortDateString() + " " + "GetUserSummary.xlsx")
    
    
    .AllPages(false)
    
    .ProxyURL(Url.Action("Excel_Export_Save", "Admin")))
    .Pageable(paging => paging.PageSizes(new int[] { 100, 500, 1000 }).Refresh(true).ButtonCount(5).Info(true).Input(true))
    .Sortable(sortable =>
    {
      sortable.SortMode(GridSortMode.SingleColumn);
    })
    .Groupable()
    .Scrollable(s => s.Height("auto"))
    .Filterable(filterable => filterable.Operators(operators => operators.ForNumber(nmbr => nmbr.Clear().IsEqualTo("Is equal to").IsLessThan("Less than").IsGreaterThan("Greater than").IsNotEqualTo("Not equal to")).ForString(str => str.Clear().Contains("Contains").IsEqualTo("Is equal to").StartsWith("Starts with").IsNotEqualTo("Is not equal to")).ForDate(date => date.Clear().IsGreaterThan("Is after").IsLessThan("Is Before").IsGreaterThanOrEqualTo("Is after or equal to").IsLessThanOrEqualTo("Is before or equal to"))))
    .Resizable(resize => resize.Columns(true))
    
    .Events(e => e.ExcelExport("Hidecolumn"))
    
    .DataSource(datasource =>
    datasource
    .Ajax()
    .Sort(sort => {
      sort.Add(c => c.MemberId).Ascending();
      sort.Add(c => c.Visit).Ascending();
    })
    .PageSize(10)
    .Read(read => read.Action("GetUserSummaryList", "Admin"))
    )
                        )
                    </div>
                </div>
            </div>
            <!-- End Content -->
        </form>
    </div>


     <script>
       var exportFlag = false;
       $("#Grid").data("kendoGrid").bind("excelExport", function (e) {
        debugger;
        if (!exportFlag) {
          e.sender.hideColumn(2);
          e.preventDefault();
          exportFlag = true;
          setTimeout(function () {
            e.sender.saveAsExcel();
          });
        } else {
          e.sender.showColumn(2);
          exportFlag = false;
        }
      });
    
    
        function Hidecolumn(e) {
    
          e.sender.hideColumn(2);
         }

</script>

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 01 Aug 2019, 10:19 AM
Hi Rajrup,

I noticed that you have attached an event handler to the excel export event and within it you hide the third column. Actually this is the approach we suggest to your clients. Does the exported document contain the `Visit` column?

Could you please make sure there are no JS errors in the console of your browser?


Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Rajrup
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Share this question
or