vue2实现excel文件预览

一、插件

通过xlsx插件解析excel数据,对解析后的html组件进行渲染展示。

html 复制代码
npm install xlsx

二、完整代码

html 复制代码
<template>
  <!-- excel文件预览 -->
  <div
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    class="panel-box pd-15"
  >
    <div class="file-pre">
      <el-tabs v-model="activeName" type="border-card" style="overflow:auto">
        <el-tab-pane v-for="(item,index) in excelSheet" :key="index" :label="item.name" :name="item.name" style="overflow:auto">
          <div class="table" v-html="item.innerHTML"></div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div class="tc mgt-20">
      <button class="btn-submit" @click="close()">关闭</button>
    </div>
  </div>
</template>

<script>
import { read, utils } from "xlsx";

export default {
  name:'excelFilePreview',
  data() {
    return {
      activeName:'',
      excelSheet:[],
    };
  },
  created() {
    const params = this.$commonUtil.decodeQuery(this.$route.query.info);
    this.previewFile(params);
  },
  methods: {
    previewFile(docId){
      this.excelSheet = [];
      const inParam = {
        DOC_ID: docId,
        STAFF_NAME: this.$store.getters.staffName,
        SYS_USER_CODE: this.$store.getters.systemUserCode
      };
      const loading = this.$commonUtil.loading.open();
      this.$downloadBuffer(this.mciApi.common.file.previewFile, {...inParam}).then(r => {
        loading.close();
        const data = new Uint8Array(r);
        const workbook = read(data, {
          type: 'array'
        });
        // 删除掉没有数据的sheet
        Object.values(workbook.Sheets).forEach((sheet, index) => {
          if (Object.keys(sheet).indexOf('!ref') === -1) {
            delete workbook.Sheets[workbook.SheetNames[index]];
          }
        });
        this.tableToHtml(workbook);
      }).catch((e) => {
        loading.close()
      })
    },
    tableToHtml(workbook) {
      const sheetList = workbook.SheetNames.filter((v) => v.indexOf('数据源') === -1);
      this.activeName = sheetList[0];
      sheetList.forEach((sheet) => {
        const worksheet = workbook.Sheets[sheet];
        if (worksheet) {
          const innerHTML = utils.sheet_to_html(worksheet);
          this.excelSheet.push({
            name: sheet,
            innerHTML: innerHTML
          });
        } else {
          this.excelSheet.push({
            name: sheet,
            innerHTML: '暂无数据',
          });
        }
      });
    },
    close(){
      this.$commonUtil.closeCurrentTagBackLast(this.$route,false);
    },
  }
};
</script>

<style lang="scss" scope>
.file-pre {
  padding: 20px;
  .table-html-wrap /deep/ table {
    border-right: 1px solid #e8eaec;
    border-bottom: 1px solid #e8eaec;
    border-collapse: collapse;
    margin: auto;
  }
  .table-html-wrap /deep/ table td {
    border-left: 1px solid #e8eaec;
    border-top: 1px solid #e8eaec;
    white-space: wrap;
    text-align: left;
    min-width: 100px;
    padding: 4px;
  }
  table {
    border-top: 1px solid #EBEEF5;
    border-left: 1px solid #EBEEF5;
    width: 100%;
    overflow: auto;
    tr {
      height: 44px;
    }
    tr:first-child{
      background: #f2f8ff;
    }
    td {
      min-width: 200px;
      max-width: 400px;
      padding: 4px 8px;
      border-right: 1px solid #EBEEF5;
      border-bottom: 1px solid #EBEEF5;
    }
  }
}
</style> 

注意:

$downloadBuffer :封装的post请求,请求中的 responseType 需要设置为 arraybuffer

三、预览效果

相关推荐
用户516816614584113 小时前
Vue Router 路由懒加载引发的生产页面白屏问题
vue.js·vue-router
前端缘梦13 小时前
Vue Keep-Alive 组件详解:优化性能与保留组件状态的终极指南
前端·vue.js·面试
Simon_He13 小时前
这次来点狠的:用 Vue 3 把 AI 的“碎片 Markdown”渲染得又快又稳(Monaco 实时更新 + Mermaid 渐进绘图)
前端·vue.js·markdown
王同学QaQ18 小时前
Vue3对接UE,通过MQTT完成通讯
javascript·vue.js
华仔啊19 小时前
基于 RuoYi-Vue 轻松实现单用户登录功能,亲测有效
java·vue.js·后端
葡萄城技术团队19 小时前
从100秒到10秒的性能优化,你真的掌握 Excel 的使用技巧了吗?
excel
艾小码19 小时前
告别Vue混入的坑!Composition API让我效率翻倍的3个秘密
前端·javascript·vue.js
Gracemark1 天前
高德地图-地图选择经纬度问题【使用输入提示-使用Autocomplete进行联想输入】(复盘)
vue.js
天下无贼2 天前
【手写组件】 Vue3 + Uniapp 手写一个高颜值日历组件(含跨月补全+今日高亮+选中状态)
前端·vue.js
洋葱头_2 天前
vue3项目不支持低版本的android,如何做兼容
前端·vue.js