jQuery Datatable Copy Excel PDF CSV Print Button

This article gives an overview of how to use jQuery datatable in MVC to implement copy, Excel, Pdf, CSV and Print button in jQuery Ajax. I will use the  jQuery datatable plugin in this demo application. Here is the code.
 
This is the final output of application.
 
jQuery Datatable Copy Excel PDF CSV Print Button
 
This example requires the following basic understanding
  • JQuery datatable
  • Ajax
  • Bootstrap
  • Entity framework
  • MVC
Step 1
 
There are multiple ways to create a new project in Visual Studio 2019. When you first open Visual Studio, the start window appears, and from there, you can choose to create a new project.
 
jQuery Datatable Copy Excel PDF CSV Print Button
 
If the Visual Studio development environment is already open, you can create a new project by choosing File > New > Project on the menu bar or by clicking the New Project button on the toolbar.
 
jQuery Datatable Copy Excel PDF CSV Print Button
 
Step 2
 
On the Create a new project page, a list of your recently selected templates appears on the left. The templates are sorted by most recently used.
 
If you're not selecting from the recently used templates, you can filter all available project templates by Language (for example, C#), Platform (for example, Windows or Azure), and Project type (for example, Desktop or Web). You can also enter search text into the search box to further filter the templates, for example, asp.net.
 
jQuery Datatable Copy Excel PDF CSV Print Button
 
Select a template and then click Next.
 
Step 3
 
The Configure your new project page has options to name your project (and solution), choose a disk location, and select a Framework version (if applicable to the template you chose).
 
jQuery Datatable Copy Excel PDF CSV Print Button
 
Click Create to create the new project.
 
Step 4
 
In create a new ASP.NET Web Application window choose MVC template click on create. Visual studio will generate MVC Web Application project for you.
 
jQuery Datatable Copy Excel PDF CSV Print Button
 
Step 5
 
In Visual Studio tools go to NuGet Package Manager and select Manage NuGet Packages for solution.
 
jQuery Datatable Copy Excel PDF CSV Print Button
 
Install the following package for this application,
  • Bootstrap latest version
  • Jquery latest version
  • Datatables latest version
  • Entity framework latest version
Step 6
 
Right click on Models Folder choose new item “Add” class Employee.
  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3.   
  4. namespace JQueryDatatableCopyExcelPdfCSVPrintButtonMvc_Demo.Models  
  5. {  
  6.     public class Employee  
  7.     {  
  8.         [Key]  
  9.         public int Id { getset; }  
  10.   
  11.         [Required]  
  12.         [StringLength(100)]  
  13.         public string Name { getset; }  
  14.   
  15.         [Required]  
  16.         [StringLength(100)]  
  17.         public string Position { getset; }  
  18.   
  19.         [Required]  
  20.         [StringLength(100)]  
  21.         public string Office { getset; }  
  22.   
  23.         [Required]  
  24.         public int Age { getset; }  
  25.   
  26.         [Required]  
  27.         [DataType(DataType.Date)]  
  28.         public DateTime StartDate { getset; }  
  29.   
  30.         [Required]  
  31.         [DataType(DataType.Currency)]  
  32.         public decimal Salary { getset; }  
  33.     }  
  34. }  
Step 7
 
Create Data Folder and add class ApplicationDbContext for database communication.
  1. using JQueryDatatableCopyExcelPdfCSVPrintButtonMvc_Demo.Models;  
  2. using System.Data.Entity;  
  3.   
  4. namespace JQueryDatatableCopyExcelPdfCSVPrintButtonMvc_Demo.Data  
  5. {  
  6.     public class ApplicationDbContext : DbContext  
  7.     {  
  8.         public ApplicationDbContext() : base("EmployeeDB")  
  9.         {  
  10.   
  11.         }  
  12.   
  13.         public DbSet<Employee> Employees { getset; }  
  14.     }  
  15. }  
  16. © 2020 GitHub, Inc.  
Step 8
  1. Enable-migrations
  2. Add-Migration InitialModel
  3. Update-database
Step 9
 
Add bootstrap, jQuery and datatable plugin in _Layout.cshtm
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  6.     <title>@ViewBag.Title</title>  
  7.     @Styles.Render("~/Content/css")  
  8.     @Scripts.Render("~/bundles/modernizr")  
  9.     <link href="~/Content/fontawesome-all.min.css" rel="stylesheet" />  
  10.     <link href="~/Content/DataTables/css/dataTables.bootstrap4.min.css" rel="stylesheet" />  
  11.     <link href="~/Content/DataTables/css/buttons.bootstrap4.min.css" rel="stylesheet" />  
  12. </head>  
  13. <body>  
  14.     <div class="container py-4">  
  15.         @RenderBody()  
  16.     </div>  
  17.     @Scripts.Render("~/bundles/jquery")  
  18.     @Scripts.Render("~/bundles/bootstrap")  
  19.     <script src="~/Scripts/jquery-3.5.1.min.js"></script>  
  20.     <script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>  
  21.     <script src="~/Scripts/DataTables/dataTables.bootstrap4.min.js"></script>  
  22.     <script src="https://cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js"></script>  
  23.     <script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.bootstrap4.min.js"></script>  
  24.     <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>  
  25.     <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>  
  26.     <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>  
  27.     <script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.html5.min.js"></script>  
  28.     <script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.print.min.js"></script>  
  29.     <script src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.colVis.min.js"></script>  
  30.     @RenderSection("scripts", required: false)  
  31. </body>  
  32. </html>   
Step 10
 
You already have HomeController in Controllers folder. If you don’t have it add HomeController in Controllers folder and write the following code.
  1. using JQueryDatatableCopyExcelPdfCSVPrintButtonMvc_Demo.Data;  
  2. using System.Linq;  
  3. using System.Web.Mvc;  
  4.   
  5. namespace JQueryDatatableCopyExcelPdfCSVPrintButtonMvc_Demo.Controllers  
  6. {  
  7.     public class HomeController : Controller  
  8.     {  
  9.         private readonly ApplicationDbContext db = new ApplicationDbContext();  
  10.         public ActionResult Index()  
  11.         {  
  12.             return View();  
  13.         }  
  14.   
  15.         public ActionResult GetData()  
  16.         {  
  17.             var employee = db.Employees.ToList();  
  18.             return Json(new { data = employee }, JsonRequestBehavior.AllowGet);  
  19.         }  
  20.     }  
  21. }   
Step 11
 
In views folder Under Home folder in Index.cshtml view add the following code.
  1. @{  
  2.     ViewBag.Title = "Home";  
  3. }  
  4. <h3 class="text-center text-uppercase">List of employees</h3>  
  5.   
  6. <table id="example" class="table table-bordered" style="width:100%;">  
  7.     <thead class="thead-dark">  
  8.         <tr>  
  9.             <th>Id</th>  
  10.             <th>Name</th>  
  11.             <th>Position</th>  
  12.             <th>Office</th>  
  13.             <th>Age</th>  
  14.             <th>Start Date</th>  
  15.             <th>Salary</th>  
  16.         </tr>  
  17.     </thead>  
  18. </table>  
  19.   
  20. @section scripts{  
  21.      
  22.     <script type="text/javascript">  
  23.         $(document).ready(function () {  
  24.             $('#example').DataTable({  
  25.                 "ajax": {  
  26.                     "url"'/Home/GetData',  
  27.                     "type"'GET',  
  28.                     "dataType"'json',  
  29.                 },  
  30.                 "columns": [  
  31.                     { 'data''Id' },  
  32.                     { 'data''Name' },  
  33.                     { 'data''Position' },  
  34.                     { 'data''Office' },  
  35.                     { 'data''Age' },  
  36.                     {  
  37.                         'data''StartDate',  
  38.                         'render': function (jsonDate) {  
  39.                             var date = new Date(parseInt(jsonDate.substr(6)));  
  40.                             var month = ("0" + (date.getMonth() + 1)).slice(-2);  
  41.                             return ("0" + date.getDate()).slice(-2) + '-' + month + '-' + date.getFullYear();  
  42.                         }  
  43.                     },  
  44.                     {  
  45.                         'data''Salary',  
  46.                         'render': function (salary) {  
  47.                             return '$' + salary;  
  48.                         }  
  49.                     }  
  50.                 ],  
  51.                 "dom"'Bfrtip',  
  52.                 "buttons": [  
  53.                     {  
  54.                         extend: 'copy',  
  55.                         className: 'btn btn-dark rounded-0',  
  56.                         text: '<i class="far fa-copy"></i> Copy'  
  57.                     },  
  58.                     {  
  59.                         extend: 'excel',  
  60.                         className: 'btn btn-dark rounded-0',  
  61.                         text: '<i class="far fa-file-excel"></i> Excel'  
  62.                     },  
  63.                     {  
  64.                         extend: 'pdf',  
  65.                         className: 'btn btn-dark rounded-0',  
  66.                         text: '<i class="far fa-file-pdf"></i> Pdf'  
  67.                     },  
  68.                     {  
  69.                         extend: 'csv',  
  70.                         className: 'btn btn-dark rounded-0',  
  71.                         text: '<i class="fas fa-file-csv"></i> CSV'  
  72.                     },  
  73.                     {  
  74.                         extend: 'print',  
  75.                         className: 'btn btn-dark rounded-0',  
  76.                         text: '<i class="fas fa-print"></i> Print'  
  77.                     }  
  78.                 ]  
  79.             });  
  80.         });  
  81.     </script>  
  82. }   
Step 12
 
Now it’s time to build and run your application >> ctrl+F5
jQuery Datatable Copy Excel PDF CSV Print Button