一、插件使用
//轉html為word
npm i html-docx-js?
//保存文件到本地
npm i file-saver?
注:vite 項目使用esm模式會報錯,with方法錯誤,修改如下:
//直接安裝修復版本
npm i html-docx-fixed
二、封裝導出 exportWord.js
import htmlDocx from 'html-docx-fixed/dist/html-docx';
import saveAs from 'file-saver';export function exportToWord(content, fileName) {var converted = htmlDocx.asBlob(content);saveAs(converted, fileName+'.docx');
}
三、頁面調用?
import {exportToWord} from "./exportWord.js";
var content = document.getElementById("exportHtml"),fileName = "word";
exportToWord(content,fileName)