vue-office 官网
一、安装
1) 分开安装
| # docx 文档预览组件 |
| npm install @vue-office/docx vue-demi@0.14.6 |
| |
| # excel 文档预览组件 |
| npm install @vue-office/excel vue-demi@0.14.6 |
| |
| # pdf 文档预览组件 |
| npm install @vue-office/pdf vue-demi@0.14.6 |
复制
vue-demi@0.14.6; 其中 @0.14.6 为版本号,可以不加,默认下载最新版。
如果是 vue2.6 版本或以下还需要额外安装 @vue/composition-api
| npm install @vue/composition-api |
复制
2) 全部安装
| npm install --save @vue-office/docx @vue-office/excel @vue-office/pdf vue-demi@0.14.6 |
复制
二、使用
1) 组件封装
① 创建文件 @/components/FileView/index.vue
我这里预览 PDF 时用的 PDFJS ,因为功能更全面。你们不用的话可以去掉,或者自行比较一下。
| <script setup lang='ts'> |
| import VueOfficeDocx from '@vue-office/docx' |
| import VueOfficeExcel from '@vue-office/excel' |
| import VueOfficePdf from '@vue-office/pdf' |
| import PDF from "@/components/FileView/PDF.vue" |
| |
| import '@vue-office/docx/lib/index.css' |
| import '@vue-office/excel/lib/index.css' |
| |
| const { data } = defineProps<{ data: { |
| type: string, |
| src: string |
| } }>() |
| |
| let loading = ref(false) |
| let options={ |
| xls: data.type === "xls" ? true : false , |
| minColLength: 0, |
| minRowLength: 0, |
| widthOffset: 10, |
| heightOffset: 10, |
| beforeTransformData: (workbookData) => {return workbookData}, |
| transformData: (workbookData) => {return workbookData}, |
| } |
| |
| const renderedHandler = () => { |
| loading.value = false; |
| console.log("渲染完成") |
| } |
| const errorHandler = () => { |
| loading.value = false; |
| console.log("渲染失败") |
| } |
| let loadingList = ["docx","xlsx","xls"] |
| if (loadingList.some(a=> a === data.type)) { |
| loading.value = true; |
| } |
| </script> |
| |
| <template> |
| |
| <div v-loading="loading" class="box"> |
| <vue-office-docx |
| v-if="data.type === 'docx'" |
| :src="data.src" |
| style="width: 100%;height: 100%" |
| @rendered="renderedHandler" |
| @error="errorHandler" |
| /> |
| <vue-office-excel |
| v-else-if="data.type === 'xlsx' || data.type === 'xls'" |
| :src="data.src" |
| :options="options" |
| style="height: 72vh;width: 100%" |
| @rendered="renderedHandler" |
| @error="errorHandler" |
| /> |
| |
| |
| |
| |
| |
| |
| |
| <PDF :url="data.src" v-else-if="data.type === 'pdf'" style="height: 72vh;width: 100%" /> |
| <div v-else-if="data.type ==='jpg' || data.type === 'png' || data.type === 'jpeg'" class="h-100 w-100 con"> |
| <img :src="data.src"> |
| </div> |
| <div v-else> |
| <el-empty description="暂不支持该文件格式" /> |
| </div> |
| </div> |
| |
| </template> |
| |
| <style scoped lang='scss'> |
| .box { |
| width: 100%; |
| height: 75vh; |
| height: 100%; |
| background-color: #fff; |
| box-sizing: border-box; |
| overflow: auto; |
| |
| } |
| .con{ |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| img{ |
| max-width: 100%; |
| max-height: 100%; |
| } |
| } |
| </style> |
复制
② 创建文件 @/components/FileView/PDF.vue
此文件主要是通过 PDFJS 预览 PDf ,没用到的可以不用管。
详细使用可以参考我的另一篇文章:记录一下使用PDFJS遇到的坑 。
| <script setup lang="ts"> |
| import { onMounted, ref } from 'vue'; |
| interface Props { |
| url: string; |
| } |
| const props = defineProps<Props>(); |
| const pdfUrl = ref(''); |
| const fileUrl = '/fileView/web/viewer.html?file='; |
| |
| onMounted(() => { |
| |
| |
| |
| pdfUrl.value = fileUrl + encodeURIComponent(props.url); |
| }); |
| |
| </script> |
| |
| <template> |
| <div class="container"> |
| <iframe :src="pdfUrl" width="100%" height="100%"></iframe> |
| </div> |
| </template> |
| |
| <style scoped lang="scss"> |
| .container { |
| width: 100%; |
| height: 100%; |
| } |
| </style> |
| |
复制
2) 调用组件
| import fileViewModal from "@/components/FileView/index.vue" |
| |
| <fileViewModal |
| :data="{ |
| src: 'https://xxx', |
| type: 'pdf' |
| }" |
| /> |
复制
三、非Vue框架文件预览
1) 安装
| # docx 文件预览库 |
| npm i @js-preview/docx |
| |
| # excel 文件预览库 |
| npm i @js-preview/excel |
| |
| # pdf 文件预览库 |
| npm i @js-preview/pdf |
复制
全部安装:
| npm install @js-preview/docx @js-preview/excel @js-preview/pdf |
复制
2) 预览
① docx 文件预览
| import jsPreviewDocx from "@js-preview/docx"; |
| import '@js-preview/docx/lib/index.css' |
| |
| |
| const myDocxPreviewer = jsPreviewDocx.init(document.getElementById('docx')); |
| |
| |
| myDocxPreviewer.preview('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.docx').then(res=>{ |
| console.log('预览完成'); |
| }).catch(e=>{ |
| console.log('预览失败', e); |
| }) |
复制
② excel 文件预览
| import jsPreviewExcel from "@js-preview/excel"; |
| import '@js-preview/excel/lib/index.css'; |
| |
| const myExcelPreviewer = jsPreviewExcel.init(document.getElementById('excel')); |
| myExcelPreviewer.preview('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.xlsx').then(res=>{ |
| console.log('预览完成'); |
| }).catch(e=>{ |
| console.log('预览失败', e); |
| }) |
复制
③ pdf 文件预览
| import jsPreviewPdf from "@js-preview/pdf"; |
| |
| const myPdfPreviewer = jsPreviewPdf.init(document.getElementById('pdf'), { |
| onError: (e)=>{ |
| console.log('发生错误', e) |
| }, |
| onRendered: ()=>{ |
| console.log('渲染完成') |
| } |
| }); |
| myPdfPreviewer.preview('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf'); |
复制
四、注意
| <script> |
| fetch('你的API文件地址', { |
| method: 'post' |
| }).then(res=>{ |
| |
| res.arrayBuffer().then(res=>{ |
| data.src = res |
| }) |
| }) |
| </script> |
复制