使用el-upload组件读取excel文件里的内容并通过xlsx插件转为json格式输出

	<el-upload
    class="upload-demo" ref="upload" accept=".xls,.xlsx"
    action="https://localhost:8081/posts/"
    :on-change="upload"
    :show-file-list="false"
    :auto-upload="false"
    >
    <el-button slot="trigger" size="large">导入excel</el-button>
    </el-upload>
    import xlsx from "xlsx"//安装并导入插件
 	upload (file, fileList) {
      const files = { 0: file.raw }// 取到File
      this.readExcel(files)
    },
    readExcel (files) { // 表格导入
      if (files.length <= 0) { // 如果没有文件名
        return false
      } else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {//文件格式校验
        this.$Message.error('上传格式不正确,请上传xls或者xlsx格式')
        return false
      }
      const fileReader = new FileReader()
      fileReader.onload = (files) => {
        try {
          const data = files.target.result
          const workbook = xlsx.read(data, {
            type: 'binary'
          })
          const wsname = workbook.SheetNames[0]// 取第一张表
          const ws = xlsx.utils.sheet_to_json(workbook.Sheets[wsname])// 生成json表格内容
          console.log(ws, 'ws是表格里的数据,且是json格式')
          this.menuData = ws//页面渲染
          this.$refs.upload.value = ''
          // return ws  也可以返回json
        } catch (e) {
          console.log(e)
          return false
        }
      }
      fileReader.readAsBinaryString(files[0])//以方式二进制读
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Element UI的el-upload组件上传Excel文件并预览可以分为以下几个步骤: 1. 安装Element UI和xlsx依赖。在命令行执行以下命令: ``` npm install element-ui xlsx --save ``` 2. 在Vue组件引入Element UI和xlsx。例如: ```javascript import { ElUpload, ElButton } from 'element-ui'; import XLSX from 'xlsx'; ``` 3. 在Vue组件使用el-upload组件创建文件上传并预览的功能。例如: ```html <template> <el-upload class="upload-demo" action="" :on-change="handleFileUpload" :before-upload="beforeUpload" :file-list="fileList" :auto-upload="false" :accept=".xlsx, .xls" :show-file-list="false"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传Excel文件</div> </el-upload> <table> <thead> <tr> <th v-for="header in headers" :key="header">{{ header }}</th> </tr> </thead> <tbody> <tr v-for="(row, index) in data" :key="index"> <td v-for="(value, key) in row" :key="key">{{ value }}</td> </tr> </tbody> </table> </template> ``` 4. 在Vue组件定义beforeUpload方法,用于限制只能上传Excel文件。例如: ```javascript beforeUpload(file) { const fileType = file.name.split('.').pop(); if (fileType !== 'xlsx' && fileType !== 'xls') { this.$message.error('只能上传Excel文件'); return false; } } ``` 5. 在Vue组件定义handleFileUpload方法,用于处理上传的Excel文件。例如: ```javascript handleFileUpload(file) { const reader = new FileReader(); reader.onload = (e) => { const data = e.target.result; const workbook = XLSX.read(data, { type: 'binary' }); const sheetName = workbook.SheetNames[0]; const sheet = workbook.Sheets[sheetName]; const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 }); this.headers = jsonData[0]; this.data = jsonData.slice(1); }; reader.readAsBinaryString(file.raw); } ``` 6. 在Vue组件定义fileList、headers和data变量,用于存储上传的Excel文件信息和解析后的数据。例如: ```javascript data() { return { fileList: [], headers: [], data: [] }; } ``` 7. 根据需要自定义样式和其他功能。例如: ```css .upload-demo { margin-top: 20px; width: 100%; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值