【发布时间】:2021-06-23 16:19:04
【问题描述】:
我正在尝试使用 Exceljs 以表格格式呈现 Excel 文件,但在打开文件之前我收到此警告:
We found a problem with some content in ’test.xlsx’. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes.
如果我单击“是”,它会“恢复”文件并且一切都很完美,但我总是在打开之前收到该警告。
只有在我执行多个选项卡时才会发生这种情况,因为单个选项卡可以正常工作。
import Excel from 'exceljs'
const tabs = {
'FIRST TAB': [
{ URL: 'https://google.com', FOO: 10 },
{ URL: 'https://apple.com', FOO: 12.5 }
],
'SECOND TAB': [
{ URL: 'https://google.com', FOO: 10 },
{ URL: 'https://apple.com', FOO: 22.5 }
]
}
;(async () => {
const workbook = new Excel.Workbook()
let worksheet = {}
for (const [label, tab] of Object.entries(tabs)) {
worksheet = workbook.addWorksheet(label)
worksheet.state = 'visible'
const columns = Object.keys(tab[0]).map((items) => ({
name: items,
filterButton: true
}))
const rows = tab.map((entry) => Object.values(entry))
workbook.getWorksheet(label).addTable({
name: label,
ref: 'A1',
headerRow: true,
columns,
rows
})
}
// Write to excel
await workbook.xlsx.writeFile(`test.xlsx`)
})()
【问题讨论】:
标签: javascript node.js exceljs