vue3项目中导入Excel表格数据

实现代码:

import { Workbook } from "exceljs"
const { nodejs } = window
const buffer = await nodejs.readPublicFile('excel/xxxxxx表.xlsx')
const workbook = new Workbook()
await workbook.xlsx.load(buffer)
const dataSheet = workbook.getWorksheet('sheetName') // 'sheetName'为左下角的sheet名称
// 获取特定id的数据
const getSiteOriginalData = (id: string) => {
  const data: Array<(string | number)[]> = []
  dataSheet.eachRow(row => {
    if (row.getCell(1).text !== id) return
    const values = (Array.isArray(row.values) ? row.values : []).filter(value => value !== undefined) as (string | number)[]
    data.push(values)
  })
  return data
}

记录于2023-08-28