vue项目前端预览pdf、docx、xls

office online

微软office平台提供的文档预览服务
https://view.officeapps.live.com/op/view.aspx?src+文档地址
文档地址必须是域名80端口且公开访问
缺点由于不可抗力概率访问失败

私人文档预览云服务

XDOC(免费)
officeweb365

pdf

  1. pdf支持网页直接打开预览
  2. vue-pdf 文档
npm i -S vue-pdf

      <pdf :src="fileUrl" @num-pages="pageCount=$event" :page="currentPage"></pdf>
      <!-- 分页根据需求使用 -->
      <div class="page-view" v-if="pageCount > 1">
        <span @click="currentPage > 1 ? currentPage-- : 1" class="turn" :class="{grey: currentPage==1}">Preview</span>
        {{currentPage}} / {{pageCount}}
        <span @click="currentPage < pageCount ? currentPage++ : 1" class="turn" :class="{grey: currentPage==pageCount}">Next</span>
      </div>
  import pdf from 'vue-pdf'
  export default {
    components: {
      pdf
    },
    data() {
      return {
        pageCount: 0,
        currentPage: 1,
        fileUrl: ''
      }
    },

docx

npm install mammoth
// 请求文件流,本地文件参考文档
const xhr = new XMLHttpRequest();
xhr.open('get', 'your file url', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
  if (xhr.status == 200) {
    mammoth.convertToHtml({arrayBuffer: new Uint8Array(xhr.response)}).then(function (resultObject) {
      document.querySelector('#wordView').innerHTML =resultObject.value
    })
  }
}
xhr.send();

xls

npm install xlsx
if(typeof require !== 'undefined') XLSX = require('xlsx');
// let workbook = XLSX.readFile('test.xlsx');
const xhr = new XMLHttpRequest();
xhr.open('get', 'your file url', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
  if (xhr.status == 200) {
    let data = new Uint8Array(xhr.response)
    let workbook = XLSX.read(data, {type: 'array'});
    let sheetName = workbook.SheetNames
    let innerHTML = ''
    // 遍历多个表
    for (let i in sheetName) {
      let sheet = workbook.Sheets[sheetName[i]]
      innerHTML += XLSX.utils.sheet_to_html(sheet)
    }
    document.querySelector('#xlsxView').innerHTML = innerHTML
  }
};
xhr.send();
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值