vue引用vue-office实现docx、excel、pdf等文件预览

vue-office

vue-office支持多种文件(docx、excel、pdf)预览的vue组件库,支持vue2/3,也支持非Vue框架的预览。

安装命令

#docx文档预览
npm install @vue-office/docx

#excel文档预览
npm install @vue-office/excel

#pdf文档预览
npm install @vue-office/pdf

安装完之后需要注意vue版本,如果是vue2.6版本或以下还需要额外安装 @vue/composition-api

npm install @vue/composition-api

文件预览
 

<template>
    <div>
        <button @click="FilePreviewDialogHandle('docx')">测试doc文件预览</button>
        <button @click="FilePreviewDialogHandle('pdf')">测试pdf文件预览</button>
        <button @click="FilePreviewDialogHandle('xlxs')">测试xlsx文件预览</button>
        <div>
            <div v-if="getFileType(fileUrl) == 'docx'">
              <vue-office-docx
                :src="fileUrl"
                style="height: 100vh;overflow: auto"
                @rendered="rendered"
                @error="errorHandler"
              />
            </div>
            <div v-if="getFileType(fileUrl) == 'xlsx'">
              <vue-office-excel
                :src="fileUrl"
                style="height: 100vh;overflow: auto"
                @rendered="rendered"
                @error="errorHandler"
              />
            </div>
            <div v-if="getFileType(fileUrl) == 'pdf'">
              <vue-office-pdf
                :src="fileUrl"
                style="height: 100vh;overflow: auto"
                @rendered="rendered"
                @error="errorHandler"
                :options="{
                  width: '100%',
                  overflow: 'auto'
                }"
              />
            </div>
        </div>
    </div>
</template>
<script>
// 引入VueOffice组件
import VueOfficeDocx from '@vue-office/docx';
import VueOfficeExcel from '@vue-office/excel';
import VueOfficePdf from '@vue-office/pdf';
// 引入相关样式
import '@vue-office/docx/lib/index.css';
import '@vue-office/excel/lib/index.css';
export default {
    components: {
        VueOfficeDocx,
        VueOfficeExcel,
        VueOfficePdf
    },
    data(){
        return:{
            fileUrl:'', // 文件url
            
        }
    },
    methods:{
        // 控制文件预览Dialog显示
        FilePreviewDialogHandle (type) {
          console.log(type);
          if (type === 'docx') {
            this.fileUrl = ref('../file/test.docx'); // 本地预览
          } else if (type === 'pdf') {
            this.fileUrl = 'http://static.shanhuxueyuan.com/test.pdf'; // 线上获取
          } else if (type === 'xlxs') {
            this.fileUrl = 'http://static.shanhuxueyuan.com/demo/excel.xlsx';
          } else if (type == null) {
            this.fileUrl = null;
          } else {
            this.fileUrl = null;
          }
           this.getFileType(this.fileUrl);
        },
        // 获取文件类型
        getFileType (fileUrl) {
          if (fileUrl !== '' && fileUrl != null && fileUrl !== undefined) {
            const fileType = fileUrl.split('.').pop().toLowerCase(); // 文件的扩展名(格式)
            console.log('type:', fileType);
            return fileType;
          } else {
            return 'docx';
          }
        },
        // 渲染结果
        rendered () {
          console.log('渲染完成');
        },

        errorHandler () {
          this.$notify.error('加载失败');
        },

    }
}
</script>

注意:目前vue-office不支持doc格式。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值