一、poi-tl官网
1. 地址:
2. 版本:1.5 版本
3. 引入Maven
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.1</version>
</dependency>
二、单个表格在word导出
1. 模板必须是docx,也就是:
2. 模板
3. 代码
publicvoidtest1(HttpServletRequest request,HttpServletResponse response){TableStyle tableStyle =newTableStyle();
tableStyle.setBackgroundColor("F2F2F2");
tableStyle.setAlign(STJc.CENTER);Style style =StyleBuilder.newBuilder().buildBold().buildFontSize(10).build();RowRenderData header =RowRenderData.build(newTextRenderData("序号", style),newTextRenderData("表名", style),newTextRenderData("描述", style));
header.setRowStyle(tableStyle);Map<String,Object> mapp =newHashMap<>();List<RowRenderData> list =newArrayList<>();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",newMiniTableRenderData(header, list));try{// 模板XWPFTemplate template =XWPFTemplate.compile("C:\\Users\\***\\Desktop\\bbbb.docx").render(mapp);// 生成的文件FileOutputStream out =newFileOutputStream("C:\\Users\\***\\Desktop\\out_.docx");
template.write(out);
out.flush();
out.close();
template.close();InputStream fis =null;OutputStream toClient =null;File file =newFile("C:\\Users\\***\\Desktop\\out_.docx");
fis =newBufferedInputStream(newFileInputStream(file));byte[] buffer =newbyte[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 =newBufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();System.out.println("---------完成---------");}catch(Exception e){
e.printStackTrace();}}
4. 效果
三、多个表格在word里导出
1. 模板必须是docx
2. 模板
-----需要2个模板-----
△1122.docx△
△5566.docx△
3. 代码
publicvoidtest(HttpServletRequest request,HttpServletResponse response)throwsIOException{// 加粗、字体大小为10Style style =StyleBuilder.newBuilder().buildBold().buildFontSize(10).build();RowRenderData header =RowRenderData.build(newTextRenderData("字段名", style),newTextRenderData("类型", style),newTextRenderData("长度", style),newTextRenderData("描述", style));List<Map<String,Object>> list =newArrayList<>();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 =newHashMap<>();List<RowRenderData> listOne =newArrayList<>();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 =newMiniTableRenderData(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();}}
publicstaticvoiddownload(HttpServletRequest request,HttpServletResponse response,String newWordName,List<Map<String,Object>> list)throwsIOException{Map<String,Object> map =newHashMap<>();DocxRenderData info =newDocxRenderData(newFile("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 =newFileOutputStream("C:\\Users\\***\\Desktop\\out_.docx");
template.write(out);
out.flush();
out.close();
template.close();InputStream fis =null;OutputStream toClient =null;File file =newFile("C:\\Users\\***\\Desktop\\out_.docx");
fis =newBufferedInputStream(newFileInputStream(file));byte[] buffer =newbyte[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 =newBufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();System.out.println("---------完成---------");}
4. 效果
本文转载自: https://blog.csdn.net/m0_48323642/article/details/137674043
版权归原作者 程序汪10100001101 所有, 如有侵权,请联系我们删除。
版权归原作者 程序汪10100001101 所有, 如有侵权,请联系我们删除。