How to fix export HTML table data to Excel using JavaScript not working properly in Chrome browser?

Spread the love

Sometimes, we want to fix export HTML table data to Excel using JavaScript not working properly in Chrome browser.

In this article, we’ll look at how to fix export HTML table data to Excel using JavaScript not working properly in Chrome browser.

How to fix export HTML table data to Excel using JavaScript not working properly in Chrome browser?

To fix export HTML table data to Excel using JavaScript not working properly in Chrome browser, we use the tableToExcel.js library.

For instance, we write

<script src="https://cdn.jsdelivr.net/gh/linways/table-to-excel@v1.0.4/dist/tableToExcel.js"></script>

<button id="btnExport" onclick="exportReportToExcel(this)">
  EXPORT REPORT
</button>

to add the script and a button to export the table when clicked.

We set the onclick attribute to exportReportToExcel(this) which exports the table data.

Then we write

function exportReportToExcel() {
  const [table] = document.getElementsByTagName("table");
  TableToExcel.convert(table, {
    name: `export.xlsx`,
    sheet: {
      name: "Sheet 1",
    },
  });
}

to define the exportReportToExcel function.

In it, we get table with getElementsByTagName and destructuring.

And then we call TableToExcel.convert to export the table as export.xlsx and put the data in Sheet 1.

Conclusion

To fix export HTML table data to Excel using JavaScript not working properly in Chrome browser, we use the tableToExcel.js library.

Leave a Reply

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