DEV Community

Cover image for Simpan table html ke excel dengan JQUERY
Eko Priyanto
Eko Priyanto

Posted on • Edited on

Simpan table html ke excel dengan JQUERY

<script src="https://cdn.jsdelivr.net/gh/linways/%3Ca%20href="https://dev.to/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="780c191a141d550c17551d001b1d14380e495648564c">[email protected]/dist/tableToExcel.js"></script>
<button id="btnExport" onclick="exportReportToExcel(this)">EXPORT REPORT</button>

function exportReportToExcel() {
  let table = document.getElementsByTagName("table"); // you can use document.getElementById('tableId') as well by providing id to the table tag
  TableToExcel.convert(table[0], { // html code may contain multiple tables so here we are refering to 1st table tag
    name: `export.xlsx`, // fileName you could use any name
    sheet: {
      name: 'Sheet 1' // sheetName
    }
  });
}
<button id="btnExport">EXPORT REPORT</button>

$(document).ready(function(){
    $("#btnExport").click(function() {
        let table = document.getElementsByTagName("table");
        TableToExcel.convert(table[0], { // html code may contain multiple tables so here we are refering to 1st table tag
           name: `export.xlsx`, // fileName you could use any name
           sheet: {
              name: 'Sheet 1' // sheetName
           }
        });
    });
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)