RT,耗费了博主半个月的时间才挤出来的成果,在此记录下开发过程。

博主的另一篇文章改用mht模板导出了包含图片的word:

https://blog.csdn.net/u011099093/article/details/85318212

一、创建freemark模板

首先在web項目中指定目錄下創建一個HTML格式的freemarker模板:

<!DOCTYPE html>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"xmlns="http://www.w3.org/TR/REC-html40">
<head><meta charset="UTF-8"><!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="--"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/><m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]--><style><!--/*Page Definitions*/@page WordSection1 {size: 595.3pt 841.9pt;margin: 72.0pt 1.0cm 72.0pt 1.0cm;mso-header-margin: 42.55pt;mso-footer-margin: 49.6pt;mso-paper-source: 0;}div.WordSection1 {page: WordSection1}--></style><title>导出html格式Word演示</title>
</head>
<body>
<div class="WordSection1"><div><h1 style="font-size: 32px;font-weight: bold;padding:0px 4px 0px 0px;text-align:center;margin:0px 0px 20px;"><span style="font-size: 22pt;font-family: 宋体,SimSun;"><strong>Word导出演示</strong></span></h1></div><p style="text-indent: 32px"><span style="font-size:21px;font-family: 宋体">${basicShow!}</span></p><table class="MsoNormalTable"style="mso-cellspacing: 0cm;mso-table-layout-alt: fixed;border:outset black 1.0pt;mso-border-alt: outset black .75pt;mso-yfti-tbllook:1184;mso-padding-alt: 0cm 0cm 0cm 0cm"border="1" cellpadding="0" cellspacing="0" style="border-color:black;"><tbody><tr style="height:39px" class="firstRow"><td width="120" style="padding: 0px 7px;"><p style="text-align: center"><strong><span style="font-size: 16px;font-family: 仿宋;">姓名</span></strong></p></td><td width="120" style="padding: 0px 7px;"><p style="text-align: center"><strong><span style="font-size: 16px;font-family: 仿宋;">年龄</span></strong></p></td><td width="120" style="padding: 0px 7px;"><p style="text-align: center"><strong><span style="font-size: 16px;font-family: 仿宋;">性别</span></strong></p></td></tr><#if lists??><#list rows as lists><tr style="height:39px" class="firstRow"><td width="120" style="padding: 0px 7px;"><p style="text-align: center"><span style="font-size: 16px;font-family: 仿宋;">${rows.name!}</span></p></td><td width="120" style="padding: 0px 7px;"><p style="text-align: center"><span style="font-size: 16px;font-family: 仿宋;">${rows.age!}</span></p></td><td width="120" style="padding: 0px 7px;"><p style="text-align: center"><span style="font-size: 16px;font-family: 仿宋;">${rows.gender!}</span></p></td></tr></#list></#if></tbody></table>
</div>
</body>
</html>

其中控制打開后展示為頁面視圖的是這一段:

<!--[if gte mso 9]>
...
<--主要是下面这一行-->
<w:View>Print</w:View>
...
<![endif]-->

百度了好久都沒有百度到相關模板,就寫了個demo先使用html代碼生成一個word,再在word中修改格式,保存。

然後使用NodePad打開保存后的doc文檔分析其中的結構才拼凑成一個格式比較像樣的模板。

控制頁邊距的是以下這一段:

<!--        /*Page Definitions*/        @page WordSection1 {            size: 595.3pt 841.9pt;            margin: 72.0pt 1.0cm 72.0pt 1.0cm;            mso-header-margin: 42.55pt;            mso-footer-margin: 49.6pt;            mso-paper-source: 0;        }        div.WordSection1 {            page: WordSection1        }        -->
使用${param!}引入參數,使用<#if lists??><#list rows as lists></#list></#if>遍歷多條數據插入到模板中;

二、导入数据到模板并生成word

@ResponseBody@RequestMapping("/exportWord")public Map<String,String> exportWord(HttpServletRequest request){try {//遍历獲取所有參數Enumeration<String> enu = request.getParameterNames();String paraName = null;Map<String, String> parameters = new HashMap<>();while (enu.hasMoreElements()) {paraName = enu.nextElement();parameters.put(paraName, request.getParameter(paraName));}//準備一段假數據Map<String, Object> map = new HashMap<>();map.put("basicShow", "這裏展示一段文字。。。啦啦啦");List<User> list = new ArrayList<>();list.add(new User() {{setAge(18);setGender("男");setName("趙康");}});list.add(new User() {{setAge(34);setGender("男");setName("劉天");}});list.add(new User() {{setAge(23);setGender("女");setName("李逵");}});map.put("lists", list);String path = request.getRealPath("/");//獲取項目的根目錄ServletContext context = request.getServletContext();String filepath = createDoc(path, context, map, "wordOfHtml.ftl", "测试word文档.doc");Map<String, String> resultMap = new HashMap<>();resultMap.put("filepath", filepath);return resultMap;}catch(Exception e){e.printStackTrace();}return null;}private String createDoc(String path, ServletContext context, Map<String, Object> data, String templateName, String docName) {long startTime=System.currentTimeMillis();System.out.println("生成word开始。。。");Configuration configurationc=new Configuration();//设置模板编码格式configurationc.setDefaultEncoding("utf-8");//设置模板存放的路径configurationc.setServletContextForTemplateLoading(context,"freemarkTemplate");Template template=null;String filepath=null;String htmlpath=null;try{//获取模板设置编码类型template=configurationc.getTemplate(templateName,"UTF-8");//设置生成word文件的存放路径filepath= path+"export"+File.separator;//+"test.doc";File file=new File(filepath);if(!file.exists()){
//                file.createNewFile();file.mkdirs();}filepath+=docName;Writer bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath),"utf-8"));
//            BufferedWriter bw=new BufferedWriter(new FileWriter(filepath));//替换模板中的占位符并输出template.process(data,bw, ObjectWrapper.BEANS_WRAPPER);return filepath;}catch (Exception e){e.printStackTrace();}finally {long end=System.currentTimeMillis();System.out.println("用时:"+(end-startTime)/1000+"秒;");System.out.println("生成word结束,开始下载。。。");}return null;}

这里为了测试,我直接造的假数据放到Map中,替换模板中对应的参数;

三、导出word

@ResponseBody@RequestMapping("/downloadWord")public void downloadWord(HttpServletResponse response,String filepath){OutputStream os=null;FileInputStream inputStream=null;System.out.println("开始下载。。。");try{File file=new File(filepath);String filename=new String(file.getName().getBytes("GB2312"),"ISO8859-1");//设置输出文件类型为 word.docresponse.setContentType("application/msword");//设置文件名response.setHeader("Content-Disposition","attachment;filename="+filename);String len=String.valueOf(file.length());response.setHeader("Content-length",len);os=response.getOutputStream();inputStream=new FileInputStream(file);byte[] bytes=new byte[1024];int i;while((i=inputStream.read(bytes))!=-1){os.write(bytes,0,i);}os.flush();boolean b=file.delete();System.out.println(b+"----------");}catch(Exception e){e.printStackTrace();}finally {try {if (inputStream != null) {inputStream.close();}if(os!=null){os.close();}}catch(Exception e){e.printStackTrace();}System.out.println("下载完成。。。");}}

前台js:

$.ajax({url: _ctx+'/report/common/exportWord',data: {},type: 'post',dataType: 'json',success: function (obj) {var filepath=obj.filepath;console.info(filepath)if(filepath!=null){var encode2=encodeURIComponent(filepath);console.info(encode2)console.log(_ctx+'/report/common/downloadWord?'+(new Date().getTime())+"&filepath="+encode2)window.location.href=_ctx+'/report/common/downloadWord?'+(new Date().getTime())+"&filepath="+encode2;}else{alert("下载word失败!");}}})

效果:

使用nodepad打开word,发现其实格式还是html:

所以如果需要什么样式可以直接在word中修改,然后保存,使用记事本打开就可以看到对应的样式,希望对各位小伙伴有所帮助!

-------------------------------------------------华丽的分割线-----------------------------------------------------

(原创文档,转载请注明出处。)

使用freemarker导出html格式的word(调整页边距,页面视图,正常表格样式)相关推荐

  1. html 导出 word 调整页边距

    经分析源码,添加如下CSS代码即可: @page WordSection1{size:595.3pt 841.9pt;margin:30.0pt 30.0pt 30.0pt 30.0pt;mso-he ...

  2. 使用FreeMarker导出固定格式word文档

    使用FreeMarker导出固定格式word文档 一.下载FreeMarker的jar包 下载地址:http://freemarker.org/freemarkerdownload.html,导入项目 ...

  3. Word如何调整页边距

    大家使用Office软件也这么久了,想必,总会遇到一些问题,这里我以我常用的speedoffice将给大家讲一下在Word中如何调整页边距,大家速来围观我的经验啦. 1,首先,请大家打开软件,然后在工 ...

  4. 导出word如何默认打开为页面视图

    最近在做一个商务导出发货单功能,商务要求导出word版,实现之后又反馈说导出的word默认打开是web版式,而不是常用的页面视图,在网上找了很久,最终找到解决方案.现附上代码: //导出word pr ...

  5. speedoffice(Word)如何调整页边距

    大家使用Office软件也这么久了,想必,总会遇到一些问题,这里我以我常用的speedoffice将给大家讲一下在Word中如何调整页边距,大家速来围观我的经验啦. 1.首先,我们用office软件打 ...

  6. word调整页脚距离 顶端和低端的距离(叫页边距)

    1.word调整页脚距离 顶端和低端的距离 不管用啊 2.word调整页面的大小 3.word 调整 长方形 方框的距离 4.页边距

  7. 在html中怎么设置页面边距,在打印网页时怎么设置调整页边距

    在打印网页时怎么设置调整页边距 今天给大家介绍一下在打印网页时怎么设置调整页边距的具体操作步骤. 1. 首先打开电脑,找到想要打印的网页打开. 2. 点打开之后,在页面右上角点击三横图标. 3. 在弹 ...

  8. html设置word页边距,怎么设置Word的页边距

    在内侧和外侧框中. 另外一种情况:单击文件页面设置页边距选项卡, word页边距怎么设置呢? word默认页边距分2003和2007版,Word 2007的上下边距是:2.54厘米;左右边距是:3.1 ...

  9. java word 纸张大小_Java 设置 Word 页边距, 页面大小, 页面方向, 页面边框

    Java 设置 Word 页边距, 页面大小, 页面方向, 页面边框 本文将通过 Java 示例介绍如何设置 Word 页边距 (包括上, 下, 左, 右), 页面大小 (可设置 Letter/A3/ ...

最新文章

  1. 打不开添加删除程序的故障
  2. 满园尽是503,记曾经的一次IIS 7性能考验
  3. python银行排队系统_socket实现银行排队系统
  4. 使用Spring Security 5.0和OIDC轻松构建身份验证
  5. iOS录音后播放声音变小的解决方法
  6. Sublime 插件- px 转rem
  7. Ubuntu16.04 安装CUDA8.0+CUDNN6.0+Tensorflow-GPU1.4版本出现问题解决方案
  8. 当CNI遇上Kata-KataNative的CNI扩展
  9. URI、URL与URN【定义+联系】
  10. 枚举smb共享期间出错_大量三星手机黑屏系统崩溃,客服:闰4月计算出错
  11. 汉字Unicode编码规范
  12. 王道训练营3月12日
  13. vscode unins000.exe报错,尝试在目标目录创造文件时发生错误
  14. 响铃:云+峰会再召开,腾讯云的政企合作玩得怎么样了?
  15. java计算机毕业设计共享充电宝管理系统源码+mysql数据库+系统+lw文档+部署(2)
  16. 大一学生一周十万字爆肝版C语言总结笔记
  17. VUE项目练习大全(附github源码)
  18. PCB工程分享:快速了解PCB设计入门基础知识
  19. css弹性盒模型详解----flex-wrap
  20. 《程序员做饭指南》霸榜 GitHub:不仅有量筒、烧杯,还用上了数学公式?

热门文章

  1. 巴西龟饲养日志----肺炎治疗情况
  2. 28js学习第十一天定时器函数
  3. 技术漫谈之——Jectpack Compose
  4. mysql的partition_MySQL分区(Partition)
  5. 【eclipse】mybatis配置文件创建与mapper接口文件创建
  6. peerDependencies WARNING问题剖析
  7. 云呐医疗行业条码固定资产管理系统
  8. mysql-8.0.28-winx64安装步骤
  9. 江西省信息技术知识竞赛
  10. 山水印|竹林野茶:你喝过的茶,都写在了脸上