最新国产好看的视频,伊人天堂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來預(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)文章

最新評(píng)論

微山县| 泗洪县| 永福县| 广宁县| 手机| 湾仔区| 琼海市| 安多县| 海淀区| 清徐县| 黄大仙区| 博爱县| 阳西县| 镇雄县| 合江县| 清水河县| 黄骅市| 乌鲁木齐县| 安丘市| 家居| 孙吴县| 仁化县| 九江县| 历史| 新竹市| 阿城市| 崇义县| 蒲江县| 武乡县| 大丰市| 上思县| 大英县| 高唐县| 麻栗坡县| 黎城县| 梅河口市| 建湖县| 金门县| 寿宁县| 蒙阴县| 长寿区|