Search code examples
javascripthtmlcssexport-to-excel

How can i export payroll using html table with format to excel file?


I'm making a payroll generator system and i don't know how to export the designed payroll html file to a excel file? Can someone help me what i have to do or to use?


Solution

  • You can use the awesome plugin that call Datatables , the plugin allows us to export the HTML table data into Excel,PDF ,TEXT. Thats easily configurable.

    Or you can try to use this simple function

    function exportPdf(elem) {
      //select your table first
      const table = document.getElementById("table");
      const html = table.outerHTML;
      const url = 'data:application/vnd.ms-excel,' + escape(html); // Set your html table into url 
      elem.setAttribute("href", url);
      elem.setAttribute("target", "__blank");
      elem.setAttribute("download", "export.xls"); // Choose the file name
      return false;
    }
    

    and call the function to a link

    <a onclick="exportPdf(this)">Export to excel</a>