I am also trying to use this example, just like my previous answer, only the jQuery binding event will be different.
You just need to make changes to the code by adding the grid Events(x => x.ExcelExport("excelExport")) event Events(x => x.ExcelExport("excelExport")) and the jQuery function excelExport(e) {} . Also use only Hidden(true) to hide the grid column.
ViewModel looks something like this:
public class ContactViewModel { public string NameSurname { get; set; } public string InstituteName { get; set; } public string CityName { get; set; } public string RegionName { get; set; } public string ContactMobile { get; set; } public string ContactAddress { get; set; } }
The controller will be:
public class TestController : Controller { public ActionResult Index() { return View(); } public ActionResult Index_Read([DataSourceRequest]DataSourceRequest request) { var listOfContactViewModel = new List<ContactViewModel>() { new ContactViewModel(){ NameSurname = "N1", InstituteName = "I1", CityName ="C1",RegionName = "R1",ContactMobile = "M1", ContactAddress = "C1" }, new ContactViewModel(){ NameSurname = "N2", InstituteName = "I2", CityName ="C2",RegionName = "R2",ContactMobile = "M2", ContactAddress = "C2" }, new ContactViewModel(){ NameSurname = "N3", InstituteName = "I3", CityName ="C3",RegionName = "R3",ContactMobile = "M3", ContactAddress = "C3" }, new ContactViewModel(){ NameSurname = "N4", InstituteName = "I4", CityName ="C4",RegionName = "R4",ContactMobile = "M4", ContactAddress = "C4" }, new ContactViewModel(){ NameSurname = "N5", InstituteName = "I5", CityName ="C5",RegionName = "R5",ContactMobile = "M5", ContactAddress = "C5" } }; return Json(listOfContactViewModel.ToDataSourceResult(request),JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult Excel_Export_Save(string contentType, string base64, string fileName) { var fileContents = Convert.FromBase64String(base64); return File(fileContents, contentType, fileName); } }
And view for this:
<h2>Index</h2> @(Html.Kendo().Grid<KendoUIMVC5.Models.ContactViewModel>() .Name("Grid") .Events(x => x.ExcelExport("excelExport")) .Columns(columns => { columns.Bound(m => m.NameSurname).Title("Name Surname").Width("%100"); columns.Bound(m => m.InstituteName).Title("Institute Name").Width("250px"); columns.Bound(m => m.CityName).Title("City").Width("145px"); columns.Bound(m => m.RegionName).Title("Region").Width("145px"); columns.Bound(m => m.ContactMobile).Title("Mobile").Width("125px"); columns.Bound(m => m.ContactAddress).Title("Address").Hidden(true); //I want to export these fields columns.Bound(m => m.ContactAddress).Title("Address").Hidden(false); //I want to export these fields }) .ToolBar(toolbar => { toolbar.Template(@<text> <div class="toolbar"> <button class="btn btn-primary btn-xs pull-right k-button k-button-icontext k-grid-excel"> <span class="k-icon k-excel"></span> Liste (xls) </button> </div> </text>); }) .Excel(excel => excel .FileName("List.xlsx") .Filterable(true) .AllPages(true) .ProxyURL(Url.Action("Excel_Export_Save", "Test")) ) .DataSource(dataSource => dataSource .Ajax() .Read(read => read.Action("Index_Read", "Test")) .ServerOperation(false) .PageSize(12) ) ) <script type="text/javascript"> var exportFlag = false; function excelExport(e) { if (!exportFlag) { e.sender.showColumn(5); e.sender.showColumn(6); e.preventDefault(); exportFlag = true; setTimeout(function () { e.sender.saveAsExcel(); }); } else { e.sender.hideColumn(5); e.sender.hideColumn(6); exportFlag = false; } } </script>