gpt4 book ai didi

javascript - 使用 sheetJS 在单个工作表中的多个表

转载 作者:行者123 更新时间:2023-12-04 11:19:06 26 4
gpt4 key购买 nike

我成功使用了example from sheetJS's website ,像这样:

/* create new workbook */
var workbook = XLSX.utils.book_new();

/* convert table 'table1' to worksheet named "Sheet1" */
var ws1 = XLSX.utils.table_to_sheet(document.getElementById('table1'));
XLSX.utils.book_append_sheet(workbook, ws1, "Sheet1");

/* convert table 'table2' to worksheet named "Sheet2" */
var ws2 = XLSX.utils.table_to_sheet(document.getElementById('table2'));
XLSX.utils.book_append_sheet(workbook, ws2, "Sheet2");

/* workbook now has 2 worksheets */

是否可以将多个 html 表附加到单个工作表?它们具有相同的结构。我想它可能是这样的:
/* convert 'table1', 'table2', 'table3' to single sheet named "Sheet1" */
var ws1 = XLSX.utils.table_to_sheet(document.getElementById('table1'));
var ws2 = XLSX.utils.table_to_sheet(document.getElementById('table2'));
var ws3 = XLSX.utils.table_to_sheet(document.getElementById('table3'));
XLSX.utils.book_append_sheet(workbook, {ws1, ws2, ws3}, "Sheet1");

最佳答案

我从以下链接中找到了答案:
https://codepen.io/anon/pen/EebyzQ?editors=0010
它的作用是将工作表附加到一个工作表中,跳过第一个工作表的标题,然后将其导出为 Excel 工作表:

function convert(){
let tbl1 = document.getElementsByTagName("table")[0]
let tbl2 = document.getElementsByTagName("table")[1]

let worksheet_tmp1 = XLSX.utils.table_to_sheet(tbl1);
let worksheet_tmp2 = XLSX.utils.table_to_sheet(tbl2);

let a = XLSX.utils.sheet_to_json(worksheet_tmp1, { header: 1 })
let b = XLSX.utils.sheet_to_json(worksheet_tmp2, { header: 1 })

a = a.concat(['']).concat(b)

let worksheet = XLSX.utils.json_to_sheet(a, { skipHeader: true })

const new_workbook = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(new_workbook, worksheet, "worksheet")
XLSX.writeFile(new_workbook, 'tmp_file.xls')
}

关于javascript - 使用 sheetJS 在单个工作表中的多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51829907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com