使用 node xlsx 获取 xlsx文件数据

   <el-upload
              class="upload-demo"
              action="###"
              accept='.xlsx'
              :http-request="customUpload">
              <el-button size="small" type="primary">点击上传</el-button>
   </el-upload>
import xlsx from 'xlsx'
    async readAsDataBinaryString (file) {
      const render = new FileReader()
      if (!render) {
        return ''
      }
      return new Promise((resolve, reject) => {
        render.onload = (res) => { // 在onload 可以获得上传文件
          resolve(res.target.result)
        }
        render.readAsBinaryString(file)
      })
    },
    customUpload (content) {
      (async () => {
        if (!content.file) return
        const file = content.file
        const binaryString = await this.readAsDataBinaryString(file)
        const workbook = xlsx.read(binaryString, {
          type: 'binary',
          codepage: 936
        })
        const sheetNames = workbook.SheetNames
        const sheet = workbook.Sheets[sheetNames[0]]
        const data = xlsx.utils.sheet_to_json(sheet)
        const filterData = data.slice(1, data.length)
        if (filterData.length === 0) return this.$message.error('文件为空!')
        _.each(filterData, async item => {
          const params = {
            menuPath: 'CASH_VALUE-CASH_VALUE',
            compareVersion: 0,
            data: item
          }
          await addVersion(params)
          await sleep(600)
        })
        this.$message.success('导入成功!')
      })()
    }

  

上一篇:爬虫入门(三):使用requests爬取搜狗搜索页面


下一篇:Vue3 ElementPlus 动态表单点击按钮新增/删除行