vue2 预览 word、excel、pdf

vue2 预览 word、excel、pdf

依赖的库

npm install @vue-office/docx
npm install @vue-office/excel
npm install @vue-office/pdf​

npm install @vue/composition-api (上面组件需要依赖的库)

代码实现

 <template>
  <div class="Office-Preview" v-loading="loading" element-loading-text="文件加载中..." >
    <VueOfficeDocx v-if="fileType == 'docx'" :src="fileUrl" @rendered="rendered" @error="onError"></VueOfficeDocx>
    <VueOfficeExcel v-if="fileType == 'excel'" :src="fileUrl" @rendered="rendered" @error="onError"></VueOfficeExcel>
    <VueOfficePdf v-if="fileType == 'pdf'" :src="fileUrl" @rendered="rendered" @error="onError"></VueOfficePdf>


    <el-empty v-if="fileType == 'errType'" image="" description="文件格式不规范" :image-size="300"></el-empty>
    <el-empty v-if="fileType == 'loadErr'" image="" description="文件加载失败" :image-size="300"></el-empty>
  </div>
</template>

<script>
//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
//引入VueOfficeExcel组件
import VueOfficeExcel from '@vue-office/excel'
//引入VueOfficeExcel相关样式
import '@vue-office/excel/lib/index.css'
//引入VueOfficePdf组件
import VueOfficePdf from '@vue-office/pdf'

export default {
  name: 'Office-Preview',
  props: {
    fileUrl: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      fileType: '',
      loading: true,
    }
  },
  components: {
    VueOfficeDocx,
    VueOfficeExcel,
    VueOfficePdf,
  },
  methods: {
    init() {
      if (!this.fileUrl) console.error('文件为空')
      var fileName = this.fileUrl.split('/').pop()
      var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1)
      if (fileExtension == 'doc' || fileExtension == 'docx') {
        this.fileType = 'docx'
      } else if (fileExtension == 'xls' || fileExtension == 'xlsx') {
        this.fileType = 'excel'
      } else if (fileExtension == 'pdf') {
        this.fileType = 'pdf'
      } else if (fileExtension == 'ppt' || fileExtension == 'pptx') {
        this.fileType = 'ppt'
      } else {
        this.fileType = 'errType'
        this.loading = false
      }
    },
    rendered() {
      console.log('渲染完成')
      this.loading = false
    },
    onError() {
      console.error('加载戳错')
      this.loading = false
      this.fileType = 'loadErr'
    },
  },
  created() {
    this.init()
  },
}
</script>

<style lang="scss" scoped>
.Office-Preview {
  overflow-y: scroll;
  height: 100%;
}
</style>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值