excel预览

**后端还回的是文件流
前端调用接口需要加 responseType: ‘arraybuffer’,
**

<template>
  <div class="container">
    <el-dialog
        title="预览"
        :visible.sync="dialogVisible"
        @open="see"
        lock-scroll
        :close-on-click-modal="false"
        :before-close="handleClose"
        width="60%"
    >
      <div v-loading="loading"
           class="excel-view-container">
        <div
            id="excelView"
            v-html="excelView"
        ></div>
      </div>
      <span slot="footer">
    <el-button type="primary" @click="sure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as XLSX from 'xlsx'
import {pbocbdmsReportView, } from "../../utils/api";

export default {
  name: "ExcelRead",
  props: {
    id: {type: String, require: true},
    dialogVisible: {type: Boolean, require: true},
    isshow: {type: Function, require: true},
    sheetName: {type: String, require: true}
  },
  data() {
    return {
      workbook: "",
      loading: false,
      excelView: ''
    }
  },
  created() {
  },
  methods: {
    handleClose() {
      this.isshow(false)
      this.excelView = ''
    },
    sure() {
      this.isshow(false)
    },
    see() {
      this.flieRead()
    },
    readWorkbookFromRemoteFile(callback) {
      this.loading = true
      pbocbdmsReportView({id: this.id}).then(res => {
        const data = new Uint8Array(res)
        this.workbook = XLSX.read(data, {type: 'array'});
        if (callback) callback(this.workbook);
        this.loading = false
      })
    },
    readWorkbook(workbook) {
      let worksheet = null
      if (this.sheetName) {
        worksheet = workbook.Sheets[this.sheetName]
      } else {
        worksheet = workbook.Sheets[workbook.SheetNames[0]];// 这里我们只读取第一张sheet
      }
      this.excelView = XLSX.utils.sheet_to_html(worksheet);
      this.$nextTick(function () { // DOM加载完毕后执行,解决HTMLConnection有内容但是length为0问题。
        this.setStyle4ExcelHtml()
      })
    },
    flieRead() {
      this.loadRemoteFile()
    },
    loadRemoteFile() {
      this.readWorkbookFromRemoteFile(() => {
        this.readWorkbook(this.workbook);
      });
    },
    // 设置Excel转成HTML后的样式
    setStyle4ExcelHtml() {
      const excelViewDOM = document.getElementById('excelView')
      if (excelViewDOM) {
        const excelViewTDNodes = excelViewDOM.getElementsByTagName('td')// 获取的是HTMLConnection
        if (excelViewTDNodes) {
          const excelViewTDArr = Array.prototype.slice.call(excelViewTDNodes)
          for (const i in excelViewTDArr) {
            const id = excelViewTDArr[i].id// 默认生成的id格式为sjs-A1、sjs-A2......
            console.log(id)
            if (id) {
              const idNum = id.replace(/[^0-9]/ig, '')// 提取id中的数字,即行号
              if (idNum && (idNum === '1' || idNum === 1)) { // 第一行标题行
                excelViewTDArr[i].classList.add('class4Title')
              }
              // if (idNum && (idNum === '2' || idNum === 2)) { // 第二行表头行
              //   excelViewTDArr[i].classList.add('class4TableTh')
              // }
            }
          }
        }
      }
    }
  }
}
</script>

<style scoped>
/deep/ table {
  max-width: 100% !important;
  border-collapse: collapse !important;
  border-spacing: 0 !important;
  text-align: left !important;

  border: 0px !important;
}

/deep/ table tr td {
  padding-left: 8px;
  border: 1px solid gray !important;
}

/**整体样式 */
/deep/ .excel-view-container {
  background-color: #FFFFFF;
}

/**标题样式 */
/deep/ .class4Title {
  font-size: 22px !important;
  font-weight: bold !important;
  padding: 10px !important;
  color: black !important;
  text-align: center !important;
  /*background-color: gainsboro !important;*/
}

/**表格表头样式 */
/deep/ .class4TableTh {
  /* font-size: 14px !important; */
  font-weight: bold !important;
  padding: 5px !important;
  color: black !important;
  text-align: center !important;
  background-color: gainsboro !important;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱技术的大仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值