Let's add the following lines of code to parse data receied from Web API to Kendo Grid.
Now, let's add Script section.
- to receive data from ASP.NET Core Web API
- to parse the data into Kendo Grid.
- Add Export to Excel Buton on Kendo Grid.
- <script>
- $(document).ready(function () {
- $("#Grid").kendoGrid({
- toolbar: ["excel"],
- excel: {
- fileName: "Demo Excel From Kendo.xlsx",
- filterable: true,
- allPages: true
- },
- dataSource: {
- type: "json",
- transport: {
- contentType: "application/json; charset=utf-8",
- type: "GET",
- dataType: "json",
- read: "/api/StudentAPI/GetAllStudents",
- },
- pageSize: 5,
- schema: {
- model: {
- fields: {
- RegNo: {
- type: "string"
- },
- Name: {
- type: "string"
- },
- Address: {
- type: "string"
- },
- PhoneNo: {
- type: "string"
- },
- admissionDate: {
- type: "date"
- }
- }
- }
- },
- },
- filterable: true,
- sortable: true,
- groupable: true,
- columnMenu: true,
- reorderable: true,
- resizable: true,
- pageable: {
- refresh: true,
- pageSizes: true,
- buttonCount: 5
- },
- columns: [{
- field: "regNo",
- title: "Regd No",
- filterable: { multi: true, search: true }
- }, {
- field: "name",
- title: "Student Name",
- filterable: { multi: true, search: true }
- }, {
- field: "address",
- title: "Address",
- filterable: { multi: true, search: true }
- }, {
- field: "phoneNo",
- title: "Phone No",
- filterable: { multi: true, search: true }
- }, {
- field: "admissionDate",
- title: "Admission Date",
- format: "{0:MM-dd-yyyy}",
- filterable: { multi: true, search: true }
- }]
- });
- });
- </script>
Now, run the application and navigate to the corresponding page to see output.
Toolbar
["excel"] will add an "Export to Excel" button on the grid, as shown in figure above. Now, let's export the above data to Excel. The exported Excel will look like the following.
You can also group the data by Columns, Reorder, FIlter by Columns and Show/Hide Columns as per your requirement. When you export the Excel, the data will be exported as per the selection.
Sample Grouped and Ungrouped Data before and after Exporting data is shown in given figure.
Ungrouped data
Grouped data
Note that Excel Export relies on a JavaScript library called jszip. Let's reference this jszip JavaScript library.
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.4.0/jszip.min.js"></script>
Kendo UI provides awesome and easiest way to configure the export button.
- $(document).ready(function () {
- $("#Grid").kendoGrid({
- toolbar: ["excel"],
- excel: {
- fileName: "Demo Excel From Kendo.xsls",
- filterable: true,
- allPages: true
- },
- .............
- });
- });
What if I want to place a Export to Excel button outside the Grid?
Well, in this case, you can complete this task easily. You just need to write a few lines of JavScript to do this.
Kendo UI has saveAsExcel() methd that serves to export data into Excel easily. Let's assume we have a button with labeled with text Export to Excel just outside of the Kendo Grid.
Now, we will say browser to download the Excel when the button is clicked.
- <script>
- $("#btnExportToExcel").kendoButton({
-
- click: function()
- {
- $("#Grid").data("kendoGrid").saveAsExcel()
- }
- });
-
-
-
- $("#Grid").kendoGrid({
- toolbar:[“excel”],
- excel:{
- filename:”Data Export to Excel.xslx”,
- filterable:true,
- allPages:false
- },
- ............
-
- });
-
- </script>
We learned how to
- Convert API Data into Kendo UI Grid
- Add some functionality on kendo UI Grid like Filtering, pagination, sortable, ColumnMenu, etc
- Export Data to Excel.
- Export data to Excel creating custom button outside Kendo Grid.