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

vue 在线预览word和excel

yarn add @vue-office/excel @vue-office/docx

 

<template><div><vue-office-docx:src="docx"style="height: 100%; margin: 0; padding: 0"@rendered="rendered"/></div>
</template><script>
//引入VueOfficeDocx组件
import VueOfficeDocx from "@vue-office/docx";
//引入相关样式
import "@vue-office/docx/lib/index.css";export default {components: {VueOfficeDocx,},props: {docx: {type: String,default:"a.docx", //设置文档网络地址,可以是相对地址},},data() {return {};},methods: {rendered() {console.log("渲染完成");},},
};
</script>

 

<template><vue-office-excel:src="excel"@rendered="renderedHandler"@error="errorHandler"style="height: calc(100vh - 90px)"/>
</template><script>
//引入VueOfficeExcel组件
import VueOfficeExcel from "@vue-office/excel";
//引入相关样式
import "@vue-office/excel/lib/index.css";export default {components: {VueOfficeExcel,},props: {excel: {type: String,default:"a.xlsx", //设置文档地址},},data() {return {};},methods: {renderedHandler() {console.log("渲染完成");},errorHandler() {console.log("渲染失败");},},
};
</script>

也可以浏览器选择文件后直接预览,不需要文件是在线的

<template><div><div v-if="extension === 'docx' || extension === 'doc'"><vue-office-docx :src="docx" style="height: 500px; margin: 0; padding: 0" /></div><div v-if="extension === 'xlsx' || extension === 'xls'"><vue-office-excel :src="excel" style="height: 500px" /></div><div v-if="extension === 'pdf'"><iframe v-if="pdfSrc" :src="pdfSrc" width="100%" height="500"></iframe></div></div>
</template>
<script lang="ts" name="cl-file-viewer" setup>
import { ref, watch } from "vue";
import VueOfficeExcel from "@vue-office/excel";
import VueOfficeDocx from "@vue-office/docx";
import "@vue-office/excel/lib/index.css";
import "@vue-office/docx/lib/index.css";const props = defineProps({fileType: {type: String,default: "file"}, // 文件类型file: {type: Object,default: null} //文件流 或者文件地址
});// 获取文件扩展名
const extension = ref("");
const docx = ref("");
const excel = ref("");
const pdfSrc:any = ref(null);const readFile = async () => {// 获取选中的文件//@ts-ignoreconst file:any = props.fileType === "file" ? props.file : getFileStream(props.file);if (!file) {return;}// 获取文件扩展名extension.value = file.name.split(".").pop();// 根据文件扩展名进行处理switch (extension.value) {case "docx":case "doc":// 读取Word文件readWordFile(file);break;case "xlsx":case "xls":// 读取Excel文件readExcelFile(file);break;case "pdf":// 读取PDF文件readPdfFile(file);break;default:// 不支持的文件类型alert("Unsupported file type");}
};
const readWordFile = (file: any) => {docx.value = URL.createObjectURL(file);
};const readExcelFile = (file: any) => {excel.value = URL.createObjectURL(file);
};const readPdfFile = async (file: any) => {if (file) {// 判断传入的 file 参数是否为字符串类型if (props.file instanceof String) {// 如果是字符串类型,则将其赋值给 pdfSrc.valuepdfSrc.value = props.file;} else {// 如果不是字符串类型,则使用 URL.createObjectURL 方法创建一个指向该文件的 URL,并将其赋值给 pdfSrc.valuepdfSrc.value = URL.createObjectURL(file);}}
};// url地址转发为文件流
const getFileStream = (url: string) => {return new Promise((resolve, reject) => {// 创建一个XMLHttpRequest对象const xhr = new XMLHttpRequest();// 设置请求方法为GET,并传入请求的URLxhr.open("GET", url);// 设置响应类型为blob,以便能够处理二进制数据xhr.responseType = "blob";// 当请求加载完成时,调用resolve方法并将响应数据作为参数传入xhr.onload = () => resolve(xhr.response);// 当请求发生错误时,调用reject方法并将错误信息作为参数传入xhr.onerror = (err) => reject(err);});
};// 初始化
watch(() => props.file,(newValue, oldValue) => {if (newValue && newValue != oldValue) {nextTick(() => {readFile();});}},{ immediate: true }
);
</script>

参考链接

https://blog.csdn.net/qq_42038623/article/details/131600935

人工智能学习网站

https://chat.xutongbao.top

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

相关文章:

  • 物联网智能项目
  • 828华为云征文|Flexus云服务器X:Python安装的极致便捷之旅
  • Midjourney中秋特典-12张图附魔咒
  • 编写程序,从键盘输入若干整数,将其保存入一个数组中。利用Arravs进行排序,然后查找出第3大的整数
  • VSCode 离线安装中文语言包
  • vue3补充
  • 关于Chrome浏览器没有网络,而其他浏览器正常这一问题的解决方法
  • 人工智能辅助汽车造型设计
  • 专家访谈:心脑血管名医蔡英丽医生的医学智慧
  • Ubuntu 22.04 源码下载的几种方法
  • floodfill+DFS(1)
  • 小程序开发设计-第一个小程序:注册小程序开发账号②
  • C++基础面试题 | C++中的构造函数可以是虚函数吗? C++中的析构函数一定要是虚函数吗?
  • Leetcode—1184. 公交站间的距离【简单】
  • 【python数据处理】保存网页
  • 智能体趋势:未来科技的核心驱动力
  • 学习笔记 韩顺平 零基础30天学会Java(2024.9.16)
  • python selenium网页操作
  • pytorch使用技巧
  • 从用户数据到区块链:Facebook如何利用去中心化技术
  • Elasticsearch之bool查询
  • IntelliJ IDEA 创建 Java 项目指南
  • 一起学Java(13)-[日志篇]教你分析SLF4J和Log4j2源码,掌握SLF4J与Log4j2桥接集成原理
  • 深入Redis:核心的缓存
  • 集群聊天服务器项目【C++】项目介绍和环境搭建
  • c++ #include <memory> 智能指针介绍
  • 32.递归、搜索、回溯之floodfill算法
  • Vue3.5+ 响应式 Props 解构
  • k8s中的认证授权
  • Leetcode 3291. Minimum Number of Valid Strings to Form Target I