使用vue-office預覽word文檔的功能詳解
本文將介紹如何使用vue-office來預覽word文檔,它支持多種文件(docx、pdf、excel)預覽的vue組件套裝,支持vue2/3。
安裝
//docx文檔預覽組件 npm install @vue-office/docx vue-demi
使用實例
文檔預覽場景大致可以分為兩種: 有文檔網(wǎng)絡地址,比如 https://***.docx 文件上傳時預覽,此時可以獲取文件的ArrayBuffer或Blob
docx文檔的預覽
這里主要介紹如何使用文件的blob來預覽,(因為上一節(jié)介紹了通過easy-poi來導出word文檔)。我這里使用的vue組件庫是antdv,如何你使用的是其他的可以對相關的進行替換
<template>
<a-button @click="handlePreview" :loading="loading">導出文檔</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="預覽"
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'
//引入相關樣式
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'); // 設置下載文件名
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ā)生請求接口:(注意這里一定要配置responseType: 'blob')
import http from "@/utils/server/http.js";
export const exportWord = (data) => {
return http.post('/exportWord', data, {responseType: 'blob'})
}
實際預覽的效果

使用vue- Office,可以非常的簡單的去預覽word文檔。
到此這篇關于使用vue-office預覽word文檔的功能詳解的文章就介紹到這了,更多相關vue預覽word內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新
這篇文章主要介紹了詳解如何實現(xiàn)Element樹形控件Tree在懶加載模式下的動態(tài)更新,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
談談因Vue.js引發(fā)關于getter和setter的思考
最近因為公司的新項目決定使用Vue.js來做,但在使用的過程中發(fā)現(xiàn)了一個有趣的事情,因為發(fā)現(xiàn)的這個事情展開了一些對于getter和setter的思考,具體是什么下面通過這篇文章來一起看看吧,有需要的朋友們可以參考學習。2016-12-12
基于Vue和ECharts實現(xiàn)定時更新與倒計時功能的實戰(zhàn)分享
在前端開發(fā)中,動態(tài)數(shù)據(jù)展示和用戶交互是構建現(xiàn)代 Web 應用的核心需求之一,在本篇博客中,我們將通過一個簡單的案例,展示如何在 Vue 中結合 ECharts 實現(xiàn)一個定時更新和倒計時功能,用于實時監(jiān)控和數(shù)據(jù)可視化,需要的朋友可以參考下2025-01-01

