一、poi-tl官网
1. 地址:
https://deepoove.com/poi-tl/
2. 版本:1.5 版本
3. 引入Maven
com.deepoove >poi-tl1.5.1
二、单个表格在word导出
1. 模板必须是docx,也就是:
2. 模板
3. 代码
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); Mapmapp = new HashMap<>(); List list = new ArrayList<>(); List
4. 效果
三、多个表格在word里导出
1. 模板必须是docx
2. 模板
-----需要2个模板-----
△1122.docx△
△5566.docx△
3. 代码
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
public static void download(HttpServletRequest request, HttpServletResponse response, String newWordName, List> list) throws IOException { Map 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("---------完成---------"); }
还没有评论,来说两句吧...