Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件
簡(jiǎn)介
在現(xiàn)代的 Web 應(yīng)用中,預(yù)覽文檔和表格是一個(gè)常見的需求。本文將介紹如何使用 Vue3 和 TypeScript 開發(fā)一個(gè)高度可定制的文檔和表格預(yù)覽組件。
技術(shù)棧
Vue3
TypeScript
Element Plus
unocss
PDF文檔預(yù)覽
使用 Vue3 + TypeScript 實(shí)現(xiàn) PDF 預(yù)覽組件
Docx 預(yù)覽組件
功能特點(diǎn)
- 支持的文件格式: 僅支持預(yù)覽
Docx
類型的文件。 - 自定義選項(xiàng): 提供豐富的渲染選項(xiàng),如頁(yè)面寬度、高度、字體等。
- 通知提示: 在不支持的文件類型時(shí),會(huì)彈出通知提示。
組件實(shí)現(xiàn)
// types export interface RenderOptions { className?: string; // 默認(rèn)和文檔樣式類的類名/前綴 inWrapper?: boolean; // 啟用在文檔內(nèi)容周圍的包裝器渲染 ignoreWidth?: boolean; // 禁用頁(yè)面寬度渲染 ignoreHeight?: boolean; // 禁用頁(yè)面高度渲染 ignoreFonts?: boolean; // 禁用字體渲染 breakPages?: boolean; // 啟用分頁(yè)在頁(yè)面斷點(diǎn)上 ignoreLastRenderedPageBreak?: boolean; // 禁用在lastRenderedPageBreak元素上的分頁(yè) experimental?: boolean; // 啟用實(shí)驗(yàn)性功能(制表符計(jì)算) trimXmlDeclaration?: boolean; // 如果為true,將從解析之前的xml文檔中移除xml聲明 useBase64URL?: boolean; // 如果為true,圖像、字體等將轉(zhuǎn)換為base 64 URL,否則使用URL.createObjectURL useMathMLPolyfill?: boolean; // 包括MathML填充,適用于chrome、edge等 showChanges?: boolean; // 啟用文檔更改的實(shí)驗(yàn)性渲染(插入/刪除) debug?: boolean; // 啟用額外的日志記錄 }
<script setup lang="ts"> import { ref, computed } from "vue"; import VueOfficeDocx from "@vue-office/docx"; import "@vue-office/docx/lib/index.css"; import { ElNotification } from "element-plus"; import { RenderOptions } from "./types"; interface DocxProps { height?: string; // 組件高度 ==> 非必傳(默認(rèn)為 150px) customOptions?: RenderOptions; // 配置參數(shù) ==> 非必傳(默認(rèn)為 {}) } // 接收父組件參數(shù)并設(shè)置默認(rèn)值 const props = withDefaults(defineProps<DocxProps>(), { height: "80vh" }); const show = ref(false); const loading = ref(true); const docx = ref(); const url = ref("http://static.shanhuxueyuan.com/test.docx"); const customStyle = computed(() => { return { height: props.height }; }); const renderedCallback = () => { loading.value = false; }; const open = async (src?: string) => { show.value = true; if (src) { url.value = src; } fetch(url.value) .then(response => response.blob()) .then(res => { if (res.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { docx.value = res; } else { ElNotification({ title: "提示", message: "目前僅支持預(yù)覽 Docx 類型的文件", type: "warning" }); } }); }; defineExpose({ open }); </script> <template> <div> <el-dialog v-model="show" align-center title="預(yù)覽" width="80%"> <VueOfficeDocx v-loading="loading" :options="customOptions" :style="customStyle" :src="docx" @rendered="renderedCallback" /> </el-dialog> </div> </template> <style lang="scss" scoped></style>
Excel預(yù)覽組件
功能特點(diǎn)
- 支持的文件格式: 僅支持預(yù)覽
Excel
類型的文件。 - 自定義選項(xiàng): 提供豐富的渲染選項(xiàng),如最小渲染列數(shù)、行數(shù)等。
- 通知提示: 在不支持的文件類型時(shí),會(huì)彈出通知提示。
// types export interface ExcelRenderOptions { minColLength: number; // excel最少渲染多少列 minRowLength: number; // excel最少渲染多少行 widthOffset: number; // 在默認(rèn)渲染的列表寬度上再加10px寬 heightOffset: number; // 在默認(rèn)渲染的列表高度上再加10px高 beforeTransformData: (workbookData: any) => any; // 修改workbookData的函數(shù)類型定義 transformData?: (workbookData: any) => any; // 修改workbookData的函數(shù)類型定義 }
<script setup lang="ts"> import { ref, computed } from "vue"; import VueOfficeExcel from "@vue-office/excel"; import "@vue-office/excel/lib/index.css"; import { ElNotification } from "element-plus"; import { type ExcelRenderOptions } from "./types"; interface DocxProps { height?: string; // 組件高度 ==> 非必傳(默認(rèn)為 150px) customOptions?: Partial<ExcelRenderOptions>; // 配置參數(shù) ==> 非必傳(默認(rèn)為 {}) } // 接收父組件參數(shù)并設(shè)置默認(rèn)值 const props = withDefaults(defineProps<DocxProps>(), { height: "80vh" }); const show = ref(false); const loading = ref(true); const docx = ref(); const url = ref("http://static.shanhuxueyuan.com/demo/excel.xlsx"); const customStyle = computed(() => { return { height: props.height }; }); const renderedCallback = () => { loading.value = false; }; const open = async (src?: string) => { show.value = true; if (src) { url.value = src; } fetch(url.value) .then(response => response.blob()) .then(res => { if (res.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { docx.value = res; } else { ElNotification({ title: "提示", message: "目前僅支持預(yù)覽 Excel 類型的文件", type: "warning" }); } }); }; defineExpose({ open }); </script> <template> <div> <el-dialog v-model="show" align-center title="預(yù)覽" width="80%"> <VueOfficeExcel v-loading="loading" :options="customOptions" :style="customStyle" :src="docx" @rendered="renderedCallback" /> </el-dialog> </div> </template> <style lang="scss" scoped></style>
總結(jié)
通過使用 Vue3 和 TypeScript,我們可以輕松地開發(fā)出高度可定制的文檔和表格預(yù)覽組件。這些組件不僅提供了豐富的功能,還支持多種自定義選項(xiàng),以滿足不同項(xiàng)目的需求。
以上就是Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件的詳細(xì)內(nèi)容,更多關(guān)于Vue3 TypeScript預(yù)覽組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue3數(shù)字滾動(dòng)插件vue-countup-v3的使用
vue-countup-v3 插件是一個(gè)基于 Vue3 的數(shù)字動(dòng)畫插件,用于在網(wǎng)站或應(yīng)用程序中創(chuàng)建帶有數(shù)字動(dòng)畫效果的計(jì)數(shù)器,本文主要介紹了Vue3數(shù)字滾動(dòng)插件vue-countup-v3的使用,感興趣的可以了解一下2023-10-10淺析Vue3中通過v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡(jiǎn)化父子組件雙向綁定
這篇文章主要介紹了淺析Vue3中通過v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡(jiǎn)化父子組件雙向綁定,需要的朋友可以參考下2022-12-12詳解Vue內(nèi)部怎樣處理props選項(xiàng)的多種寫法
這篇文章主要介紹了詳解Vue內(nèi)部怎樣處理props選項(xiàng)的多種寫法,詳細(xì)的介紹了props的使用的寫法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11SpringBoot+Vue開發(fā)之Login校驗(yàn)規(guī)則、實(shí)現(xiàn)登錄和重置事件
這篇文章主要介紹了SpringBoot+Vue開發(fā)之Login校驗(yàn)規(guī)則、實(shí)現(xiàn)登錄和重置事件,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10vue+element實(shí)現(xiàn)動(dòng)態(tài)加載表單
這篇文章主要為大家詳細(xì)介紹了vue+element實(shí)現(xiàn)動(dòng)態(tài)加載表單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12Vue3+TypeScript實(shí)現(xiàn)二維碼生成組件
在?Web?應(yīng)用中,生成二維碼是常見的需求,本文介紹如何用?Vue3?和?TypeScript?開發(fā)一個(gè)二維碼生成組件,支持生成圖片或?canvas?形式的二維碼,并提供豐富的配置選項(xiàng),感興趣的小伙伴跟著小編一起來看看吧2024-04-04使用WebStorm導(dǎo)入已有Vue項(xiàng)目并運(yùn)行的詳細(xì)步驟與注意事項(xiàng)
這篇文章主要介紹了如何使用WebStorm導(dǎo)入、運(yùn)行和管理Vue項(xiàng)目,包括環(huán)境配置、Node.js和npm版本管理、項(xiàng)目依賴管理以及常見問題的解決方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue3封裝一個(gè)帶動(dòng)畫的關(guān)閉按鈕示例詳解
這篇文章主要為大家介紹了vue3封裝一個(gè)帶動(dòng)畫的關(guān)閉按鈕示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09