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 vue-demi@0.14.6
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 vue-demi@0.14.6
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 vue-demi@0.14.6
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 vue-demi@0.14.6
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>
相关推荐
开开心心就好2 小时前
近200个工具的电脑故障修复合集
安全·智能手机·pdf·电脑·consul·memcache·1024程序员节
Python私教3 小时前
Pure-Admin-Thin 深度解析:完整版和精简版到底怎么选?
vue.js·人工智能·开源
其实秋天的枫3 小时前
2026年初中英语大纲词汇表1600词
经验分享·pdf
开开心心_Every4 小时前
轻量级PDF阅读器,仅几M大小打开秒开
linux·运维·服务器·安全·macos·pdf·phpstorm
福大大架构师每日一题5 小时前
ragflow v0.25.1 最新版发布:API 统一、PDF 解析性能大幅优化、连接器删除同步全面增强,更新要点一次看懂
pdf·ragflow
ayqy贾杰5 小时前
Cursor SDK发布!开发者可直接搬走其内核
前端·vue.js·面试
李白的天不白5 小时前
vue 数据格式问题
前端·vue.js·windows
小白蒋博客5 小时前
【ai开发段永平投资理财的知识图谱网站】第一天:搭 Vite + Vue 项目,跑通 Hello World
vue.js·人工智能·trae
@yanyu66616 小时前
登录注册功能-明文
vue.js·springboot
滕青山20 小时前
在线PDF拆分工具核心JS实现
前端·javascript·vue.js