> 技术文档 > html转word下载

html转word下载


一、插件使用

//转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)