最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用vue-office預(yù)覽word文檔的功能詳解

 更新時(shí)間:2025年09月28日 15:52:08   作者:Bluecook  
這篇文章主要為大家詳細(xì)介紹了如何使用vue-office預(yù)覽word文檔的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

本文將介紹如何使用vue-office來(lái)預(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來(lái)預(yù)覽,(因?yàn)樯弦还?jié)介紹了通過(guò)easy-poi來(lái)導(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)文章

最新評(píng)論

涞源县| 新和县| 奎屯市| 从化市| 抚松县| 淄博市| 九龙坡区| 外汇| 阿荣旗| 长治县| 汤阴县| 莒南县| 朝阳市| 金湖县| 华安县| 定兴县| 西畴县| 金门县| 新泰市| 岳普湖县| 吴忠市| 武功县| 景德镇市| 绥中县| 鹤庆县| 蓬溪县| 邳州市| 从江县| 垣曲县| 耒阳市| 黄龙县| 双辽市| 中牟县| 繁峙县| 定远县| 雷山县| 东城区| 来安县| 汶上县| 龙门县| 永善县|