Export Table data into excel/csv

edited April 2023 in Web Development

It's simple here we have a simple way from javascript that can export table data into csv or excel as shown below,

firstly import the script link in js

<script type="text/javascript" src="/TableDataExport.js"></script>

then we have a function to which we have to pass the table id and type of export as below.

 <script>
  function ExportToExcel(type, fn, dl,tble_id,filename) {
       var elt = document.getElementById(tble_id);
       var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" });
       return dl ?
         XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }):
         XLSX.writeFile(wb, fn || (filename+'.' + (type || 'xlsx')));
    }
</script>

After the button click or this function is called then file will be downloaded.

<script type="text/javascript" src="/TableDataExport.js"></script>
 <script>
  function ExportToExcel(type, fn, dl,tble_id,filename) {
       var elt = document.getElementById(tble_id);
       var wb = XLSX.utils.table_to_book(elt, { sheet: "sheet1" });
       return dl ?
         XLSX.write(wb, { bookType: type, bookSST: true, type: 'base64' }):
         XLSX.writeFile(wb, fn || (filename+'.' + (type || 'xlsx')));
    }
</script>

Thank you...

Tagged:
Sign In or Register to comment.