Others

Export Each HTML Table to a Separate Excel Worksheet with JavaScript

Adam DavisAdam Davis Nov 22, 2026 868 views

Question details

Export multiple HTML tables so that each table appears on its own Excel worksheet.

Export Each HTML Table to a Separate Excel Worksheet with JavaScript
Product
Microsoft Excel
Device & OS
not provided
Scenario
A JavaScript export creates an Excel workbook from multiple HTML tables.
Observed behavior
The workbook has multiple worksheets, but all tables appear on the first worksheet.
Before you start

Identify the HTML tables you want to export and check which JavaScript library your current export uses. The example below uses SheetJS and assumes the tables are already present in the browser DOM.

Solution 1Recommended

Create a worksheet from each HTML table with SheetJS

Use this approach to build a new XLSX workbook with one worksheet per HTML table.

The example converts each table into a worksheet cell grid rather than combining all tables on the first sheet. It does not create a native Excel Table object, and HTML styling or other advanced features may not be retained.

1
Install the library

In your JavaScript project, install SheetJS with `npm install xlsx`. Use a browser bundler or build tool that supports the import shown below.

2
Add an export button

Add a button such as `<button id="export">Export</button>` to the page containing the HTML tables.

3
Convert and append each table

In your browser JavaScript, add: import * as XLSX from 'xlsx'; document.getElementById('export').addEventListener('click', () => { const tables = document.querySelectorAll('table'); if (!tables.length) return; const workbook = XLSX.utils.book_new(); tables.forEach((table, index) => { const worksheet = XLSX.utils.table_to_sheet(table); XLSX.utils.book_append_sheet(workbook, worksheet, `Table ${index + 1}`); }); XLSX.writeFile(workbook, 'tables.xlsx'); });

4
Check the workbook

Click Export, open `tables.xlsx` in Excel, and select each worksheet tab to confirm that it contains the corresponding table.

Create a worksheet from each HTML table with SheetJS
Export only selected tables: Replace `document.querySelectorAll('table')` with a more specific selector, such as `document.querySelectorAll('.export-table')`, if the page contains tables you do not want to export.
Manual spreadsheet alternative

Place HTML tables on separate sheets in WPS Spreadsheet

For a manual alternative to changing the JavaScript export, copy each HTML table into its own worksheet in WPS Spreadsheet and save the workbook as an Excel file.

  1. 1. Create a workbook: Open WPS Spreadsheet and choose New to create a blank workbook.
  2. 2. Copy the first table: In your browser, select the first HTML table and copy it. Return to WPS Spreadsheet, select cell A1, and paste.
  3. 3. Add the remaining tables: Use the plus button beside the worksheet tabs to add a sheet for each remaining table, then paste each table into cell A1 of its own sheet.
  4. 4. Save as an Excel workbook: Choose Menu > Save As, select the XLSX format, and save the workbook. Check the pasted cells and formatting on each sheet.
Keep each HTML table on a separate worksheetOpen and save Microsoft Excel XLSX filesReview and edit worksheet cells in a familiar spreadsheet interface
microsoft office alternative - wps office

Frequently Asked Questions

Why are all my tables appearing on the first worksheet?

The export code may be writing every table to the same worksheet. Create a separate worksheet for each table and append it to the workbook inside the loop.

How can I export only certain HTML tables?

Give the desired tables a class such as `export-table`, then select them with `document.querySelectorAll('.export-table')` before creating the worksheets.

Does `table_to_sheet` create an Excel Table object?

No. It converts the HTML table into worksheet cells. If you need a native Excel Table object with features such as filters and table styles, use a workbook library that supports creating those objects.

Will the export preserve all HTML formatting?

Not necessarily. Open the generated XLSX file and check its formatting and merged cells. More complex HTML features may require additional processing or a different export library.