使用vue-office預(yù)覽word文檔的功能詳解
本文將介紹如何使用vue-office來預(yù)覽word文檔,它支持多種文件(docx、pdf、excel)預(yù)覽的vue組件套裝,支持vue2/3。
安裝
//docx文檔預(yù)覽組件 npm install @vue-office/docx vue-demi
使用實(shí)例
文檔預(yù)覽場(chǎng)景大致可以分為兩種: 有文檔網(wǎng)絡(luò)地址,比如 https://***.docx 文件上傳時(shí)預(yù)覽,此時(shí)可以獲取文件的ArrayBuffer或Blob
docx文檔的預(yù)覽
這里主要介紹如何使用文件的blob來預(yù)覽,(因?yàn)樯弦还?jié)介紹了通過easy-poi來導(dǎo)出word文檔)。我這里使用的vue組件庫(kù)是antdv,如何你使用的是其他的可以對(duì)相關(guān)的進(jìn)行替換
<template>
<a-button @click="handlePreview" :loading="loading">導(dǎo)出文檔</a-button>
<a-drawer
v-model:open="open"
class="custom-class"
width="80%"
root-class-name="root-class-name"
:root-style="{ color: 'blue' }"
style="color: red"
title="預(yù)覽"
placement="right"
@after-open-change="afterOpenChange"
>
<template #extra>
<a-button style="margin-right: 8px" @click="handleDownload">下載</a-button>
</template>
<vue-office-docx :src="docx" @rendered="rendered"/>
</a-drawer>
</template>
<script setup>
//引入VueOfficeDocx組件
import VueOfficeDocx from '@vue-office/docx'
//引入相關(guān)樣式
import '@vue-office/docx/lib/index.css'
import {exportWord} from "@/api/exportWord/index.js";
import {ref} from "vue";
const docx = ref('')
const open = ref(false)
const loading = ref(false)
const handlePreview = () => {
loading.value = true
exportWord({}).then(res => {
const url = window.URL.createObjectURL(new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'}));
docx.value = url;
open.value = true
}).finally(() => {
loading.value = false
})
}
const rendered = () => {
console.log('rendered')
}
const afterOpenChange = () => {
console.log('afterOpenChange')
}
const handleDownload = () => {
// 獲取該word文檔并下載
const link = document.createElement('a');
link.href = docx.value;
link.setAttribute('download', 'export.docx'); // 設(shè)置下載文件名
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
<style scoped>
:deep(.docx-wrapper) {
background-color: #ffffff;
}
:deep(.docx) {
width: 100% !important;
}
</style>
import {exportWord} from "@/api/exportWord/index.js";引入的是向后端發(fā)生請(qǐng)求接口:(注意這里一定要配置responseType: 'blob')
import http from "@/utils/server/http.js";
export const exportWord = (data) => {
return http.post('/exportWord', data, {responseType: 'blob'})
}
實(shí)際預(yù)覽的效果

使用vue- Office,可以非常的簡(jiǎn)單的去預(yù)覽word文檔。
到此這篇關(guān)于使用vue-office預(yù)覽word文檔的功能詳解的文章就介紹到這了,更多相關(guān)vue預(yù)覽word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uniapp+vue3路由跳轉(zhuǎn)傳參的實(shí)現(xiàn)
本文主要介紹了uniapp+vue3路由跳轉(zhuǎn)傳參的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
vue實(shí)現(xiàn)錄音并轉(zhuǎn)文字功能包括PC端web手機(jī)端web(實(shí)現(xiàn)過程)
vue實(shí)現(xiàn)錄音并轉(zhuǎn)文字功能,包括PC端,手機(jī)端和企業(yè)微信自建應(yīng)用端,本文通過實(shí)例代碼介紹vue實(shí)現(xiàn)錄音并轉(zhuǎn)文字功能包括PC端web手機(jī)端web,感興趣的朋友跟隨小編一起看看吧2024-08-08
Vue如何使用js-audio-recorder插件實(shí)現(xiàn)錄音功能并將文件轉(zhuǎn)成wav上傳
這篇文章主要給大家介紹了關(guān)于Vue如何使用js-audio-recorder插件實(shí)現(xiàn)錄音功能并將文件轉(zhuǎn)成wav上傳的相關(guān)資料,文中通過示例代碼講解了彈窗界面、變量控制、錄音啟動(dòng)與停止、波形可視化、文件上傳及WAV格式獲取的完整流程,需要的朋友可以參考下2025-06-06
Vue數(shù)據(jù)更新視圖不更新的幾種解決方案小結(jié)
這篇文章主要介紹了Vue數(shù)據(jù)更新視圖不更新的幾種解決方案小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue項(xiàng)目及axios請(qǐng)求獲取數(shù)據(jù)方式
這篇文章主要介紹了vue項(xiàng)目及axios請(qǐng)求獲取數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue前端實(shí)現(xiàn)導(dǎo)出頁(yè)面為word的兩種方法代碼
在前端開發(fā)中我們常常需要將頁(yè)面頁(yè)面為word文件,這篇文章主要給大家介紹了關(guān)于vue前端實(shí)現(xiàn)導(dǎo)出頁(yè)面為word的兩種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
vue3學(xué)習(xí)指導(dǎo)教程(附帶獲取屏幕可視區(qū)域?qū)捀?
Vue3是Vue的第三個(gè)版本更快,更輕,易維護(hù),更多的原生支持,下面這篇文章主要給大家介紹了關(guān)于vue3學(xué)習(xí)指導(dǎo)教程(附帶獲取屏幕可視區(qū)域?qū)捀?的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04

