赞
踩
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.1</version>
</dependency>
public void test1(HttpServletRequest request, HttpServletResponse response){ TableStyle tableStyle = new TableStyle(); tableStyle.setBackgroundColor("F2F2F2"); tableStyle.setAlign(STJc.CENTER); Style style = StyleBuilder.newBuilder().buildBold().buildFontSize(10).build(); RowRenderData header = RowRenderData.build( new TextRenderData("序号", style), new TextRenderData("表名", style), new TextRenderData("描述", style)); header.setRowStyle(tableStyle); Map<String, Object> mapp = new HashMap<>(); List<RowRenderData> list = new ArrayList<>(); List<Map<String, Object>> mapList = ***Service.test1(); for (Map<String, Object> map : mapList) { RowRenderData rowRenderData = RowRenderData.build( map.get("xh").toString(), map.get("name").toString(), map.get("comment") != null ? map.get("comment").toString() : null ); list.add(rowRenderData); } // tables 就是模板定义的那个 mapp.put("tables", new MiniTableRenderData(header, list)); try { // 模板 XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\***\\Desktop\\bbbb.docx").render(mapp); // 生成的文件 FileOutputStream out = new FileOutputStream("C:\\Users\\***\\Desktop\\out_.docx"); template.write(out); out.flush(); out.close(); template.close(); InputStream fis = null; OutputStream toClient = null; File file = new File("C:\\Users\\***\\Desktop\\out_.docx"); fis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); String newWordName = URLEncoder.encode("单表模板", "utf-8"); response.addHeader("Content-Disposition", "attachment;filename=" + newWordName); response.setContentType("application/octet-stream;charset=utf-8"); response.addHeader("Content-Length", "" + file.length()); toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); System.out.println("---------完成---------"); } catch (Exception e) { e.printStackTrace(); } }
-----需要2个模板-----
△1122.docx△
△5566.docx△
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException { // 加粗、字体大小为10 Style style = StyleBuilder.newBuilder().buildBold().buildFontSize(10).build(); RowRenderData header = RowRenderData.build( new TextRenderData("字段名", style), new TextRenderData("类型", style), new TextRenderData("长度", style), new TextRenderData("描述", style)); List<Map<String, Object>> list = new ArrayList<>(); List<Map<String, Object>> mapList = ***Service.test(); // 根据某值分组;可以根据具体业务情况,自行修改,下面代码可有可无。 Map<String, List<Map<String, Object>>> groupedMap = mapList.stream() .collect(Collectors.groupingBy(map -> map.get("tname").toString())); for (String key : groupedMap.keySet()) { List<Map<String, Object>> maps = groupedMap.get(key); Map<String, Object> mapp = new HashMap<>(); List<RowRenderData> listOne = new ArrayList<>(); for (Map<String, Object> map : maps) { RowRenderData rowRenderData = RowRenderData.build( map.get("cname").toString(), map.get("ctype").toString(), map.get("clength") != null ? map.get("clength").toString() : null, map.get("ccomment") != null ? map.get("ccomment").toString() : null ); listOne.add(rowRenderData); } mapp.put("tname", ObjectUtils.isNotEmpty(maps.get(0).get("tcomment")) ? maps.get(0).get("tcomment") : key); MiniTableRenderData miniTable = new MiniTableRenderData(header, listOne); miniTable.setWidth(15.22f); // 1122.docx模板的单个表格+数据 mapp.put("table", miniTable); list.add(mapp); } try { download(request, response, "模板数据.docx", list); } catch (Exception e) { e.printStackTrace(); } }
public static void download(HttpServletRequest request, HttpServletResponse response, String newWordName, List<Map<String, Object>> list) throws IOException { Map<String, Object> map = new HashMap<>(); DocxRenderData info = new DocxRenderData(new File("C:\\Users\\***\\Desktop\\1122.docx"), list); // 将每个单表格放到 5566.docx模板 map.put("tables", info); // 展示多个表格的模板 XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\***\\Desktop\\5566.docx").render(map); //输出文件 FileOutputStream out = new FileOutputStream("C:\\Users\\***\\Desktop\\out_.docx"); template.write(out); out.flush(); out.close(); template.close(); InputStream fis = null; OutputStream toClient = null; File file = new File("C:\\Users\\***\\Desktop\\out_.docx"); fis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); // 解决中文乱码 newWordName = URLEncoder.encode(newWordName, "utf-8"); response.addHeader("Content-Disposition", "attachment;filename=" + newWordName); response.setContentType("application/octet-stream;charset=utf-8"); response.addHeader("Content-Length", "" + file.length()); toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); System.out.println("---------完成---------"); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。