100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > POI-TL导出Word文档(支持WPS查看)

POI-TL导出Word文档(支持WPS查看)

时间:2023-09-23 06:53:57

相关推荐

POI-TL导出Word文档(支持WPS查看)

需求:公司原来已有导出word,实现方式是poi;实现复杂且不支持wps,现要求支持wps查看。

poi导出excel比较方便

导出word文档使用POI-TL

poi-tl是一种 "logic-less" 模板引擎,没有复杂的控制结构和变量赋值,只有标签。标签由前后两个大括号组成, {{title}} 是标签, {{?title}} 也是标签, title 是这个标签的名称, ? 标识了标签类型

实现方式如下:

1.模板创建

注意:poi-tl只支持docx格式的文档,不支持doc格式*1.1创建需要导出的模板.docx文件1.2在模板中进行标签渲染:替换需要动态生成的部分可以对文本,图片,表格,列表等进行渲染也可嵌套使用参考/poi-tl/#example-table

以下是创建的模板供参考:

1.1模板创建(moban.docx)

1.2对模板进行渲染(moban.docx)

注意:标签间{{}}不能有空格

2.导出实现

2.1导入jar包

<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.8.2</version></dependency>

2.2将上面渲染好的模板copy到项目下

2.3给模板中添加数据

Map<String, Object> map = new HashMap<String, Object>();//1.文本类型,实现测量仪器前数据+验收评定数据map.put("title1", "Poi-tl 模板引擎工程");map.put("title2", "XXX");map.put("pono":"BA000001");map.put("ponoDesc", "合同名称");。。。。。。//2.表格类型,实现指标信息//2.1默认提供的表格{{#var}},实现了N行N列的样式//2.2默认的表格不支持集合数据循环生成表格,所以通过以下插件实现//2.3集合数据循环表格行--通过HackLoopTableRenderPolicy实现List<ProcessCheckListIndex> processTestIndexList = new ArrayList<ProcessCheckListIndex>();.....processTestIndexList .add(...)//集合中添加对象map.put("table", new MiniTableRenderData(header,list));ConfigureBuilder builder = Configure.newBuilder();HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();builder.bind("processTestIndexList", policy);map.put("processTestIndexList", processTestIndexList);//3.区块对,实现各个指标的详细信息:包括图片列表、文本等//3.1区块对由前后两个标签组成,开始标签以?标识,结束标签以/标识:{{?sections}}{{/sections}}//3.2可以包含多个图片、表格、段落、列表、图表 等//3.3开始和结束标签可以跨多个段落,也可以在同一个段落//3.4如果在表格中使用区块对,开始和结束标签必须在同一个单元格内List<Map<String,Object>> recordTypeInfoList = new ArrayList<Map<String,Object>>();Map<String,Object> recordTypeInfo = new HashMap<String, Object>();//普通文本recordTypeInfo.put("rtiftname", "指标1");recordTypeInfo.put("rtiinstr", "说明");recordTypeInfo.put("rtimeaunit", "mm");recordTypeInfo.put("rtidesignvalue", "10");recordTypeInfo.put("rtideviation", "2");recordTypeInfo.put("rtiother", "无");recordTypeInfo.put("rtiresult", "优良");//4 .图片类 图片列表,图片类型 PictureRenderDataList<Map<String,Object>> images = new ArrayList<Map<String,Object>>();Map<String,Object> mapImg = new HashMap<String, Object>();mapImg.put("image", new PictureRenderData(80, 100, "E:/bg.png"));mapImg.put("r", "\r");//换行images.add(mapImg);if(mapImg!=null&&mapImg.size()>0){recordTypeInfo.put("images", mapImg);}else{recordTypeInfo.put("rtiraid", "无");}recordTypeInfoList.add(recordTypeInfo);map.put("recordTypeInfoList", recordTypeInfoList);//图片的其他类型使用//本地图片//map.put("img", new PictureRenderData(80, 100, "E:/bg.png"));// 图片流//map.put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png")));// 网络图片(注意网络耗时对系统可能的性能影响)//map.put("urlpicture", new PictureRenderData(50, 50, ".png", BytePictureUtils.getUrlBufferedImage("/images/icecream.png")));// java 图片//map.put("bufferimage", new PictureRenderData(80, 100, ".png", bufferImage)));

2.4使用模板并导出文件(下载)

Configure configure = builder.build();String basePath = request.getSession().getServletContext().getRealPath("/");XWPFTemplate template = pile(basePath+"/template/moban1.docx",configure ).render(map);//如果未使用插件//XWPFTemplate template = pile(basePath+"/template/moban1.docx").render(map);String fileName = "验收表.docx";response.setContentType("application/octet-stream");String agent = request.getHeader("User-Agent");boolean isMSIE =((agent!=null&&agent.indexOf("MSIE")!=-1)||(agent!=null&&agent.indexOf("like Gecko")!=-1));if(isMSIE){response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF8")); }else{response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("UTF-8"), "ISO8859-1")); }OutputStream out = response.getOutputStream();BufferedOutputStream bos = new BufferedOutputStream(out);template.write(bos);template.close();bos.flush();bos.close();out.flush();out.close();

至此导出完成,以供参考!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。