office 系列文件不下载浏览器在线预览

本文介绍了在文档管理中如何实现在线预览功能,包括微软的OfficeWebViewer服务,其优点是无需安装Office,但可能涉及文件隐私;以及使用vue-office组件进行本地和远程文件预览,提供隐私保护选项。前端代码示例展示了如何在Vue应用中集成这两种预览方式。
摘要由CSDN通过智能技术生成

如果遇到文档管理类的业务功能,会出现需要在线预览的业务需求,合同文件、发票文件等业务同样需要文件的在线预览。

解决方案一:微软 Office Web Viewer

Office Web Viewer 是由微软提供的一项免费服务,用户不需要安装 Office 软件,直接通过浏览器在线预览 docx、xlsx、pptx 文件。

Office Web Viewer 的服务链接为:

https://view.officeapps.live.com/op/view.aspx?src=[文件链接]

 其中 [文件链接] 是需要预览文件的在线地址,按需替换成自己的文件链接即可。

注意

  1. 文件会传输到微软的服务器上,因此可能会涉及到文件隐私。
  2. 不支持 pdf 预览。

适用场景

  • 用于公网展示的文件预览,如某公告文件在线预览,能快速实现需求。
  • 支持 docx、xlsx、pptx 文件,用于无需下载文件直接在线预览,适用于分享查看。

 前端实现:

<template>
  <div class="container">
    <div class="btns">
      <!-- 微软 Office Web Viewer 服务 -->
      <button @click="previewOffice(docx)">预览docx</button>
      <button @click="previewOffice(xlsx)">预览xlsx</button>
      <button @click="previewOffice(pptx)">预览pptx</button>
      <!-- pdf 直接预览 -->
      <button @click="officeUrl = pdf">预览pdf</button>
      <!-- 复制分享地址 -->
      <button class="copy-btn" v-if="officeUrl" @click="copyToClipboard(officeUrl)">
        复制分享地址
      </button>
    </div>
    <!-- 通过 iframe 嵌入 -->
    <iframe class="previewOffice" :src="officeUrl"></iframe>
  </div>
</template>

<script setup >
import { ref } from 'vue'


// 预览地址
const officeUrl = ref('')

// 资源地址
const docx = 'https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/solution/demo.docx'
const xlsx = 'https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/solution/demo.xlsx'
const pptx = 'https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/solution/demo.pptx'
const pdf = 'https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/solution/demo.pdf'

// 使用微软 Office Web Viewer 服务预览 office 文件
const previewOffice = (url) => {
  // 考虑到特殊字符,通过 encodeURIComponent 处理一下 url
  officeUrl.value = `https://view.officeapps.live.com/op/view.aspx?src=${url}`
}

// 复制分享地址
const copyToClipboard = async (text) => {
  await navigator.clipboard.writeText(text)
  alert('复制成功')
}
</script>

<style lang="scss" scoped>
body {
  margin: 0;
}

.container {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;

  .previewOffice {
    flex: 1;
  }

  .copy-btn {
    background-color: #070;
    color: #fff;
  }
}
</style>

效果如下: 

解决方案二:vue-office 组件 

 方案一文件会传输到微软的服务器上,因此可能会涉及到文件隐私问题。一些公网展示的文件可以使用方案一预览,涉及隐私可以用这个

1.docx 预览
  • 安装依赖 npm i @vue-office/docx
<template>
  <div>
    <button @click="getFile">获取远程</button>
    <input type="file" accept=".docx" @change="previewFile" />
    <vue-office-docx :src="src" style="height: 100vh" />
  </div>
</template>

<script setup >
// 引入 VueOffice 组件
import VueOfficeDocx from '@vue-office/docx'
// 引入相关样式
import '@vue-office/docx/lib/index.css'
import { ref } from 'vue'

// 设置文档网络地址,可以是本地文件
const src = ref()

// 本地预览
const previewFile = async (event) => {
  const file = event.target.files?.[0]
  src.value = file
}

// 请求预览
const getFile = () => {
  src.value = 'https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/solution/demo.docx'
}
</script>

<style lang="scss" scoped></style>
2.xlsx 预览
  • 安装依赖 npm i @vue-office/excel
<template>
  <div>
    <button @click="getFile">获取远程</button>
    <input type="file" accept=".xlsx" @change="previewFile" />
    <vue-office-excel :src="src" style="height: 100vh" />
  </div>
</template>

<script setup >
// 引入 VueOffice 组件
import VueOfficeExcel from '@vue-office/excel'
// 引入相关样式
import '@vue-office/excel/lib/index.css'
import { ref } from 'vue'

// 设置文档网络地址,可以是本地文件
const src = ref()

// 本地预览
const previewFile = async (event) => {
  const file = event.target.files?.[0]
  src.value = file
}

// 请求预览
const getFile = () => {
  src.value = 'https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/solution/demo.xlsx'
}
</script>

<style lang="scss" scoped></style>

3.pdf 预览
  • 安装依赖 npm i @vue-office/pdf


<template>
  <div>
    <button @click="getFile">获取远程</button>
    <input type="file" accept=".pdf" @change="previewFile" />
    <vue-office-pdf :src="src" style="height: 100vh" />
  </div>
</template>
  
<script setup>
// 引入 VueOffice 组件
import VueOfficePdf from '@vue-office/pdf'
import { ref } from 'vue'

// 设置文档网络地址,可以是本地文件
const src = ref()

// 本地预览
const previewFile = async (event) => {
  const file = event.target.files?.[0]
  src.value = file
}

// 请求预览
const getFile = () => {
  src.value = 'https://yjy-teach-oss.oss-cn-beijing.aliyuncs.com/solution/demo.pdf'
}
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值