最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create more than one XLSX sheet from HTML table in js-xlsx (sheetjs)? - Stack Overflow

programmeradmin0浏览0评论

I am using sheetjs to convert some of my table data into xlsx format (and download later).

However, how can I add more than one sheet into the xlsx?

The current code is:

var elt = document.getElementById('table_instance');
var wb = XLSX.utils.table_to_book(elt, {sheet:"Sheet 1"});

What I'm looking for is:

var elt = document.getElementById('table_instance');
var elt2 = document.getElementById('table_instance2');
var wb = XLSX.utils.table_to_book(
    [elt, {sheet:"Sheet 1"}
    [elt2, {sheet:"Sheet 2"}
);

Thanks

I am using sheetjs to convert some of my table data into xlsx format (and download later).

However, how can I add more than one sheet into the xlsx?

The current code is:

var elt = document.getElementById('table_instance');
var wb = XLSX.utils.table_to_book(elt, {sheet:"Sheet 1"});

What I'm looking for is:

var elt = document.getElementById('table_instance');
var elt2 = document.getElementById('table_instance2');
var wb = XLSX.utils.table_to_book(
    [elt, {sheet:"Sheet 1"}
    [elt2, {sheet:"Sheet 2"}
);

Thanks

Share Improve this question edited Jun 29, 2021 at 9:34 Robin Mackenzie 19.3k7 gold badges40 silver badges59 bronze badges asked Jun 29, 2021 at 8:55 AGamePlayerAGamePlayer 7,73221 gold badges73 silver badges147 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Step: 1 Create two separate sheets

var elt = document.getElementById('table_instance');
var elt2 = document.getElementById('table_instance2');

var ws = XLSX.utils.table_to_sheet(elt);
var ws2 = XLSX.utils.table_to_sheet(elt2);

Step: 2 Create an empty workbook

var wb = XLSX.utils.book_new();

Step: 3 Append the sheets to the workbook

XLSX.utils.book_append_sheet(wb, ws, "Sheet 1");
XLSX.utils.book_append_sheet(wb, ws2, "Sheet 2");

Working fiddle

There is a table_to_sheet utility function that returns a sheet object that you can insert into a workbook.

The example below shows this with a brand new workbook, with two sheets from two different tables:

// html
const tbl1 = document.getElementById("test1");
const tbl2 = document.getElementById("test2");

// new workbook - could be some existing book as well
let wb = XLSX.utils.book_new();

// table_to_sheet for different sheets from html table
let ws1 = XLSX.utils.table_to_sheet(tbl1);
let ws2 = XLSX.utils.table_to_sheet(tbl2);
// ... etc

// add sheets to workbook
// sheet names are your choice
XLSX.utils.book_append_sheet(wb, ws1, "Sheet1");
XLSX.utils.book_append_sheet(wb, ws2, "Sheet2");

// test persisted in sheet objects
const test1 = XLSX.utils.sheet_to_json(wb.Sheets["Sheet1"], {header: 1});
const test2 = XLSX.utils.sheet_to_json(wb.Sheets["Sheet2"], {header: 1});
console.log(`Sheet1: ${JSON.stringify(test1)}`);
console.log(`Sheet2: ${JSON.stringify(test2)}`);
<script src="https://cdnjs.cloudflare./ajax/libs/xlsx/0.17.0/xlsx.min.js"></script>
<table id="test1">
<tr><td>a1</td><td>b1</td><td>c1</td></tr>
<tr><td>d1</td><td>e1</td><td>f1</td></tr>
<tr><td>g1</td><td>h1</td><td>i1</td></tr>
</table>

<table id="test2">
<tr><td>a2</td><td>b2</td><td>c2</td></tr>
<tr><td>d2</td><td>e2</td><td>f2</td></tr>
<tr><td>g2</td><td>h2</td><td>i2</td></tr>
</table>

发布评论

评论列表(0)

  1. 暂无评论