vue-cli3+docxtemplater實現(xiàn)多個數(shù)據(jù)表格導(dǎo)出成docx及部署

實現(xiàn)效果

image.png

插件文檔地址
https://docxtemplater.com/docs/get-started-node/
1. 安裝對應(yīng)依賴

npm install  docxtemplater pizzip jszip-utils jszip file-saver --save  
image.png

2.定義函數(shù)方法封裝
在utils目錄下創(chuàng)建downTable.js文件

  /**
     導(dǎo)出docx
     @param { String } tempDocxPath 模板文件路徑
      @param { Object } data 文件中傳入的數(shù)據(jù)
     @param { String } fileName 導(dǎo)出文件名稱
*/
import { Message } from "element-ui";
import docxtemplater from "docxtemplater";
import PizZip from "pizzip";
import JSZipUtils from "jszip-utils";
import { saveAs } from "file-saver";
export const exportDocx = (tempDocxPath, data, fileName) => {
    // 讀取并獲得模板文件的二進制內(nèi)容
    JSZipUtils.getBinaryContent(tempDocxPath, (error, content) => {
     // input.docx是模板宣蠕。我們在導(dǎo)出的時候,會根據(jù)此模板來導(dǎo)出對應(yīng)的數(shù)據(jù)
      // 拋出異常
      if (error) {
        throw error
      }
       // 創(chuàng)建一個JSZip實例储矩,內(nèi)容為模板的內(nèi)容
      const zip = new PizZip(content)
       // 創(chuàng)建并加載docxtemplater實例對象
      const doc = new docxtemplater().loadZip(zip)
      // 設(shè)置模板變量的值
      doc.setData({
        list: data.list
      })
      try {
        // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...)
        doc.render()
      } catch (error) {
        const e = {
          message: error.message,
          name: error.name,
          stack: error.stack,
          properties: error.properties
        }
        console.log({
          error: e
        })
        Message.error(e)
        // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
        throw error
      }
      const out = doc.getZip().generate({
        type: 'blob',
        mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
      }) // Output the document using Data-URI
      saveAs(out, fileName)
    })
  }

3. 下載

import { exportDocx } from "@/utils/downTable.js";
export default {
  name: "Home",
  data() {
    return {
      downData: [
          {
            "columns": [
                {
                    "columnSize": 10,
                    "isNull": "YES",
                    "typeName": "INT",
                    "digits": 0,
                    "columnDef": "",
                    "remarks": "",
                    "columnName": "id"
                },
                {
                    "columnSize": 1024,
                    "isNull": "YES",
                    "typeName": "VARCHAR",
                    "digits": 0,
                    "columnDef": "",
                    "remarks": "",
                    "columnName": "bookname"
                },
                {
                    "columnSize": 10,
                    "isNull": "YES",
                    "typeName": "INT",
                    "digits": 0,
                    "columnDef": "",
                    "remarks": "",
                    "columnName": "size"
                }
            ],
            "dbName": "test_1",
            "comment": "",
            "tableName": "book"
        },
        {
            "columns": [
                {
                    "columnSize": 10,
                    "isNull": "NO",
                    "typeName": "INT",
                    "digits": 0,
                    "columnDef": "",
                    "remarks": "",
                    "columnName": "id"
                },
                {
                    "columnSize": 10,
                    "isNull": "NO",
                    "typeName": "INT",
                    "digits": 0,
                    "columnDef": "",
                    "remarks": "",
                    "columnName": "i"
                }
            ],
            "dbName": "test_1",
            "comment": "",
            "tableName": "t1"
        }],
    };
  },
  methods: {
    downFile() {
      var that = this;
      const data = {
        list: that.downData,
      };
      exportDocx("/temp.docx", data, `${that.dbNameNow}.docx`);
    },
  },
};

4. 創(chuàng)建docx模板
vue-cli3需要將temp.docx模板放置到public目錄下锌云,vue-cli2放置到static目錄下

image.png

5. 部署
在打包前需要將模板的路徑修改成exportDocx("temp.docx", data, ${that.dbNameNow}.docx);否則會導(dǎo)致獲取模板404

image.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末荠医,一起剝皮案震驚了整個濱河市吁脱,隨后出現(xiàn)的幾起案子桑涎,更是在濱河造成了極大的恐慌,老刑警劉巖兼贡,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件攻冷,死亡現(xiàn)場離奇詭異,居然都是意外死亡遍希,警方通過查閱死者的電腦和手機等曼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人禁谦,你說我怎么就攤上這事胁黑。” “怎么了州泊?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵丧蘸,是天一觀的道長。 經(jīng)常有香客問我遥皂,道長力喷,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任演训,我火速辦了婚禮弟孟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘样悟。我一直安慰自己拂募,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布乌奇。 她就那樣靜靜地躺著没讲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪礁苗。 梳的紋絲不亂的頭發(fā)上爬凑,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機與錄音试伙,去河邊找鬼嘁信。 笑死,一個胖子當(dāng)著我的面吹牛疏叨,可吹牛的內(nèi)容都是我干的潘靖。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蚤蔓,長吁一口氣:“原來是場噩夢啊……” “哼卦溢!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起秀又,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤单寂,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后吐辙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宣决,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年昏苏,在試婚紗的時候發(fā)現(xiàn)自己被綠了尊沸。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片威沫。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖洼专,靈堂內(nèi)的尸體忽然破棺而出棒掠,到底是詐尸還是另有隱情,我是刑警寧澤屁商,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布句柠,位于F島的核電站,受9級特大地震影響棒假,放射性物質(zhì)發(fā)生泄漏溯职。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一帽哑、第九天 我趴在偏房一處隱蔽的房頂上張望谜酒。 院中可真熱鬧,春花似錦妻枕、人聲如沸僻族。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽述么。三九已至,卻和暖如春愕掏,著一層夾襖步出監(jiān)牢的瞬間度秘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工饵撑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留剑梳,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓滑潘,卻偏偏與公主長得像垢乙,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子语卤,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容