前端實(shí)現(xiàn)docx文件預(yù)覽的3種方式舉例及分析
需求:
有一個(gè)docx文件,需要按其本身的格式,將內(nèi)容展示出來(lái),即:實(shí)現(xiàn)doc文件預(yù)覽。
本文將doc文件存放到前端項(xiàng)目的public目錄下

1、docx-preview 實(shí)現(xiàn)(推薦)
安裝命令 npm install docx-preview

實(shí)現(xiàn)代碼
<template>
<div class="document-wrapper">
<div class="content" ref="contentRef"></div>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import axios from "axios";
import { renderAsync } from 'docx-preview';
const contentRef = ref<any>(null);
function getDocxContent() {
// 注意:如果是前端本地靜態(tài)文件,需要放放在public目錄下
axios.get('/test.docx', { responseType: 'arraybuffer' }).then((res) => {
renderAsync(res.data, contentRef.value);
}).catch((err) => { console.log(err) })
}
getDocxContent();
</script>
<style lang="less">
.document-wrapper {
margin: 10px;
}
// 設(shè)置word文檔的樣式
// .docx-wrapper {
// background: white !important;
// border: 1px solid red;
// section {
// width: 100% !important;
// padding: 20px !important;
// }
// .docx {
// box-shadow: none !important;
// }
// }</style>
效果: 樣式還原度一般,無(wú)分頁(yè)

2、vue-office 實(shí)現(xiàn)
vue-office特點(diǎn):
- 一站式:提供word(.docx)、pdf、excel(.xlsx, .xls)、ppt(.pptx)多種文檔的在線(xiàn)預(yù)覽方案。
- 簡(jiǎn)單:只需提供文檔的src(網(wǎng)絡(luò)地址)即可完成文檔預(yù)覽。
- 體驗(yàn)好:選擇每個(gè)文檔的最佳預(yù)覽方案,保證用戶(hù)體驗(yàn)和性能都達(dá)到最佳狀態(tài)。
- 性能好:針對(duì)數(shù)據(jù)量較大做了優(yōu)化。
安裝命令
# docx文檔預(yù)覽,基于docx-preview庫(kù)實(shí)現(xiàn) npm install @vue-office/docx # excel文檔預(yù)覽,基于exceljs和x-data-spreadsheet實(shí)現(xiàn),全網(wǎng)樣式支持更好 npm install @vue-office/excel # pdf文檔預(yù)覽,基于pdfjs庫(kù)實(shí)現(xiàn),實(shí)現(xiàn)了虛擬列表增加性能 npm install @vue-office/pdf # pptx文檔預(yù)覽,基于pptx-preview實(shí)現(xiàn) npm install @vue-office/pptx
本文只針對(duì)word文檔,安裝好如下:

實(shí)現(xiàn)代碼
<template>
<div class="document-wrapper">
<VueOfficeDocx :src="fileData" />
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import axios from "axios";
import VueOfficeDocx from '@vue-office/docx'
const fileData = ref<any>(null);
function getDocxContent() {
// 注意:本地靜態(tài)文件需要放放在public目錄下
axios.get('/test.docx', { responseType: 'arraybuffer' }).then((res) => {
fileData.value = res.data;
}).catch((err) => { console.log(err) })
}
getDocxContent();
</script>
<style lang="less">
.document-wrapper {
margin: 10px;
}
</style>
效果: 同第一種方式實(shí)現(xiàn)的效果

3、mammoth 實(shí)現(xiàn)(不推薦)
安裝命令:npm install mammoth

實(shí)現(xiàn)代碼
<template>
<div class="document-wrapper">
<div ref="docxPreviewRef" v-html="fileContent"></div>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import axios from "axios";
import mammoth from 'mammoth';
const fileContent = ref<any>(null);
function getDocxContent() {
axios.get('/test.docx', { responseType: 'arraybuffer' }).then((res) => {
mammoth.convertToHtml({ arrayBuffer: new Uint8Array(res.data) }).then((res) => {
fileContent.value = res.value;
})
}).catch((err) => { console.log(err) })
}
getDocxContent();
</script>
<style lang="less">
.document-wrapper {
margin: 10px;
}
</style>
效果: word文檔的樣式?jīng)]有了,所以不推薦直接使用此中方式預(yù)覽.docx文件

總結(jié)
到此這篇關(guān)于前端實(shí)現(xiàn)docx文件預(yù)覽的3種方式的文章就介紹到這了,更多相關(guān)前端實(shí)現(xiàn)docx文件預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
原生js實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)代碼分享
這篇文章主要介紹了原生js實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)功能以及代碼分享,對(duì)此有需要的朋友可以參考學(xué)習(xí)下。2018-02-02
JavaScript實(shí)現(xiàn)shuffle數(shù)組洗牌操作示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)shuffle數(shù)組洗牌操作,結(jié)合實(shí)例形式分析了javascript數(shù)組的定義、構(gòu)造、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
javascript過(guò)濾數(shù)組重復(fù)元素的實(shí)現(xiàn)方法
這篇文章主要介紹了javascript過(guò)濾數(shù)組重復(fù)元素的實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-05-05
JS動(dòng)態(tài)更改div高度實(shí)現(xiàn)代碼例子
在Web開(kāi)發(fā)中通過(guò)使用JavaScript可以動(dòng)態(tài)地修改HTML元素的屬性和樣式,下面這篇文章主要給大家介紹了關(guān)于JS動(dòng)態(tài)更改div高度實(shí)現(xiàn)的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-11-11
淺談js中同名函數(shù)和同名變量的執(zhí)行問(wèn)題
下面小編就為大家?guī)?lái)一篇淺談js中同名函數(shù)和同名變量的執(zhí)行問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
javascript自定義右鍵彈出菜單實(shí)現(xiàn)方法
這篇文章主要介紹了javascript自定義右鍵彈出菜單實(shí)現(xiàn)方法,涉及javascript操作鼠標(biāo)事件及頁(yè)面元素的相關(guān)技巧,需要的朋友可以參考下2015-05-05
uniapp使用u-upload組件來(lái)實(shí)現(xiàn)圖片上傳功能
最近在用uniapp開(kāi)發(fā)微信小程序,下面這篇文章主要給大家介紹了關(guān)于uniapp使用u-upload組件來(lái)實(shí)現(xiàn)圖片上傳功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

