【问题标题】:Exceljs: 'We found a problem with some content in ’filename.xlsx’.'Exceljs:“我们发现 'filename.xlsx' 中的某些内容存在问题。”
【发布时间】: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


    【解决方案1】:

    问题是由表名中的空格引起的。

    修复它的一种方法是用下划线替换空格,这实际上是 Excel 在“修复”文件时所做的。

        workbook.getWorksheet(label).addTable({
          name: label.replace(' ', '_'),
          ref: 'A1',
          headerRow: true,
          columns,
          rows
        })
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 2020-06-28
      • 2019-12-08
      • 2020-07-15
      • 1970-01-01
      相关资源
      最近更新 更多