Datatables

Adding Excel Export in Datatables

2 min read

Here is the complete code with all the JavaScript files needed for the Excel, PDF, Copy, and Print buttons in the datatables.

<html>
<head>
<title>Export to Excel and PDF with DataTables</title>
<link href=”https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css” rel=”stylesheet” type=”text/css”>
<link href=”https://cdn.datatables.net/buttons/1.7.1/css/buttons.dataTables.min.css” rel=”stylesheet” type=”text/css”>
</head>
<body>
<table id=”example” class=”display nowrap” style=”width:100%”>
<!- Your table data goes here –>
</table>

<script src=”https://code.jquery.com/jquery-3.5.1.js”></script><script src=”https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js”></script><script src=”https://cdn.datatables.net/buttons/1.7.1/js/dataTables.buttons.min.js”></script><script src=”https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js”></script><script src=”https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js”></script><script src=”https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js”></script><script src=”https://cdn.datatables.net/buttons/1.7.1/js/buttons.html5.min.js”></script><script src=”https://cdn.datatables.net/buttons/1.7.1/js/buttons.print.min.js”></script>
<script> $(document).ready(function() { $(‘#example’).DataTable({ dom: ‘Bfrtip’, buttons: [ ‘excel’, ‘pdf’, ‘copy’, ‘print’ ] }); }); </script>
</body>
</html>

In the code above, we added some additional JavaScript files for the Buttons plugin, JSZip (which is needed to create an Excel file), pdfmake (which is needed to create a PDF file), and the print button. Good luck!


Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

× Chat