• POI-TL制作word


    本文相当于笔记,主要根据官方文档Poi-tl Documentationpoi-tl的使用(最全详解)_JavaSupeMan的博客-CSDN博客文章进行学习(上班够用)

    1. @Data
    2. @AllArgsConstructor
    3. @NoArgsConstructor
    4. @ToString
    5. @EqualsAndHashCode
    6. public class Author {
    7. private String name;
    8. private String sex;
    9. }
    1. @Data
    2. @AllArgsConstructor
    3. @NoArgsConstructor
    4. @ToString
    5. @EqualsAndHashCode
    6. public class Student implements Serializable {
    7. private String name;
    8. private String startTime;
    9. private Author author;
    10. private String img;
    11. }

    两个实体类

    下边测试类

    1. @SpringBootTest
    2. @RunWith(SpringRunner.class)
    3. @Slf4j
    4. public class Demo {
    5. @Test
    6. public void Tx01() throws IOException {
    7. Map datas = new HashMap();
    8. // 对象形式 存储对象
    9. Student student = new Student("华", "2023-09-02 20:41:10",
    10. new Author("张三", "男"),null);
    11. datas.put("student",student);
    12. // 处理文字样式(下划线等)
    13. // Style underlineStyle = Style.builder().underline().build();
    14. datas.put("var",Texts.of("hello").bold().italic().create());
    15. // 图片 http://deepoove.com/images/icecream.png
    16. // datas.put("image","http://deepoove.com/images/icecream.png");
    17. // datas.put("svg","http://deepoove.com/images/icecream.png");
    18. // datas.put("svg",Pictures.ofUrl("http://deepoove.com/images/icecream.png")
    19. // .size(100,100).create());
    20. datas.put("image", Pictures.ofUrl("https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg")
    21. .size(400, 300).create());
    22. // 表格 基本操作
    23. //RowRenderData就是指定每一行数据的,可以去官网查阅,这里相当于设置了三行,row0就是表头,row1,row2是表内容
    24. RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")
    25. .bgColor("4472C4").center().create();
    26. RowRenderData row1 = Rows.create("李四", "博士");
    27. RowRenderData row2 = Rows.create("李四", "博士");
    28. MergeCellRule rule1 = MergeCellRule.builder()
    29. .map(MergeCellRule.Grid.of(0,0),MergeCellRule.Grid.of(0,1)).build();//合并单元格(第几行第几列)
    30. datas.put("var1", Tables.of(row0, row1,row2).mergeRule(rule1).create());
    31. //合并单元格
    32. RowRenderData roW0 = Rows.of("列0", "列1", "列2", "列3")
    33. .bgColor("4472C4")
    34. .textColor("7F7f7F").textFontFamily("Hei").textFontSize(15).center().create();
    35. RowRenderData roW1 = Rows.create("没有数据", null, null, null);//第一行
    36. //合并第几行第几列 到 第几行第几列
    37. MergeCellRule rule = MergeCellRule.builder()
    38. .map(MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(1, 2)).build();
    39. datas.put("var2", Tables.of(roW0, roW1).mergeRule(rule).create());
    40. // 动态生成表格 列表循环 Configure.builder()
    41. Student s1 = new Student("张三", "2023-09-02 20:41:10",
    42. new Author("赵", "男"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg");
    43. Student s2 = new Student("李四", "2023-09-02 20:41:10",
    44. new Author("钱", "男"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg");
    45. Student s3 = new Student("王五", "2023-09-02 20:41:10",
    46. new Author("孙", "女"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664016579528.png");
    47. Student s4 = new Student("赵六", "2023-09-02 20:41:10",
    48. new Author("李", "女"),"https://ee19hua.oss-cn-beijing.aliyuncs.com/images/2022/09/24/1664008700443.jpg");
    49. List studentList = new ArrayList<>();
    50. studentList.add(s1);
    51. studentList.add(s2);
    52. studentList.add(s3);
    53. studentList.add(s4);
    54. datas.put("lists",studentList);
    55. // 插件列表,可以去官网查看,有列循环,还有行循环,这里是行循环实例
    56. LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
    57. //这里可以指定一个 config 类,用来指定一些规则,也可以改变模板中{{}}的这种格式
    58. Configure config = Configure.builder()
    59. .bind("lists", policy).build();
    60. // 列表
    61. datas.put("listString",studentList);
    62. //模板地址
    63. String templateFilePath = "d:/data/template/";
    64. //生成文件的保存地址
    65. String destFilePath = "d:/data/template/word";
    66. // 生成word
    67. XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "test1.docx",config);
    68. // XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "test3.docx",config);
    69. compile.render(datas);
    70. //输出为文件,指定输出文件名
    71. compile.writeToFile(destFilePath+"out_test01.docx");
    72. System.out.println("运行完成-------------------------------------");
    73. }
    74. }

    word模板

     生成结果:

     

    如果想生成柱状图等,按照官网进行学习即可,以上两篇(官网,和博客学习够用)

  • 相关阅读:
    计数排序与基数排序
    Java中线程的六种状态
    【博客547】keepalived实现vip的原理剖析
    智能科技助力服装业:商品计划管理系统的革命性变革
    广义模态控制实例
    嵌入式基础
    配置OSPF包文分析和验证
    网络安全专业术语中英对照指南
    mysql协议
    牛客网刷题-(2)
  • 原文地址:https://blog.csdn.net/weixin_50769390/article/details/132644222