├── README.md ├── mhtmlToWord.js ├── package-lock.json └── package.json /README.md: -------------------------------------------------------------------------------- 1 | ## mhtml-to-word 2 | mhtmlToWord.js是将html模板字符串通过模板引擎转换并导出word文件的js库,它支持浏览器环境和nodejs支持环境如react,vue等 3 | ### 演示地址:[http://118.24.95.11:8888](http://118.24.95.11:8888) 4 | ### 依赖 5 | + FileSaver.js 6 | + baiduTemplatePro.js (可选) 7 | ### 安装 8 | #### 浏览器 9 | 需下载 10 | + mhtmlToWord.js: [https://github.com/BetterZxx/mhtml-to-word](https://github.com/BetterZxx/mhtml-to-word) 11 | + baiduTemplatePro.js: [https://github.com/BetterZxx/baidu-template-pro](https://github.com/BetterZxx/baidu-template-pro) 12 | + FileSaver.js: [https://github.com/eligrey/FileSaver.js](https://github.com/eligrey/FileSaver.js) 13 | #### nodejs支持环境 14 | ``` 15 | npm install mhtml-to-word 16 | ``` 17 | ### 用法 18 | + nodejs环境 19 | ``` 20 | import { exportWord } from mhtml-to-word 21 | ``` 22 | ``` 23 | exportWord({String mhtml, String filename, optional String style, Object data,optional String selector}) 24 | ``` 25 | + 浏览器环境直接使用 `exportWord()` 26 | 27 | 28 | ### 例子 29 | ##### 使用selector 30 | ``` 31 | exportWord({ 32 | selector: ".box", 33 | style: "p{font-size: 30px; color: red;}", 34 | filename: "exportTest" 35 | }) 36 | ``` 37 | ##### 使用模板字符串(模板使用方法: [baidu-template-pro](https://github.com/BetterZxx/baidu-template-pro)) 38 | ``` 39 | var model = ` 40 |
41 | <% for(var i = 0 ; i < 10 ; i++){ %> 42 | <%=title%> 43 |
44 | ` 45 | exportWord({ 46 | mhtml: model, 47 | data: {title: "exportword"}, 48 | filename: "exportTest", 49 | style: "span{ font-size:30px; }" 50 | }) 51 | 52 | ``` 53 | 54 | -------------------------------------------------------------------------------- /mhtmlToWord.js: -------------------------------------------------------------------------------- 1 | 2 | ;(function(window){ 3 | //符合commonjs规范引入依赖 4 | if(typeof module !== 'undefined'){ 5 | window.saveAs = require('file-saver') 6 | window.baidu = require('baidu-template-pro') 7 | } 8 | function getModelHtml(mhtml,style=''){ 9 | return` 10 | Content-Type: text/html; charset="utf-8" 11 | 12 | 13 | 14 | 17 | 18 | 19 | ${mhtml} 20 | 21 | 22 | ` 23 | } 24 | //主函数 25 | let exportWord = ({mhtml,style,filename,data,selector})=>{ 26 | 27 | if(selector){ 28 | let nodes = window.document.querySelectorAll(selector) 29 | mhtml = nodes.length>0?Array.from(nodes).reduce((a,b)=>a+b.innerHTML,''):'' 30 | 31 | } 32 | 33 | //没有baiduTemplatePro.js依赖时必须传入selector 34 | if (!selector && typeof baidu === 'undefined') { 35 | console.error("wordExport : missing (selector) for params without depandency (baiduTemplatePro.js)"); 36 | return; 37 | } 38 | if (typeof saveAs === "undefined") { 39 | console.error("wordExport : missing dependency (FileSaver.js)"); 40 | return; 41 | } 42 | 43 | //没有模板引擎时,将获取节点的html字符串生成模板 44 | let html = typeof baidu !== 'undefined'?baidu.template(getModelHtml(mhtml,style),data):getModelHtml(mhtml) 45 | 46 | let blob = new Blob([html],{type:'application/msword;charset=utf-8'}) 47 | saveAs(blob,filename+'.doc') 48 | } 49 | //添加exportWord到全局对象 50 | window.exportWord = window.exportWord||exportWord 51 | 52 | //如果符合commonjs规范,exports出去 53 | if(typeof module==='object'&&typeof module.exports==='object'){ 54 | module.exports = {exportWord} 55 | } 56 | 57 | })(window) -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mhtml-to-word", 3 | "version": "1.0.2", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "baidu-template-pro": { 8 | "version": "1.0.0", 9 | "resolved": "https://registry.npmjs.org/baidu-template-pro/-/baidu-template-pro-1.0.0.tgz", 10 | "integrity": "sha512-gnw6uvCn4CvZZ9d1Tu+a0B7BjQuNXvDcxsJk7bwxTpNutj6JFIxcy2sS0rB2/0/t9GwatoEIXd8HV0qkXpdRow==" 11 | }, 12 | "file-saver": { 13 | "version": "2.0.2", 14 | "resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.2.tgz", 15 | "integrity": "sha512-Wz3c3XQ5xroCxd1G8b7yL0Ehkf0TC9oYC6buPFkNnU9EnaPlifeAFCyCh+iewXTyFRcg0a6j3J7FmJsIhlhBdw==" 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mhtml-to-word", 3 | "version": "1.0.4", 4 | "description": "", 5 | "main": "mhtmlToWord.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/BetterZxx/mhtml-to-word" 12 | }, 13 | "author": "zhangxiang", 14 | "license": "ISC", 15 | "dependencies": { 16 | "baidu-template-pro": "^1.0.0", 17 | "file-saver": "^2.0.2" 18 | } 19 | } 20 | --------------------------------------------------------------------------------