vue 前端读取Excel文件并解析

前端读取Excel文件并解析

平时项目中对于Excel的导入解析是很常见的功能,一般都是放在后端执行;但是也有特殊的情况,偶尔也有要求说前端执行解析,判空,校验等,最后组装成后端接口想要的数据结构。

前端如何解释Excel呢

因为我使用插件执行的 you know

  • 安装插件

    npm install xlsx@0.14.1 // 0.14.1 是我使用的版本

  • 还有个nanoid

    npm i nanoid

此处我没有使用安装的这个 而是使用 自定的代码如下:

复制代码
// index.js
const createId =  () => {
  return (
    Number(Math.random().toString().substr(2, 7) + Date.now()).toString(36) +
    Date.now()
  )
}
export {
  createId as default,
  createId
}
  • 安装已经完成了 当然 依然 you know

    import XLSX from 'xlsx'
    import nanoid from 'xxxx/xxx/index'

  • 报一丝 差点忘记了 template

    <el-upload class="upload" action="" :auto-upload="false" :show-file-list="false" :multiple="false" :on-change="(file, fileList) => importTemp(file, fileList)"> <el-button v-permission="'are you ok'" type="primary" size="small" plain class="flex" >you know the name of the custom button</el-button> </el-upload>
  • finally

    methods: {
    importTemp(file, fileList) {
    const fileReader = new FileReader()
    fileReader.onload = ev => {
    try {
    const data = ev.target.result
    const workbook = XLSX.read(data, {
    type: 'binary'
    })
    const sheet = Object.keys(workbook.Sheets)[1] // 我是用的是第二个
    const json = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]) //
    const worksheet = workbook.Sheets[sheet]
    const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 })
    if (!jsonData.length) return
    const headers = jsonData[0]
    if (!headers.length) return
    const colorParam = headers.find(i => /xxx/.test(i))
    const transtypeParam = headers.find(i => /xxx/.test(i))
    const operationTypeParam = headers.find(i => /xxx/.test(i))
    const systemCodeParam = headers.find(i => /xxx/.test(i))
    const platCodeParam = headers.find(i => /xxx/.test(i))
    const truckingBillNoParam = headers.find(i => /xxx/.test(i))
    const delivyPlanTypeParam = headers.find(i => /xxx/.test(i))
    const targetObj = {}
    const nameMap = { // 定义必填的字段
    systemCode: systemCodeParam,
    platCode: platCodeParam,
    operationType: operationTypeParam,
    truckingBillNo: truckingBillNoParam,
    transType: transtypeParam,
    vehicleNumber: '车牌号',
    carColor: colorParam,
    driverName: '司机姓名',
    idcard: '司机身份证号',
    driverPhone: '司机手机号',
    clientCompanyCode: 'xxx',
    clientCompanyName: 'xxx名称',
    // messageBatchNo: 'xxx批次号',
    // detailsCounts: '明细条数',
    billId: 'xxx',
    billDependId: 'xxx',
    factoryBillId: 'xxx',
    delivyPlanType: delivyPlanTypeParam
    }
    for (let [idx, el] of json.entries()) {
    let tempObj = {
    }
    for (const key of Object.keys(nameMap)) {
    const value = el[nameMap[key]]
    if (!value && value !== 0) {
    this.message.warning(`第{idx + 2}行,字段: ${nameMap[key]} 为必填值`)
    return
    }
    tempObj[key] = value
    }
    tempObj = {
    ...tempObj,
    queueId: el['车辆排队号']
    }
    if (targetObj[tempObj.truckingBillNo]) {
    targetObj[tempObj.truckingBillNo].push(tempObj)
    } else {
    targetObj[tempObj.truckingBillNo] = [tempObj]
    }
    }
    const targetList = Object.keys(targetObj).map((key, i) => {
    const mainitem = targetObj[key][0]
    const target = {}
    Object.keys(mainitem).map(key => {
    if (!['qqq', 'xxxx', 'ssss', 'wwww'].includes(key)) {
    const val = (mainitem[key] + '').replace(/\s/gi, '')
    target[key] = ['null', 'undefined'].includes(val) ? '' : val // 判空下
    }
    })
    target.details = targetObj[key].map(item => {
    return {
    billId: item.qqq,
    billDependId: item.xxxx,
    factoryBillId: item.ssss,
    delivyPlanType: item.wwww
    }
    })
    target.detailsCounts = target.details.length
    target.messageBatchNo = nanoid() + i
    return target
    })

    复制代码
            // 后台请求接口
            import({ // 你自己的哦
              importDataList: targetList
            }).then(res => {
              const { msg, status } = res
              if (status) {
                this.$message.success('发送成功')
              } else {
                this.$message.error(msg ?? '操作失败')
              }
            })
          } catch (e) {
            console.log(e, 'error')
          }
        }
        fileReader.readAsBinaryString(file.raw)
      }

    }

  • catch

    双人行也有我师焉:哎呦不错哦

相关推荐
code_YuJun1 小时前
corepack 作用
前端
千寻girling1 小时前
Koa.js 教程 | 一份不可多得的 Node.js 的 Web 框架 Koa.js 教程
前端·后端·面试
全栈前端老曹1 小时前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈
code_YuJun1 小时前
pnpm-workspace.yaml
前端
天才熊猫君1 小时前
“破案”笔记:iframe动态加载内容后,打印功能为何失灵?
前端
五月君_1 小时前
炸裂!Claude Opus 4.6 与 GPT-5.3 同日发布:前端人的“自动驾驶“时刻到了?
前端·gpt
Mr Xu_1 小时前
前端开发中CSS代码的优化与复用:从公共样式提取到CSS变量的最佳实践
前端·css
鹏北海-RemHusband2 小时前
从零到一:基于 micro-app 的企业级微前端模板完整实现指南
前端·微服务·架构
LYFlied2 小时前
AI大时代下前端跨端解决方案的现状与演进路径
前端·人工智能
光影少年2 小时前
AI 前端 / 高级前端
前端·人工智能·状态模式