1、安装依赖
npm install html-docx-js -S
npm install file-saver -S
2、绑定一个id
3、引入
4、点击导出事件
//模板word导出 const exportWordTpl= () =>{ let contentHtml = document.getElementById("cvb").innerHTML let cssHTML = ` table { width: 100%; table-layout: fixed; margin-top:10px; border: 1px solid #ddd; border-collapse: collapse; } .export-tb .thead td { font-weight: bold; } td { border: 1px solid #ddd; color: #333; text-align: left; padding: 6px 10px; }` let content = ` <!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="textml; charset=UTF-8"> <style> ${cssHTML} <yle> </head> <body> <table> ${contentHtml} </table> </body> <ml>`; let converted = htmlDocx.asBlob(content); FileSaver.saveAs(converted, '文件名.docx'); }
5、找到\node_modules\html-docx-js\dist\html-docx.js文件 更改一下内容
13101行更改
13133行
13147行: