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:下载组件库
html 复制代码
npm install @vue-office/docx [email protected]
1.2:页面使用
html 复制代码
<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:下载组件库
html 复制代码
npm install @vue-office/pdf [email protected]
2.2: 页面使用
html 复制代码
<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:下载组件库
html 复制代码
npm install @vue-office/excel [email protected]
3.2:页面使用
html 复制代码
<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:下载组件库
html 复制代码
npm install @vue-office/pptx [email protected]
4.2:页面使用
html 复制代码
<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>
相关推荐
白小白灬6 分钟前
Vue主题色切换实现方案(CSS 变量 + 类名切换)
前端·css·vue.js
诸葛大钢铁28 分钟前
如何免费在线PDF转换成Excel
职场和发展·pdf·excel
多云的夏天1 小时前
前端:VUE-(0)-环境搭建和helloworld
前端·javascript·vue.js
开开心心就好1 小时前
Word图片格式调整与转换工具
java·javascript·spring·eclipse·pdf·word·excel
GanGuaGua2 小时前
Vue3:脚手架
前端·javascript·css·vue.js·vue
weixin_431600442 小时前
使用 Vue Tour 封装一个统一的页面引导组件
javascript·vue.js·ecmascript
icloudelectron3 小时前
Altium Designer AD如何输出PIN带网络名的PDF装配图
pdf
沉到海底去吧Go3 小时前
【软件工具】基于PDF文件内容识别的改名软件,PDF根据内容自动重命名,如何识别pdf内容并做文件命名,PDF批量改名
pdf·扫描pdf文档批量文件改名·批量提取识别pdf中的特定字段·根据pdf某个区域内容改名·图片识别工具
EchoZeal3 小时前
【实测有效】Edge浏览器打开部分pdf文件显示空白
edge·pdf·adobe acrobat
胡斌附体3 小时前
vue添加loading后修复页面渲染问题
前端·javascript·vue.js·渲染·v-if·异步加载