Export Grid Data To Excel In Advance Kendo UI Using MVC WEB API And Entity Framework

Main Objective of this Article

This article shows how to export grid data to excel in advance Kendo UI using MVC WEB API and Entity Framework. 

Requirements

  • VS2010 and above
  • Fiddler/Postman for testing
  • SQL Server 2008 and above
Before going through this article ensure that you have a basic understanding of the MVC Architecture, ASP.NET Web API and jQuery.

Description

Let us start with creating a REST service using WEB API.

Just create a WEB API project in Visual Studio as shown in the following Figures 1 and 2:


                                                                  Figure 1

 
                                                                     Figure 2 

Creating a Model Class

Right-click on the model folder and create a class. In my case I named it Product.

Write the following code in the Product.cs model class.
  1. public class Product  
  2. {  
  3.     [Key]  
  4.     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]    
  5.     public int ProductID { getset; }  
  6.     [Required]  
  7.     public string ProductName { getset; }  
  8.     [Required]  
  9.     public string UnitPrice { getset; }  
  10. }  

Here I am using Entity Framework Code first technique so we need to create the context class.

Right-click on the model folder and create one more class. In my case I named it ProductContext.

Write the following code in ProductContext class.

  1. public class ProductContext:DbContext  
  2. {  
  3.     public ProductContext() : base("name=TestConnection") {  }  
  4.     public DbSet<Product> Products { getset; }  
  5.   
  6. }  
Scaffolding the WEB API Controller Class

Note:
 Before doing Scaffolding build your application once.

Right-click on Controller folder then select  Add, then  Controller and create a WEB API class as shown in 3 and 4.

 
                                                                     Figure 3
 
  
                                                         Figure 4 

The preceding procedure will scaffold the RESTfull service in the ProductsController.cs.

You will get some pre-defined HTTP GET, POST, PUT and DELETE requests/responses in the products Controller. Modify the code based on your application requirements. For this example I didn't modified the code. 

Now the REST services are created, it's time to create a Kendo UI Grid View to consume the services.

Before implementing the service in the Kendo UI once check that in Postman / Fiddler.

Using a Kendo Grid with remote binding

Create an HMTL page in your project, in my case I named it GridtoExcel.html.

Design in GridtoExcel.html

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" />  
  5.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" />  
  6.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.min.css" />  
  7.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.default.min.css" />  
  8.     <script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script>  
  9.     <script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>  
  10.     <script src="http://cdn.kendostatic.com/2014.3.1029/js/jszip.min.js"></script>  
  11.     <title></title>  
  12. </head>  
  13. <body>  
  14.     <div class="container" id="example">  
  15.         <div class="row">  
  16.   
  17.             <div id="test-grid" data-role="grid"  
  18.                  data-scrollable="true"  
  19.                  data-editable="false"  
  20.                  data-selectable="true"  
  21.                  data-toolbar="['excel']"  
  22.                  data-excel='{fileName: "ProductDetail.xlsx"}'  
  23.                  data-columns="[  
  24.   
  25.                        { 'field''ProductName','width':'100px' },  
  26.                     { 'field'' UnitPrice','width':'100px'},  
  27.                  ]"  
  28.                  data-pageable='true'  
  29.                  data-bind="source:products"  
  30.                      style="height: 300px"></div>  
  31.   
  32.         </div>  
  33.         </div></body>  
  34. </html>  

The Rest service End Point is: api/products

JavaScipt with MVVM Model

  1. <script>    
  2.      var viewModel = kendo.observable({    
  3.          isVisible: true,    
  4.              
  5.          products: new kendo.data.DataSource({    
  6.              schema: {    
  7.                  model: {    
  8.                      id: "ProductID",    
  9.                      fields: {    
  10.                          ProductName: { type: "string" },    
  11.                          UnitPrice: { type: "string" }    
  12.                      }    
  13.                  }    
  14.              },    
  15.              batch: true,    
  16.              transport: {    
  17.                  read: {    
  18.                      url: "api/Products",    
  19.                      dataType: "json"    
  20.                  },    
  21.                  parameterMap: function (options, operation) {    
  22.                      if (operation !== "read" && options.models) {    
  23.                          return { models: kendo.stringify(options.models) };    
  24.                      }    
  25.                  }    
  26.              }    
  27.          })    
  28.      });    
  29.      kendo.bind($("#example"), viewModel);    
  30.  </script>  

Result in a browser



Click on Export to Excel button to export the grid data into excel sheet.

Excel Sheet




Export Grid Data to Excel with filtering

Design in GridtoExcel.html

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" />  
  5.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" />  
  6.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.min.css" />  
  7.     <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.default.min.css" />  
  8.     <script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script>  
  9.     <script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>  
  10.     <script src="http://cdn.kendostatic.com/2014.3.1029/js/jszip.min.js"></script>  
  11.     <title></title>  
  12. </head>  
  13. <body>  
  14.     <div class="container" id="example">  
  15.         <div class="row">  
  16.   
  17.             <div id="test-grid" data-role="grid"  
  18.                  data-scrollable="true"  
  19.                  data-editable="false"  
  20.                  data-selectable="true"  
  21.                  data-toolbar="['excel']"  
  22.                  data-excel='{fileName: "ProductDetail.xlsx",filterable:"true"}'  
  23.                  data-columns="[  
  24.   
  25.                        { 'field': 'ProductName','width':'100px' },  
  26.                     { 'field': ' UnitPrice','width':'100px'},  
  27.                  ]"  
  28.                  data-pageable='true'  
  29.                  data-bind="source:products"  
  30.                      style="height: 300px"></div>  
  31.   
  32.         </div>  
  33.         </div>  
  34. </body>  
  35. </html>  
JavaScipt with MVVM Model
  1.  var viewModel = kendo.observable({    
  2.     isVisible: true,    
  3.         
  4.     products: new kendo.data.DataSource({    
  5.         schema: {    
  6.             model: {    
  7.                 id: "ProductID",    
  8.                 fields: {    
  9.                     ProductName: { type: "string" },    
  10.                     UnitPrice: { type: "string" }    
  11.                 }    
  12.             }    
  13.         },    
  14.         batch: true,    
  15.         transport: {    
  16.             read: {    
  17.                 url: "api/Products",    
  18.                 dataType: "json"    
  19.             },    
  20.                
  21.             parameterMap: function (options, operation) {    
  22.                 if (operation !== "read" && options.models) {    
  23.                     return { models: kendo.stringify(options.models) };    
  24.                 }    
  25.             }    
  26.         }    
  27.     })    
  28. });    
  29. kendo.bind($("#example"), viewModel);  
Result in Browser:
 
   
 
Excel Sheet:
 
  
 
 
 
 

Now, I am going to add more entries in the Grid with paging. 

Result in browser 
 


Entries in Pages 2
 



Data in excel sheet

 

From the above result you can observe one thing that the Mobile entry is missing in Excel sheet because of the paging in Grid.

To overcome this issue we need to use allPages property

Design in GridtoExcel.html

  1. <div class="container" id="example">  
  2.         <div class="row">  
  3.   
  4.             <div id="test-grid" data-role="grid"  
  5.                  data-scrollable="true"  
  6.                  data-editable="false"  
  7.                  data-selectable="true"  
  8.                  data-toolbar="['excel']"  
  9.                  data-excel='{fileName: "ProductDetail.xlsx",filterable:"true",allPages:"true"}'  
  10.                  data-columns="[  
  11.   
  12.                        { 'field': 'ProductName','width':'100px' },  
  13.                     { 'field': ' UnitPrice','width':'100px'},  
  14.                  ]"  
  15.                  data-pageable='true'  
  16.                  data-bind="source:products"  
  17.                  style="height: 300px"></div>  
  18.   
  19.            </div>  
  20.      </div>  
  21. </div>

JavaScipt with MVVM Model

  1. var viewModel = kendo.observable({    
  2.     isVisible: true,    
  3.     
  4.     products: new kendo.data.DataSource({    
  5.         schema: {    
  6.             model: {    
  7.                 id: "ProductID",    
  8.                 fields: {    
  9.                     ProductName: { type: "string" },    
  10.                     UnitPrice: { type: "string" }    
  11.                 }    
  12.             }    
  13.         },    
  14.         batch: true,    
  15.         pageSize:5,    
  16.         transport: {    
  17.             read: {    
  18.                 url: "api/Products",    
  19.                 dataType: "json"    
  20.             },    
  21.            
  22.             parameterMap: function (options, operation) {    
  23.                 if (operation !== "read" && options.models) {    
  24.                     return { models: kendo.stringify(options.models) };    
  25.                 }    
  26.             }    
  27.         }    
  28.     })    
  29. });    
  30. kendo.bind($("#example"), viewModel);   
Data in excel sheet
 


From the above result you can observe that we have got all the entries from Grid to Excel by using allPages Property.

Conclusion

We have seen how the new export capability in Kendo Grid is more powerful and flexible.

Thank you

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kendo UI框架提供了强大的Excel导出功能,通过Grid的saveAsExcel能方便地导出Grid中的数据,而且格式美观大方,使用起来也非常方便。但是在实际使用中不是很理想,主要有以下两个问题: 1. 导出的列数据是原始值 Kendo UI默认导出的是该列的value值,及查出来的值,有时候我们会用template渲染一下导出的列,比如将“Y”显示成“是”,把“N”显示成“否”。而Kendo UI导出的却是Y/N这种只有程序员看得懂的数据库标识,显然不是我们需要的,这种情况非常常见。 2. 不能灵活控制可导出的列 Kendo UIExcel导出主要看两点,一是该列(column)是field字段,而不是自定义的name;二是该列不是隐藏的(hidden:true)。这样我们无法灵活导出我们需要的列。 为了解决上面两个问题,我查看了Kendo UI的源代码,提取并改进了源代码。主要更改点及使用方法请看下面源代码。主要针对以上两点做了更改,只需要在grid定义columns时加上isExportexportTemplate即可: 在导出数据,先看该列有没有自定义exportTemplate(),没有则看Kendo UI自带的template(),再没有才会导出查出来的值。 判断是否导出该列不再看hidden属性,而是看列的isExport属性,如果为false则不导出,其它情况一律导出该列。 经过这两个更改,基本可以应对所有业务场景,可以方便快捷地开发了! 但要注意,Kendo UI自带的导出功能无法应用于导出大量数据,似乎是浏览器的jvm溢出了,建议超过5万条的数据导出还是老老实实写后台导出功能吧。。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值