Install dependencies

XLSX –> Excel processing library Excel import and export are dependent on JS-XLSx to achieve –> on the basis of this dependency package encapsulated for the export of data Export2Excel

Create a file to UploadExcel

Create the component in the SRC path –> UploadExcel/index.vue

<template>
  <div>
    <! -- :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" -->
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :http-request="upload"
    >
      <img v-if="value" :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <el-progress v-show="isShowProgress" type="circle" :percentage="percentage" :width="178" class="progress" />
    </el-upload>
  </div>
</template>

<script>
// instantiate the cos object
const COS = require('cos-js-sdk-v5')
// Fill in key and ID (key) in Tencent Cloud COS
const cos = new COS({
  SecretId: 'AKID3wZw2sRjlW0D9oCJAk23nH2WjgMkrpXy'.// Identity ID
  SecretKey: 'B4g15sOdcJkunPkob6KIzPXP0LPBhwua' // Identity key
})
export default {
  name: 'UploadImg'.props: {
    value: {
      type: String.default: ' '}},data() {
    return {
      isShowProgress: false.percentage: 0}},methods: {
    upload(res) {
      if (res.file) {
        this.isShowProgress = true
        cos.putObject({
          Bucket: 'rlzy-one-1306404223'./* Buckets */
          Region: 'ap-beijing'./* Specifies the location of the bucket. */ is a mandatory field
          Key: res.file.name, /* File name */
          StorageClass: 'STANDARD'.// Upload mode, standard mode
          Body: res.file, // Upload the file object
          onProgress: (progressData) = > {
            console.log(JSON.stringify(progressData))
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              this.isShowProgress = false}}},(err, data) = > {
          console.log(err || data)
          if (data.statusCode === 200) {
            const urlImg = `https:${data.Location}`
            this.$emit('input', urlImg)
          }
        })
      }
    }
    // handleAvatarSuccess(res, file) {
    // this.imageUrl = URL.createObjectURL(file.raw)
    // },
    // beforeAvatarUpload(file) {
    // const isPNG = file.type === 'image/png'
    // const isLt2M = file.size / 1024 / 1024 < 2
    // if (! isPNG) {
    // this.$message.error(' Upload avatar only in PNG format! ')
    / /}
    // if (! isLt2M) {
    // this.$message.error(' The size of the uploaded avatar cannot exceed 2MB! ')
    / /}
    // return isPNG && isLt2M
    // }}}</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.progress {
  position: absolute;
  top: 1px;
  left: 1px
}
</style>
Copy the code

Three. Use this component, flexible application of communication events on this component

/ / pseudo code
import uploadExcel from 'xxx/UploadExcel'
// Use the component
<upload-excel :on-success="handleSuccess" :before-upload="handleUpload" />
Copy the code
  • BeforeUpload: You can condition the data before uploading. The — parameter is the currently passed file
beforeUpload(file) {
 const isLt1M = file.size / 1024 / 1024 < 1

 if (isLt1M) {
   return true
 }

 this.$message({
   message: 'Please do not upload files larger than 1m in size.'.type: 'warning'
 })
 return false
}
Copy the code
  • OnSuccess: Parses the successfully triggered callback function to return the table header and contents
handleSuccess({ results, header }) {
  this.tableData = results  / / content
  this.tableHeader = header  / / headers
}
Copy the code