菜单

将HTML导出生成word文档,

2018年11月15日 - jQuery

一律、添加maven依赖,导入FreeMarker所需要之jar包

1 <dependency>
2      <groupId>org.freemarker</groupId>
3      <artifactId>freemarker</artifactId>
4      <version>2.3.20</version>
5 </dependency>

拿HTML导出生成word文档,

 

次、定义word模板文件

图片 1

前言:

类开中碰到了特需拿HTML页面的情导出为一个word文档,所以来了这里随笔。

自,项目开而日略紧,第一时间想到的是因此插件,所以百度了生。下面就介绍两只导出word文档的点子。

老三、修改模板xml文件

把正制作的word文件另存为xml文件

图片 2

 

改者xml文件,开发工具我所以的idea,我是将这xml文件在项目中,Ctrl+Alt+L进行格式化,这样文件内容看起结构比较清晰,方便修改,你为堪行使任何xml编辑器等等

拿文档内容被之动态数据,换成freemarker的标识。其实就是Map<String,
Object>中key,如把段然涛换成${name};

图片 3

傅经历是急需循环遍历的,在这边可以循环遍历list,list中蕴藏的还是Map数据,然后再次同平取出

图片 4

折叠起来看就是如此,一定毫无忘记了为止之list标签

图片 5

接通下去就图了,在参加了图片占位的地方,会相同样切片base64编码后的代码,把base64删除替换成${image},图片未确定出略张,也是得循环遍历的

图片 6

这些所有整好之后,模板就炮制完了,修改文件为.ftl即可,然后将模版放入到项目中

图片 7

 

法一:通过jquery.wordexport.js导出word

备注:兼容IE9以上

大概浏览了下jquery.wordexport.js插件的代码,了解及了经该插件可以导出文本和图,而图片首先通过canvas的形式

制图,文本则用再次依赖FileSaver.js插件,FileSaver.js插件则根本透过H5的文书操作新特性new
Blob()和new FileReader()

来贯彻公文的导出。

插件源码:

FileSaver.js

图片 8 1 /*
FileSaver.js 2 * A saveAs() FileSaver implementation. 3 * 1.3.2 4 *
2016-06-16 18:25:19 5 * 6 * By Eli Grey, http://eligrey.com 7 *
License: MIT 8 * See
https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md 9 */ 10
11 /*global self */ 12 /*jslint bitwise: true, indent: 4, laxbreak:
true, laxcomma: true, smarttabs: true, plusplus: true */ 13 14 /*!
@source
http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
15 16 var saveAs = saveAs || (function(view) { 17 “use strict”; 18 // IE
<10 is explicitly unsupported 19 if (typeof view === “undefined” ||
typeof navigator !== “undefined” && /MSIE
[1-9]\./.test(navigator.userAgent)) { 20 return; 21 } 22 var 23 doc =
view.document 24 // only get URL when necessary in case Blob.js hasn’t
overridden it yet 25 , get_URL = function() { 26 return view.URL ||
view.webkitURL || view; 27 } 28 , save_link =
doc.createElementNS(“http://www.w3.org/1999/xhtml”, “a”) 29 ,
can_use_save_link = “download” in save_link 30 , click =
function(node) { 31 var event = new MouseEvent(“click”); 32
node.dispatchEvent(event); 33 } 34 , is_safari =
/constructor/i.test(view.HTMLElement) 35 , is_chrome_ios
=/CriOS\/[\d]+/.test(navigator.userAgent) 36 , throw_outside =
function(ex) { 37 (view.setImmediate || view.setTimeout)(function() { 38
throw ex; 39 }, 0); 40 } 41 , force_saveable_type =
“application/octet-stream” 42 // the Blob API is fundamentally broken as
there is no “downloadfinished” event to subscribe to 43 ,
arbitrary_revoke_timeout = 1000 * 40 // in ms 44 , revoke =
function(file) { 45 var revoker = function() { 46 if (typeof file ===
“string”) { // file is an object URL 47
get_URL().revokeObjectURL(file); 48 } else { // file is a File 49
file.remove(); 50 } 51 }; 52 setTimeout(revoker,
arbitrary_revoke_timeout); 53 } 54 , dispatch = function(filesaver,
event_types, event) { 55 event_types = [].concat(event_types); 56
var i = event_types.length; 57 while (i–) { 58 var listener =
filesaver[“on” + event_types[i]]; 59 if (typeof listener ===
“function”) { 60 try { 61 listener.call(filesaver, event || filesaver);
62 } catch (ex) { 63 throw_outside(ex); 64 } 65 } 66 } 67 } 68 ,
auto_bom = function(blob) { 69 // prepend BOM for UTF-8 XML and text/*
types (including HTML) 70 // note: your browser will automatically
convert UTF-16 U+FEFF to EF BB BF 71 if
(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type))
{ 72 return new Blob([String.fromCharCode(0xFEFF), blob], {type:
blob.type}); 73 } 74 return blob; 75 } 76 , FileSaver = function(blob,
name, no_auto_bom) { 77 if (!no_auto_bom) { 78 blob =
auto_bom(blob); 79 } 80 // First try a.download, then web filesystem,
then object URLs 81 var 82 filesaver = this 83 , type = blob.type 84 ,
force = type === force_saveable_type 85 , object_url 86 ,
dispatch_all = function() { 87 dispatch(filesaver, “writestart progress
write writeend”.split(” “)); 88 } 89 // on any filesys errors revert to
saving with object URLs 90 , fs_error = function() { 91 if
((is_chrome_ios || (force && is_safari)) && view.FileReader) { 92 //
Safari doesn’t allow downloading of blob urls 93 var reader = new
FileReader(); 94 reader.onloadend = function() { 95 var url =
is_chrome_ios ? reader.result :
reader.result.replace(/^data:[^;]*;/, ‘data:attachment/file;’); 96
var popup = view.open(url, ‘_blank’); 97 if(!popup) view.location.href
= url; 98 url=undefined; // release reference before dispatching 99
filesaver.readyState = filesaver.DONE; 100 dispatch_all(); 101 }; 102
reader.readAsDataURL(blob); 103 filesaver.readyState = filesaver.INIT;
104 return; 105 } 106 // don’t create more object URLs than needed 107
if (!object_url) { 108 object_url = get_URL().createObjectURL(blob);
109 } 110 if (force) { 111 view.location.href = object_url; 112 } else
{ 113 var opened = view.open(object_url, “_blank”); 114 if (!opened) {
115 // Apple does not allow window.open, see
https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
116 view.location.href = object_url; 117 } 118 } 119
filesaver.readyState = filesaver.DONE; 120 dispatch_all(); 121
revoke(object_url); 122 } 123 ; 124 filesaver.readyState =
filesaver.INIT; 125 126 if (can_use_save_link) { 127 object_url =
get_URL().createObjectURL(blob); 128 setTimeout(function() { 129
save_link.href = object_url; 130 save_link.download = name; 131
click(save_link); 132 dispatch_all(); 133 revoke(object_url); 134
filesaver.readyState = filesaver.DONE; 135 }); 136 return; 137 } 138 139
fs_error(); 140 } 141 , FS_proto = FileSaver.prototype 142 , saveAs =
function(blob, name, no_auto_bom) { 143 return new FileSaver(blob,
name || blob.name || “download”, no_auto_bom); 144 } 145 ; 146 // IE
10+ (native saveAs) 147 if (typeof navigator !== “undefined” &&
navigator.msSaveOrOpenBlob) { 148 return function(blob, name,
no_auto_bom) { 149 name = name || blob.name || “download”; 150 151 if
(!no_auto_bom) { 152 blob = auto_bom(blob); 153 } 154 return
navigator.msSaveOrOpenBlob(blob, name); 155 }; 156 } 157 158
FS_proto.abort = function(){}; 159 FS_proto.readyState =
FS_proto.INIT = 0; 160 FS_proto.WRITING = 1; 161 FS_proto.DONE = 2;
162 163 FS_proto.error = 164 FS_proto.onwritestart = 165
FS_proto.onprogress = 166 FS_proto.onwrite = 167 FS_proto.onabort =
168 FS_proto.onerror = 169 FS_proto.onwriteend = 170 null; 171 172
return saveAs; 173 }( 174 typeof self !== “undefined” && self 175 ||
typeof window !== “undefined” && window 176 || this.content 177 )); 178
// `self` is undefined in Firefox for Android content script context
179 // while `this` is nsIContentFrameMessageManager 180 // with an
attribute `content` that corresponds to the window 181 182 if (typeof
module !== “undefined” && module.exports) { 183 module.exports.saveAs =
saveAs; 184 } else if ((typeof define !== “undefined” && define !==
null) && (define.amd !== null)) { 185 define([], function() { 186
return saveAs; 187 }); 188 } View
Code

jquery.wordexport.js

图片 9 1 if
(typeof jQuery !== “undefined” && typeof saveAs !== “undefined”) { 2
(function($) { 3 $.fn.wordExport = function(fileName) { 4 fileName =
typeof fileName !== ‘undefined’ ? fileName : “jQuery-Word-Export”; 5 var
static = { 6 mhtml: { 7 top: “Mime-Version: 1.0\nContent-Base: ” +
location.href + “\nContent-Type: Multipart/related;
boundary=\”NEXT.ITEM-BOUNDARY\”;type=\”text/html\”\n\n–NEXT.ITEM-BOUNDARY\nContent-Type:
text/html; charset=\”utf-8\”\nContent-Location: ” + location.href +
“\n\n<!DOCTYPE html>\n<html>\n_html_</html>”, 8
head: “<head>\n<meta http-equiv=\”Content-Type\”
content=\”text/html;
charset=utf-8\”>\n<style>\n_styles_\n</style>\n</head>\n”,
9 body: “<body>_body_</body>” 10 } 11 }; 12 var options =
{ 13 maxWidth: 624 14 }; 15 // Clone selected element before
manipulating it 16 var markup = $(this).clone(); 17 18 // Remove hidden
elements from the output 19 markup.each(function() { 20 var self =
$(this); 21 if (self.is(‘:hidden’)) 22 self.remove(); 23 }); 24 25 //
Embed all images using Data URLs 26 var images = Array(); 27 var img =
markup.find(‘img’); 28 for (var i = 0; i < img.length; i++) { 29 //
Calculate dimensions of output image 30 var w = Math.min(img[i].width,
options.maxWidth); 31 var h = img[i].height * (w / img[i].width);
32 // Create canvas for converting image to data URL 33 var canvas =
document.createElement(“CANVAS”); 34 canvas.width = w; 35 canvas.height
= h; 36 // Draw image to canvas 37 var context =
canvas.getContext(‘2d’); 38 context.drawImage(img[i], 0, 0, w, h); 39
// Get data URL encoding of image 40 var uri =
canvas.toDataURL(“image/png/jpg”); 41 $(img[i]).attr(“src”,
img[i].src); 42 img[i].width = w; 43 img[i].height = h; 44 // Save
encoded image to array 45 images[i] = { 46 type:
uri.substring(uri.indexOf(“:”) + 1, uri.indexOf(“;”)), 47 encoding:
uri.substring(uri.indexOf(“;”) + 1, uri.indexOf(“,”)), 48 location:
$(img[i]).attr(“src”), 49 data: uri.substring(uri.indexOf(“,”) + 1) 50
}; 51 } 52 53 // Prepare bottom of mhtml file with image data 54 var
mhtmlBottom = “\n”; 55 for (var i = 0; i < images.length; i++) { 56
mhtmlBottom += “–NEXT.ITEM-BOUNDARY\n”; 57 mhtmlBottom +=
“Content-Location: ” + images[i].location + “\n”; 58 mhtmlBottom +=
“Content-Type: ” + images[i].type + “\n”; 59 mhtmlBottom +=
“Content-Transfer-Encoding: ” + images[i].encoding + “\n\n”; 60
mhtmlBottom += images[i].data + “\n\n”; 61 } 62 mhtmlBottom +=
“–NEXT.ITEM-BOUNDARY–“; 63 64 //TODO: load css from included
stylesheet 65 66 //var styles=’ /* Font Definitions
*/@font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1
1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3
680460288 22 0 262145 0;} @font-face{font-family:”Cambria
Math”;panose-1:2 4 5 3 5 4 6 3 2
4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0
0 0 0 0 0;} @font-face{font-family:”\@宋体”;panose-1:2 1 6 0 3 1 1 1 1
1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3
680460288 22 0 262145 0;}/* Style Definitions */p.MsoNormal,
li.MsoNormal,
div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:””;margin:0cm;margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:14.0pt;font-family:宋体;mso-bidi-font-family:宋体;}p.MsoHeader,
li.MsoHeader,
div.MsoHeader{mso-style-noshow:yes;mso-style-priority:99;mso-style-link:”页眉
Char”;margin:0cm;margin-bottom:.0001pt;text-align:center;mso-pagination:widow-orphan;layout-grid-mode:char;font-size:9.0pt;font-family:宋体;mso-bidi-font-family:宋体;}p.MsoFooter,
li.MsoFooter,
div.MsoFooter{mso-style-noshow:yes;mso-style-priority:99;mso-style-link:”页脚
Char”;margin:0cm;margin-bottom:.0001pt;mso-pagination:widow-orphan;layout-grid-mode:char;font-size:9.0pt;font-family:宋体;mso-bidi-font-family:宋体;}p.MsoAcetate,
li.MsoAcetate,
div.MsoAcetate{mso-style-noshow:yes;mso-style-priority:99;mso-style-link:”批注框文本
Char”;margin:0cm;margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:9.0pt;font-family:宋体;mso-bidi-font-family:宋体;}span.Char{mso-style-name:”页眉
Char”;mso-style-noshow:yes;mso-style-priority:99;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:页眉;font-family:宋体;mso-ascii-font-family:宋体;mso-fareast-font-family:宋体;mso-hansi-font-family:宋体;}span.Char0{mso-style-name:”页脚
Char”;mso-style-noshow:yes;mso-style-priority:99;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:页脚;font-family:宋体;mso-ascii-font-family:宋体;mso-fareast-font-family:宋体;mso-hansi-font-family:宋体;}span.Char1{mso-style-name:”批注框文本
Char”;mso-style-noshow:yes;mso-style-priority:99;mso-style-unhide:no;mso-style-locked:yes;mso-style-link:批注框文本;font-family:宋体;mso-ascii-font-family:宋体;mso-fareast-font-family:宋体;mso-hansi-font-family:宋体;}p.msochpdefault,
li.msochpdefault,
div.msochpdefault{mso-style-name:msochpdefault;mso-style-unhide:no;mso-margin-top-alt:auto;margin-right:0cm;mso-margin-bottom-alt:auto;margin-left:0cm;mso-pagination:widow-orphan;font-size:10.0pt;font-family:宋体;mso-bidi-font-family:宋体;}span.msonormal0{mso-style-name:msonormal;mso-style-unhide:no;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;font-size:10.0pt;mso-ansi-font-size:10.0pt;mso-bidi-font-size:10.0pt;mso-ascii-font-family:”Times
New Roman”;mso-hansi-font-family:”Times New
Roman”;mso-font-kerning:0pt;}/* Page Definitions */ @page
WordSection1{size:595.3pt 841.9pt;margin:72.0pt 90.0pt 72.0pt
90.0pt;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;}div.WordSection1{page:WordSection1;}’;
67 68 var styles=””; 69 70 // Aggregate parts of the file together 71
var fileContent = static.mhtml.top.replace(“_html_”,
static.mhtml.head.replace(“_styles_”, styles) +
static.mhtml.body.replace(“_body_”, markup.html())) + mhtmlBottom; 72
73 // Create a Blob with the file contents 74 var blob = new
Blob([fileContent], { 75 type: “application/msword;charset=utf-8” 76
}); 77 saveAs(blob, fileName + “.doc”); 78 }; 79 })(jQuery); 80 } else {
81 if (typeof jQuery === “undefined”) { 82 console.error(“jQuery Word
Export: missing dependency (jQuery)”); 83 } 84 if (typeof saveAs ===
“undefined”) { 85 console.error(“jQuery Word Export: missing dependency
(FileSaver.js)”); 86 } 87 } View
Code

插件调用:

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title>生成word文档</title>
 6 </head>
 7 <body lang=ZH-CN style='tab-interval:21.0pt'>
 8 <div class="word">
 9     <p align="center">10 </div>
11 <input type="button" value="导出word">
12 <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
13 <script type="text/javascript" src="js/FileSaver.js"></script>
14 <script type="text/javascript" src="js/jquery.wordexport.js"></script>
15 <script>
16     $(function(){
17         $("input[type='button']").click(function(event) {
18             $(".word").wordExport('生成word文档');
19         });
20     })
21 </script>
22 </body>
23 </html>

直白调用wordExport()接口就可以导出word文档,传的参数为导出的word文件称。

补充:

透过我们例行写的外联样式设置样式是杯水车薪的,通过个人的实践发现需要写内联样式才会奏效,而单位为亟需以word的布

单位pt设置。

万一jquery.wordexport.js插件是如果部署了只style样式让咱加样式设置的:

图片 10

可个人执行了生,设置的样式也束手无策生效,只能通过内联设置才生效。

截图:

图片 11图片 12

季、代码编写

对接下便代码有了,编写FreeMarkerUtil工具类,并测试

  1 package com.cccuu.project.utils;
  2 
  3 import freemarker.template.Configuration;
  4 import freemarker.template.DefaultObjectWrapper;
  5 import freemarker.template.Template;
  6 import freemarker.template.TemplateExceptionHandler;
  7 import org.apache.log4j.Logger;
  8 import sun.misc.BASE64Encoder;
  9 
 10 import java.io.*;
 11 import java.util.*;
 12 
 13 /*******************************************
 14  *
 15  * @Package com.cccuu.project.utils
 16  * @Author duan
 17  * @Date 2018/7/27 11:51
 18  * @Version V1.0
 19  *******************************************/
 20 public class FreeMarkerUtil {
 21 
 22     private static Logger log = Logger.getLogger(FreeMarkerUtil.class);
 23     private static final String ENCODING = "UTF-8";
 24     private static Configuration cfg = new Configuration();
 25 
 26     //初始化cfg
 27     static {
 28         //设置模板所在文件夹
 29         cfg.setClassForTemplateLoading(FreeMarkerUtil.class, "/templates/word");
 30         // setEncoding这个方法一定要设置国家及其编码,不然在ftl中的中文在生成html后会变成乱码
 31         cfg.setEncoding(Locale.getDefault(), ENCODING);
 32         // 设置对象的包装器
 33         cfg.setObjectWrapper(new DefaultObjectWrapper());
 34         // 设置异常处理器,这样的话就可以${a.b.c.d}即使没有属性也不会出错
 35         cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
 36 
 37     }
 38 
 39     //获取模板对象
 40     public static Template getTemplate(String templateFileName) throws IOException {
 41         return cfg.getTemplate(templateFileName, ENCODING);
 42     }
 43 
 44     /**
 45      * 据数据及模板生成文件
 46      * @param data             Map的数据结果集
 47      * @param templateFileName ftl模版文件名
 48      * @param outFilePath      生成文件名称(可带路径)
 49      */
 50     public static File crateFile(Map<String, Object> data, String templateFileName, String outFilePath) {
 51         Writer out = null;
 52         File outFile = new File(outFilePath);
 53         try {
 54             // 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致
 55             Template template = getTemplate(templateFileName);
 56             if (!outFile.getParentFile().exists()) {
 57                 outFile.getParentFile().mkdirs();
 58             }
 59             out = new OutputStreamWriter(new FileOutputStream(outFile), ENCODING);
 60             // 处理模版
 61             template.process(data, out);
 62             out.flush();
 63             log.info("由模板文件" + templateFileName + "生成" + outFilePath + "成功.");
 64         } catch (Exception e) {
 65             log.error("由模板文件" + templateFileName + "生成" + outFilePath + "出错");
 66             e.printStackTrace();
 67         } finally {
 68             try {
 69                 if (out != null) {
 70                     out.close();
 71                 }
 72             } catch (IOException e) {
 73                 log.error("关闭Write对象出错", e);
 74                 e.printStackTrace();
 75             }
 76         }
 77         return outFile;
 78     }
 79 
 80     //获得图片的base64码
 81     public static String getImageBase(String src) throws Exception {
 82         if (src == null || src == "") {
 83             return "";
 84         }
 85         File file = new File(src);
 86         if (!file.exists()) {
 87             return "";
 88         }
 89         InputStream in = null;
 90         byte[] data = null;
 91         try {
 92             in = new FileInputStream(file);
 93             data = new byte[in.available()];
 94             in.read(data);
 95             in.close();
 96         } catch (IOException e) {
 97             e.printStackTrace();
 98         }
 99         BASE64Encoder encoder = new BASE64Encoder();
100         return encoder.encode(data);
101     }
102 
103     public static void main(String[] args) {
104         try {
105             Map<String, Object> data = new HashMap<String, Object>();
106             data.put("name", "段然涛");
107             data.put("sex", "男");
108             data.put("birthday", "1994-03-14");
109             data.put("phone", "17737138812");
110             data.put("address", "河南省许昌市");
111             data.put("school", "江西科技师范大学");
112             List<Map<String, String>> educations = new ArrayList<Map<String, String>>();
113             Map<String, String> paramsMap = new HashMap<String, String>();
114             paramsMap.put("school", "禹州一高");
115             paramsMap.put("startDate", "2008-09");
116             paramsMap.put("endDate", "2012-06");
117             paramsMap.put("person", "李磊");
118             Map<String, String> paramsMap2 = new HashMap<String, String>();
119             paramsMap2.put("school", "江西科技师范大学");
120             paramsMap2.put("startDate", "2012-09");
121             paramsMap2.put("endDate", "2016-07");
122             paramsMap2.put("person", "李杰");
123             educations.add(paramsMap);
124             educations.add(paramsMap2);
125             data.put("educations", educations);
126             List<String> images = new ArrayList<String>();
127             images.add(getImageBase("C:/Users/Administrator/Desktop/图片/timg.jpg"));
128             images.add(getImageBase("C:/Users/Administrator/Desktop/图片/timg11.jpg"));
129             data.put("images", images);
130             crateFile(data, "文档1.ftl", "C:/Users/Administrator/Desktop/文档/简历.doc");
131         } catch (Exception e) {
132             e.printStackTrace();
133         }
134     }
138 }

效仿二:通过百度js模板引擎生成word文档

最主要是经js模板设置相应之竹签,然后XDoc.to(baidu.template())导出word,而由此百度js模板引擎的利是吗得以导出PDF文件。

完整demo:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="UTF-8">
 5     <script type="text/javascript" src="http://www.xdocin.com/xdoc.js"></script>
 6     <script type="text/javascript" src="http://www.xdocin.com/baiduTemplate.js"></script>
 7     <style>
 8         .head{
 9             font-size: 29px;
10             display: block;
11         }
12         .content{
13             display: block;
14         }
15     </style>
16 </head>
17 <body>
18 <input type="button" onclick="gen('pdf')" value="生成PDF"/>
19 <input type="button" onclick="gen('docx')" value="生成Word"/>
20 <br/>
21 <script id="tmpl" type="text/html">
22     <xdoc version="A.3.0">
23         <body>
24         <para heading="1" lineSpacing="28">
25             <text class="head" valign="center" fontName="标宋" fontSize="29"><%=title%></text>
26         </para>
27         <para>
28             <img  src="<%=img%>" sizeType="autosize"/>
29         </para>
30         <para lineSpacing="9">
31             <text class="content" fontName="仿宋" fontSize="18"><%=content%></text>
32         </para>
33         </body>
34     </xdoc>
35 </script>
36 <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
37 <script type="text/javascript">
38     var type="docx";//pdf
39     var data = {
40         title: "导出"+type+"文件",
41         img: "http://www.wordlm.com/uploads/allimg/130101/1_130101000405_1.jpg",
42         content: "我这样就可以导出"+type+"格式的文件了,是不是很方便",
43     };
44     function renderTemplate(){
45         var template=$("#tmpl").html();
46         var html=template.replace(/<%=title%>/,data.title)
47                 .replace(/<%=img%>/,data.img)
48                 .replace(/<%=content%>/,data.content);
49         $("body").append(html);
50     }
51     renderTemplate();
52     function gen(type) {
53         XDoc.to(baidu.template('tmpl', data), type, {}, "_blank");
54     }
55     console.log('http://www.xdocin.com/xml.html');
56 </script>
57 </body>
58 </html>  

此处自己经过renderTemplate函数叫js模板渲染到HTML中,实现了文件的显示和导出内容之结缘。而因此地导出的word文档是索要特地设置样式的,所以于页面样式展示下我们可以通过加加.class的法门设置。

附部分导出word文档样式设置:

图片 13

 

截图:

图片 14图片 15

 

又多参考:

FileSave.js:https://github.com/eligrey/FileSaver.js

百度导出文档模板:http://www.xdocin.com/xml.html

 

http://www.bkjia.com/HTML5/1217833.htmlwww.bkjia.comtruehttp://www.bkjia.com/HTML5/1217833.htmlTechArticle将HTML导出生成word文档, 前言:
项目支付被遇见了需用HTML页面的情节导出为一个word文档,所以有矣这边随笔。
当然,项目开而日有…

 五、最后效果

图片 16

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图