vue-office 支持预览多种文件(docx、excel、pdf、pptx)预览的vue组件库

官网地址:https://github.com/501351981/vue-office 

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

1.在线预览word文件(以及本地上传预览)

        1.1:下载组件库
npm install @vue-office/docx vue-demi@0.14.6
        1.2:页面使用
<template>
    <div>
        <!-- 在线word预览 -->
        <!-- 本地文件上传预览 -->
        <input type="file" @change="changeHandle" />
        <vue-office-docx :src="docx" class="wordOffce" @rendered="rendered" />
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
//引入相关样式
import '@vue-office/docx/lib/index.css';

let docx = ref<any>('http://static.shanhuxueyuan.com/test6.docx');
let rendered = () => {
    console.log("渲染完成")
}
// 本地文件上传预览
let changeHandle = (event: any) => {
    let file = event.target.files[0]
    let fileReader = new FileReader()
    fileReader.readAsArrayBuffer(file)
    fileReader.onload = () => {
        docx.value = fileReader.result;
    }
}
</script>
<style lang="less" scoped>
.wordOffce {
    height: 80vh !important;

    :deep(.docx-wrapper) {
        background-color: #fff !important;
    }
}
</style>

2. 在线预览PDF文件(以及本地上传预览)

         2.1:下载组件库
npm install @vue-office/pdf vue-demi@0.14.6
        2.2: 页面使用
<template>
    <div>
        <!-- 在线pdf预览 -->
        <!-- 本地文件上传预览 -->
        <input type="file" @change="changeHandle" />
        <vue-office-pdf :src="pdf" class="wordOffce" @rendered="renderedHandler" @error="errorHandler" />
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
//引入VueOfficeDocx组件
import VueOfficePdf from '@vue-office/pdf'

let pdf = ref<any>('http://static.shanhuxueyuan.com/test.pdf');
let renderedHandler = () => {
    console.log("渲染完成")
}
let errorHandler = () => {
    console.log("渲染失败")
}
// 本地文件上传预览
let changeHandle = (event: any) => {
    let file = event.target.files[0]
    let fileReader = new FileReader()
    fileReader.readAsArrayBuffer(file)
    fileReader.onload = () => {
        pdf.value = fileReader.result;
    }
}
</script>
<style lang="less" scoped>
.wordOffce {
    height: 80vh !important;
    box-shadow: 0px 0px 5px 27px rgba(0, 0, 0, 0.75);
    -webkit-box-shadow: 0px 0px 5px 27px rgba(0, 0, 0, 0.75);
    -moz-box-shadow: 0px 0px 5px 27px rgba(0, 0, 0, 0.75);

    :deep(.vue-office-pdf-wrapper) {
        background-color: #fff !important;
    }
}
</style>

3. 在线预览Excel文件(以及本地上传预览)

        3.1:下载组件库
npm install @vue-office/excel vue-demi@0.14.6
        3.2:页面使用
<template>
    <div>
        <!-- 在线pdf预览 -->
        <!-- 本地文件上传预览 -->
        <input type="file" @change="changeHandle" />
        <vue-office-excel :src="excel" class="wordOffce" @rendered="renderedHandler" @error="errorHandler" />
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
//引入VueOfficeDocx组件
import VueOfficeExcel from '@vue-office/excel'
import '@vue-office/excel/lib/index.css'

let excel = ref<any>('http://static.shanhuxueyuan.com/demo/excel.xlsx');
let renderedHandler = () => {
    console.log("渲染完成")
}
let errorHandler = () => {
    console.log("渲染失败")
}
// 本地文件上传预览
let changeHandle = (event: any) => {
    let file = event.target.files[0]
    let fileReader = new FileReader()
    fileReader.readAsArrayBuffer(file)
    fileReader.onload = () => {
        excel.value = fileReader.result;
    }
}
</script>
<style lang="less" scoped>
.wordOffce {
    height: 80vh !important;
}
</style>

4. 在线预览pptx文件(以及本地上传预览)

        4.1:下载组件库
npm install @vue-office/pptx vue-demi@0.14.6
        4.2:页面使用 
<template>
    <div>
        <!-- 在线pdf预览 -->
        <!-- 本地文件上传预览 -->
        <input type="file" @change="changeHandle" />
        <vue-office-pptx :src="pptx" class="wordOffce" @rendered="renderedHandler" @error="errorHandler" />
    </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
//引入VueOfficeDocx组件
import VueOfficePptx from '@vue-office/pptx'

let pptx = ref<any>('http://static.shanhuxueyuan.com/test.pptx');
let renderedHandler = () => {
    console.log("渲染完成")
}
let errorHandler = () => {
    console.log("渲染失败")
}
// 本地文件上传预览
let changeHandle = (event: any) => {
    let file = event.target.files[0]
    let fileReader = new FileReader()
    fileReader.readAsArrayBuffer(file)
    fileReader.onload = () => {
        pptx.value = fileReader.result;
    }
}
</script>
<style lang="less" scoped>
.wordOffce {
    height: 80vh !important;
}
</style>
 

        

### vue-office 展示 DOC 和 XLS 文件失败的原因分析 `vue-office` 主要支持 `.docx`、`.xlsx`、`.pdf` 和 `.pptx` 等现代 Office 文档格式的在线预览功能[^1]。然而,传统的 `.doc` 和 `.xls` 文件并不被直接支持,因为这些文件属于旧版二进制格式,而 `vue-office` 更倾向于处理基于 XML 的新版本文档(如 `.docx` 和 `.xlsx`)。以下是可能的解决方案: --- #### 方案一:将 `.doc` 转换为 `.docx` 或 `.xls` 转换为 `.xlsx` 最简单的办法是先将传统格式转换为较新的 XML 基础格式。可以通过以下方式实现: - **手动转换**:使用 Microsoft Office 或 WPS 手动打开并保存为 `.docx` 或 `.xlsx` 格式。 - **自动转换服务**:借助第三方工具或 API 自动批量转换。 如果需要程序化操作,可以考虑使用 Node.js 中的一些库来完成这一过程,例如 `mammoth` 用于 `.doc` 到 `.docx` 的转换[^4]。 ```javascript const mammoth = require("mammoth"); async function convertDocToDocx(docFilePath, outputFilePath) { const result = await mammoth.convertToHtml({ path: docFilePath }); fs.writeFileSync(outputFilePath, result.value); } ``` --- #### 方案二:通过中间件服务器进行实时转换 对于动态生成的传统格式文件,可以在后端部署一个中间层服务,负责将 `.doc` 和 `.xls` 实时转换为 `.docx` 和 `.xlsx` 后返回给前端。常见的技术栈包括但不限于: - 使用 LibreOffice 提供的命令行接口 (`soffice --headless`) 进行无头模式下的文件转换。 - 部署专门的服务(如 Aspose.Cells Cloud SDK),该服务能够高效地执行此类任务。 下面是一个基于 LibreOffice 的简单脚本例子: ```bash libreoffice --convert-to docx input.doc --outdir /path/to/output/ ``` 之后,前端只需要加载经过转换后的 `.docx` 文件即可正常显示[^3]。 --- #### 方案三:嵌入第三方插件支持老旧格式 尽管 `vue-office` 不原生支持 `.doc` 和 `.xls`,但可以选择集成其他开源项目或者商业产品作为补充手段。比如 Google Docs Viewer 或者微软自带的 Web 版 Office Online Service,在某些场景下可以直接解析这类老式文档并通过 iframe 渲染出来。 不过需要注意的是这种方法可能会带来额外的安全隐患以及用户体验上的局限性,因此建议谨慎评估后再决定是否采用这种方式[^2]。 --- ### 总结 综上所述,针对 `vue-office` 无法展示 `.doc` 和 `.xls` 文件的问题,推荐优先尝试将原始文件升级到最新标准 (即 `.docx`, `.xlsx`);其次可探索搭建后台服务做即时迁移;最后才考虑依赖外部资源完成相应目标。每种方法各有优劣,请依据实际业务需求权衡利弊做出最佳抉择。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值