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

基于vue-office实现docx、xlsx、pdf文件的在线预览

概述

在做项目的时候会遇到docxxlsxpdf等文件的在线预览需求,实现此需求可以有多种解决方式,本文基于vue-office实现纯前端的文件预览。

效果

如下图,分别为docxxlsxpdf三种类型的文件在线加载后的效果。你也可以访问官方预览网址查看效果。
image.png

实现

1. 添加依赖

package.json文件中添加如下依赖,并通过命令npm i安装依赖。

{...,"dependencies": {"@vue-office/docx": "1.6.0","@vue-office/excel": "1.6.0","@vue-office/pdf": "1.6.0"},
}

2. 引入依赖

vue文件中引入依赖,示例代码如下:

//引入相关样式
import '@vue-office/docx/lib/index.css'
import '@vue-office/excel/lib/index.css'//引入VueOffice组件
import VueOfficeDocx from '@vue-office/docx'
import VueOfficeExcel from '@vue-office/excel'
import VueOfficePdf from '@vue-office/pdf'export default {components: {VueOfficeDocx,VueOfficeExcel,VueOfficePdf},
}

3. 使用组件

<template><div class="layout-file"><div class="layout-file-title">{{docx}}</div><vue-office-docx :src="docx" @rendered="rendered"/></div><div class="layout-file"><div class="layout-file-title">{{excel}}</div><vue-office-excel :src="excel" @rendered="rendered"/></div><div class="layout-file"><div class="layout-file-title">{{pdf}}</div><vue-office-pdf style="height: 100%;" :src="pdf" @rendered="rendered"/></div>
</template>

完整的实现代码如下:

<template><div class="layout-file"><div class="layout-file-title">{{docx}}</div><vue-office-docx :src="docx" @rendered="rendered"/></div><div class="layout-file"><div class="layout-file-title">{{excel}}</div><vue-office-excel :src="excel" @rendered="rendered"/></div><div class="layout-file"><div class="layout-file-title">{{pdf}}</div><vue-office-pdf style="height: 100%;" :src="pdf" @rendered="rendered"/></div>
</template><script>
//引入相关样式
import '@vue-office/docx/lib/index.css'
import '@vue-office/excel/lib/index.css'//引入VueOffice组件
import VueOfficeDocx from '@vue-office/docx'
import VueOfficeExcel from '@vue-office/excel'
import VueOfficePdf from '@vue-office/pdf'export default {components: {VueOfficeDocx,VueOfficeExcel,VueOfficePdf},data() {return {pdf: 'http://localhost:18081/doc.pdf',excel: 'http://localhost:18081/doc.xlsx',docx: 'http://localhost:18081/doc.docx',ppt: 'http://localhost:18081/doc.pptx'}},methods: {rendered() {console.log('rendered')}}
}
</script><style lang="scss" scoped>
.layout-file {width: calc(50% - 1rem);height: 50%;float: left;margin-top: 0.8rem;margin-right: 0.8rem;position: relative;border: 1px solid #f00;border-radius: 0.3rem;&:nth-child(1), &:nth-child(2) {margin-top: 0;}.layout-file-title {position: absolute;top: 0.5rem;right: 1.5rem;background-color: rgba(255,0,0,0.8);padding: 0.1rem 0.4rem;z-index: 99;color: #fff;border-radius: 0.3rem;font-size: 12px;}
}
</style>
http://www.lryc.cn/news/310163.html

相关文章:

  • 数据库系统架构与DBMS功能探微:现代信息时代数据管理的关键
  • 51单片机-(中断系统)
  • c++之旅——第三弹
  • GO—函数
  • [WebUI Forge]ForgeUI的安装与使用 | 相比较于Auto1111 webui 6G显存速度提升60-75%
  • 高刷显示器 - HKC VG253KM
  • javascript实现的星座查询
  • 全国青少年软件编程(Python)等级考试试卷(一级) 测试卷2021年12月
  • 昇腾ACL应用开发之硬件编解码dvpp
  • MFC 模态对话框退出机制的探究
  • Android13 framework添加关机接口
  • 如何使用ArcGIS Pro为栅格图添加坐标信息
  • FDM打印机学习
  • C++进阶-- map和set
  • AI-数学-高中-33概率-事件的关系与运算
  • 数据结构:链队
  • 十四、计算机视觉-形态学梯度
  • 3月03日,每日信息差
  • leetcode 简单
  • 服务器硬件基础知识全解析
  • python毕设选题 - 大数据商城人流数据分析与可视化 - python 大数据分析
  • vmware网络负载均衡方式
  • Docker基础教程 - 2 Docker安装
  • Android 多桌面图标启动, 爬坑点击打开不同页面
  • 2024-3-1-网络编程作业
  • pytorch基础2-数据集与归一化
  • Python测试框架pytest介绍用法
  • AI对话系统app开源
  • SpringBoot+aop实现主从数据库的读写分离
  • 胎神游戏集第二期