欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Vue3中生成動(dòng)態(tài)的word文檔的示例代碼

 更新時(shí)間:2024年09月24日 08:33:09   作者:花信少年  
這篇文章主要介紹了如何在 Vue 3 中生成動(dòng)態(tài)的 Word 文檔,在開(kāi)發(fā)過(guò)程中遇到一個(gè)需求,動(dòng)態(tài)生成一個(gè)word報(bào)表,當(dāng)時(shí)考慮了是前端做還是后端做的問(wèn)題,最后發(fā)現(xiàn)兩個(gè)解決需求的方法都大差不差,但考慮到前端少發(fā)一個(gè)請(qǐng)求,就此使用的前端來(lái)解決,需要的朋友可以參考下

需求

在開(kāi)發(fā)過(guò)程中遇到一個(gè)需求,動(dòng)態(tài)生成一個(gè)word報(bào)表,當(dāng)時(shí)考慮了是前端做還是后端做的問(wèn)題,最后發(fā)現(xiàn)兩個(gè)解決需求的方法都大差不差,但考慮到前端少發(fā)一個(gè)請(qǐng)求,就此使用的前端來(lái)解決。

首先,我們需要安裝以下幾個(gè)庫(kù):

npm i docxtemplater     是一個(gè)用于基于模板生成動(dòng)態(tài) `.docx` 文檔的 JavaScript 庫(kù)。它允許你將動(dòng)態(tài)數(shù)據(jù)填充到 `.docx` 模板文件中
npm i jszip-utils       用于在瀏覽器中處理 ZIP 文件的工具庫(kù)
npm file-saver          是一個(gè)用于在瀏覽器中保存文件的 JavaScript 庫(kù)

創(chuàng)建一個(gè)工具函數(shù),用于加載 .docx 模板,填充數(shù)據(jù)并生成文檔。

//引入工具
import PizZip from 'pizzip';
import Docxtemplater from 'docxtemplater';
import JSZipUtils from 'jszip-utils';
import { saveAs } from 'file-saver';

// 加載 .docx 模板文件
function loadFile(url, callback) {
    JSZipUtils.getBinaryContent(url, callback);
}

// 下載生成的文檔
export function download(file, name) {
    saveAs(file, name);
}

// 生成并下載 Word 文檔(templatePath是word文檔模版地址,data是對(duì)應(yīng)的數(shù)據(jù))
export function generateWordDocument(templatePath, data) {
    return new Promise((resolve, reject) => {
        loadFile(templatePath, function (error, content) {
            if (error) {
                reject(new Error(`Error loading template file: ${error.message}`));
                return;
            }

            try {
                // 加載模板文件內(nèi)容到 PizZip
                const zip = new PizZip(content);
                const doc = new Docxtemplater(zip, {
                    paragraphLoop: true,
                    linebreaks: true,
                });

                // 設(shè)置模板中的占位符數(shù)據(jù)
                doc.setData(data);

                // 渲染文檔
                doc.render();

                // 生成最終的文檔 Blob
                const fileWord = doc.getZip().generate({
                    type: 'blob',
                    mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                });

                // 返回生成的文檔 Blob
                resolve(fileWord);
            } catch (error) {
                console.error('Error rendering document:', error);
                reject(new Error(`Error rendering document: ${error.message}`));
            }
        });
    });
}

在需要的組件內(nèi)調(diào)用

<template>
   <div>
    <button @click="startGeneration">生成文檔</button> <button @click="downloadWord" :disabled="!generatedFile">下載文檔</button> 
   </div>
 </template>
 
<script setup>
import { ref } from 'vue';
import { generateWordDocument, download } from '../utils/wordGenerate.js';

const generatedFile = ref(null);
// 模板文件的路徑,一般放在 public 目錄中
const templatePath = '/template.docx'; 
const data = { name: '測(cè)試', date: '2024-09-23' };
const startGeneration = async () => { 
try {
    generatedFile.value = await generateWordDocument(templatePath, data); 
    console.log('文檔生成成功');
} catch (error){
    console.error('Error generating document:', error);
  }
};
//文檔下載
const downloadWord = () => {
if (generatedFile.value) {
download(generatedFile.value, 'generated-document.docx');
} else {
console.error('No file generated');
}};
</script>

就此功能完成了,當(dāng)然如果需求是生成成功的同時(shí)就下載文檔,可以將下載功能直接寫(xiě)在生成功能內(nèi),就可以省去異步處理的代碼量。

到此這篇關(guān)于在Vue3中生成動(dòng)態(tài)的word文檔的示例代碼的文章就介紹到這了,更多相關(guān)Vue3生成動(dòng)態(tài)word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue中使用 pako.js 解密 gzip加密字符串的方法

    vue中使用 pako.js 解密 gzip加密字符串的方法

    這篇文章主要介紹了vue項(xiàng)目中 使用 pako.js 解密 gzip加密字符串 的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • vue中mint-ui環(huán)境搭建詳細(xì)介紹

    vue中mint-ui環(huán)境搭建詳細(xì)介紹

    這篇文章主要介紹了vue中mint-ui環(huán)境搭建詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • vue操作dom元素的3種方法示例

    vue操作dom元素的3種方法示例

    這篇文章主要給大家介紹了關(guān)于vue操作dom元素的3種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 詳解vue項(xiàng)目中實(shí)現(xiàn)圖片裁剪功能

    詳解vue項(xiàng)目中實(shí)現(xiàn)圖片裁剪功能

    這篇文章主要介紹了vue項(xiàng)目中實(shí)現(xiàn)圖片裁剪功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • vue 行為驗(yàn)證碼之滑動(dòng)驗(yàn)證AJ-Captcha使用詳解

    vue 行為驗(yàn)證碼之滑動(dòng)驗(yàn)證AJ-Captcha使用詳解

    這篇文章主要介紹了vue 行為驗(yàn)證碼之滑動(dòng)驗(yàn)證AJ-Captcha使用詳解,AJ-Captcha不需要npm安裝,只需要將組件 verifition復(fù)制到所使用的components目錄下,本文給大家詳細(xì)講解,需要的朋友可以參考下
    2023-05-05
  • 最新評(píng)論