2024/04/19——VUE开发EXCEL上传

本文介绍了如何在Vue项目中使用HTML模板和XLSX库实现文件上传功能,重点是处理Excel文件并将其转换为JSON数据。
摘要由CSDN通过智能技术生成

VUE开发EXCEL上传


HTML

<template>
  <div class="table">     
    <div>
      <el-upload                
        class="upload-demo"                
        ref="upload"                
        action=""                
        :auto-upload="false"                
        :file-list="fileList"                
        :on-change="handleChange"                
        multiple                
        :show-file-list="false"                
      >              
      <el-button type="text">点击上传</el-button>                         
      </el-upload>
    </div>
  </div>
  </template>
<script>
// echarts
import XLSX from 'xlsx';
export default {
  data() {
    return {    
      fileList:[],            
		  file:""
    };
  },
  methods: {
    handleChange(file, fileList) {
      this.fileList = [fileList[fileList.length - 1]]; // 只能上传一个Excel,重复上传会覆盖之前的        
      this.file = file.raw;
      let reader = new FileReader()
      let _this = this
      reader.readAsArrayBuffer(this.file)
      reader.onload = function () {
        let buffer = reader.result
        let bytes = new Uint8Array(buffer)
        let length = bytes.byteLength
        let binary = ''
        for (let i = 0; i < length; i++) {
          binary += String.fromCharCode(bytes[i])
        }
        let XLSX = require('xlsx')
        let wb = XLSX.read(binary, {
          type: 'binary'
        })
        let outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
        console.log(outdata)
        //这个里面的this,不是当前dom,上面有函数
      }
    },
  },
};
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值