当前位置: 首页 > news >正文

vue2实现excel文件预览

一、插件

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

npm install xlsx

二、完整代码 

<template><!-- excel文件预览 --><divelement-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'});// 删除掉没有数据的sheetObject.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

三、预览效果

 

http://www.lryc.cn/news/515418.html

相关文章:

  • STM32 和 ESP32
  • R语言中的时间序列分析·
  • QML学习(六) anchors锚点和坐标,以及anchors锚点的使用
  • BFS广度优先搜索详解
  • vue项目利用webpack进行优化案例
  • 如何单独安装 MATLAB 工具箱
  • 组网实训实现
  • openbmc sdk09.03 适配(一)
  • SQL使用存储过程
  • C语言----函数、指针、数组
  • 基于Java的敬老院管理系统的设计和实现【源码+文档+部署讲解】
  • 12306分流抢票软件 bypass v1.16.43 绿色版(春节自动抢票工具)
  • 【数据仓库】hadoop3.3.6 安装配置
  • 小试牛刀-SpringBoot集成SOL链
  • 批量插入报错: No value specified for parameter
  • VSCode设置ctrl或alt+mouse(left)跳转
  • Crosslink-NX应用连载(12):如何复用特殊功能管脚
  • ‘元素.style.样式名‘获取不到样式,应该使用Window.getComputedStyle()获取正真的样式
  • 双目视觉:reprojectImageTo3D函数
  • Arduino Uno简介与使用方法
  • 深入了解 StarRocks 表类型:解锁高效数据分析的密码
  • L27.【LeetCode笔记】2 的幂(五种解法)
  • Pentaho Kettle迁移至Oracle的空字符串和NULL的问题处理,大坑!
  • 「Mac畅玩鸿蒙与硬件50」UI互动应用篇27 - 水果掉落小游戏
  • 2.C语言基础:语句、表达式、注释与标准库简介
  • Python 基于 opencv 的人脸识别监控打卡系统(源码+部署)
  • Maven的依赖管理
  • 数据结构考前一天
  • 获取 Astro Bot AI 语音来增强您的游戏体验!
  • html5开发,js 在元素div id=img1的最前面插入一个图片