Vue3+TypeScript實現(xiàn)Docx/Excel預(yù)覽組件
簡介
在現(xiàn)代的 Web 應(yīng)用中,預(yù)覽文檔和表格是一個常見的需求。本文將介紹如何使用 Vue3 和 TypeScript 開發(fā)一個高度可定制的文檔和表格預(yù)覽組件。
技術(shù)棧
Vue3
TypeScript
Element Plus
unocss
PDF文檔預(yù)覽
使用 Vue3 + TypeScript 實現(xiàn) PDF 預(yù)覽組件
Docx 預(yù)覽組件
功能特點
- 支持的文件格式: 僅支持預(yù)覽
Docx類型的文件。 - 自定義選項: 提供豐富的渲染選項,如頁面寬度、高度、字體等。
- 通知提示: 在不支持的文件類型時,會彈出通知提示。
組件實現(xiàn)
// types
export interface RenderOptions {
className?: string; // 默認和文檔樣式類的類名/前綴
inWrapper?: boolean; // 啟用在文檔內(nèi)容周圍的包裝器渲染
ignoreWidth?: boolean; // 禁用頁面寬度渲染
ignoreHeight?: boolean; // 禁用頁面高度渲染
ignoreFonts?: boolean; // 禁用字體渲染
breakPages?: boolean; // 啟用分頁在頁面斷點上
ignoreLastRenderedPageBreak?: boolean; // 禁用在lastRenderedPageBreak元素上的分頁
experimental?: boolean; // 啟用實驗性功能(制表符計算)
trimXmlDeclaration?: boolean; // 如果為true,將從解析之前的xml文檔中移除xml聲明
useBase64URL?: boolean; // 如果為true,圖像、字體等將轉(zhuǎn)換為base 64 URL,否則使用URL.createObjectURL
useMathMLPolyfill?: boolean; // 包括MathML填充,適用于chrome、edge等
showChanges?: boolean; // 啟用文檔更改的實驗性渲染(插入/刪除)
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; // 組件高度 ==> 非必傳(默認為 150px)
customOptions?: RenderOptions; // 配置參數(shù) ==> 非必傳(默認為 {})
}
// 接收父組件參數(shù)并設(shè)置默認值
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ù)覽組件
功能特點
- 支持的文件格式: 僅支持預(yù)覽
Excel類型的文件。 - 自定義選項: 提供豐富的渲染選項,如最小渲染列數(shù)、行數(shù)等。
- 通知提示: 在不支持的文件類型時,會彈出通知提示。
// types
export interface ExcelRenderOptions {
minColLength: number; // excel最少渲染多少列
minRowLength: number; // excel最少渲染多少行
widthOffset: number; // 在默認渲染的列表寬度上再加10px寬
heightOffset: number; // 在默認渲染的列表高度上再加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; // 組件高度 ==> 非必傳(默認為 150px)
customOptions?: Partial<ExcelRenderOptions>; // 配置參數(shù) ==> 非必傳(默認為 {})
}
// 接收父組件參數(shù)并設(shè)置默認值
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ù)覽組件。這些組件不僅提供了豐富的功能,還支持多種自定義選項,以滿足不同項目的需求。
以上就是Vue3+TypeScript實現(xiàn)Docx/Excel預(yù)覽組件的詳細內(nèi)容,更多關(guān)于Vue3 TypeScript預(yù)覽組件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue3數(shù)字滾動插件vue-countup-v3的使用
vue-countup-v3 插件是一個基于 Vue3 的數(shù)字動畫插件,用于在網(wǎng)站或應(yīng)用程序中創(chuàng)建帶有數(shù)字動畫效果的計數(shù)器,本文主要介紹了Vue3數(shù)字滾動插件vue-countup-v3的使用,感興趣的可以了解一下2023-10-10
淺析Vue3中通過v-model實現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡化父子組件雙向綁定
這篇文章主要介紹了淺析Vue3中通過v-model實現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡化父子組件雙向綁定,需要的朋友可以參考下2022-12-12
SpringBoot+Vue開發(fā)之Login校驗規(guī)則、實現(xiàn)登錄和重置事件
這篇文章主要介紹了SpringBoot+Vue開發(fā)之Login校驗規(guī)則、實現(xiàn)登錄和重置事件,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
vue+element實現(xiàn)動態(tài)加載表單
這篇文章主要為大家詳細介紹了vue+element實現(xiàn)動態(tài)加載表單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12
Vue3+TypeScript實現(xiàn)二維碼生成組件
在?Web?應(yīng)用中,生成二維碼是常見的需求,本文介紹如何用?Vue3?和?TypeScript?開發(fā)一個二維碼生成組件,支持生成圖片或?canvas?形式的二維碼,并提供豐富的配置選項,感興趣的小伙伴跟著小編一起來看看吧2024-04-04
使用WebStorm導(dǎo)入已有Vue項目并運行的詳細步驟與注意事項
這篇文章主要介紹了如何使用WebStorm導(dǎo)入、運行和管理Vue項目,包括環(huán)境配置、Node.js和npm版本管理、項目依賴管理以及常見問題的解決方案,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-11-11
使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實例
今天小編就為大家分享一篇使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

