POI-tl 知识整理:整理1 -> 利用模板向word中写入数据

1 文本传值

    @Test
    public void testText() throws Exception {
        XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates.docx");
        Map<String, Object> map = new HashMap<>();
        map.put("title", "Hi, girl");
        template.render(map);

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output.docx");
        template.writeAndClose(fileOutputStream);

        template.close();
    }

2 对象传值

public class Student {
    private  String name;
    private  int age;
    private  String sex;

    // getter and setter

}

    @Test
    public void testObject() throws Exception{
        // 数据可以是对象
        Student student = new Student();
        student.setName("小蟹");
        student.setAge(20);
        student.setSex("男");

        XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates1.docx");
        Map<String, Object> map = new HashMap<>();
        map.put("name", student.getName());
        map.put("age", student.getAge());
        map.put("sex", student.getSex());
        template.render(map);

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_object.docx");
        template.writeAndClose(fileOutputStream);

        template.close();
    }

3 Map 传值

    // Map 传值
    @Test
    public void testMapping() throws Exception{
        // 数据可以是Map集合
        Map<String, Object> data = new HashMap<>();
        data.put("name", "小草");
        data.put("age", 22);
        data.put("sex", "女");

        XWPFTemplate template = XWPFTemplate.compile("D:\\Idea-projects\\POI_word\\templates1.docx");

        Map<String, Object> map = new HashMap<>();
        map.put("map", data);
        XWPFTemplate render = template.render(map);

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\Idea-projects\\POI_word\\output_map.docx");
        template.writeAndClose(fileOutputStream);

        template.close();  // 一定要记得关闭
    }

 

相关推荐

  1. springboot使用poi-tl动态填充word模板

    2024-01-14 01:22:04       40 阅读
  2. 【Node.js】笔记整理 1 - 基础知识

    2024-01-14 01:22:04       38 阅读
  3. C语言:数组、字符串知识整理

    2024-01-14 01:22:04       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-14 01:22:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-14 01:22:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-14 01:22:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-14 01:22:04       20 阅读

热门阅读

  1. [蓝桥杯 2015 省 A] 饮料换购

    2024-01-14 01:22:04       40 阅读
  2. AcWing:4965. 三国游戏

    2024-01-14 01:22:04       40 阅读
  3. 【Kotlin】中英数字混合等多种情况下的排序方式

    2024-01-14 01:22:04       42 阅读
  4. OLAP型数据库 ClickHouse的简介 应用场景 优势 不足

    2024-01-14 01:22:04       44 阅读
  5. xtu-c语言考试复习

    2024-01-14 01:22:04       41 阅读
  6. C#的索引和范围运算符的用法

    2024-01-14 01:22:04       29 阅读
  7. 一个Pytorch 的简单的分类本地图片的训练AI例子

    2024-01-14 01:22:04       40 阅读
  8. 50天精通Golang(第16天)

    2024-01-14 01:22:04       44 阅读
  9. Linux上对大于2T的硬盘分区

    2024-01-14 01:22:04       39 阅读
  10. 常用电容功能以及型号

    2024-01-14 01:22:04       43 阅读
  11. Python装饰器管理类和函数

    2024-01-14 01:22:04       48 阅读