POI-tl 知识整理:整理2 -> 标签

1 文本标签

{{var}}

数据模型:

  • String :文本

  • TextRenderData :有样式的文本

  • HyperlinkTextRenderData :超链接和锚点文本

  • Object :调用 toString() 方法转化为文本

代码示例:

    @Test
    public void testTextLabel() throws Exception{
        Student student = new Student();
        student.setName("小蟹");
        student.setAge(20);
        student.setSex("男");

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

        Map<String, Object> map = new HashMap<>();
        map.put("name",new TextRenderData("Eff000", student.getName()));
        map.put("link", new HyperlinkTextRenderData("链接", "http://www.baidu.com") );
        map.put("anchor", new HyperlinkTextRenderData("回到最顶端", "anchor: appendix1"));

        XWPFTemplate render = template.render(map);

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

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

 链式代码示例:

    @Test
    public void testTextLabel() throws Exception{

        Student student = new Student();
        student.setName("小蟹");
        student.setAge(20);
        student.setSex("男");

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

        Map<String, Object> map = new HashMap<>();

        /*
        * 可以使用链式写法:
        * */
        // 可以通过这种方式设置样式,在下方可以直接get对应的样式
        //Style style = new Style();
        //style.setStrike(true);
        //style.setUnderlinePatterns(UnderlinePatterns.SINGLE);
        //style.setVertAlign(String.valueOf(VerticalAlign.SUPERSCRIPT));

        map.put("name", Texts.of(student.getName()).color("FF0000").bold().fontSize(20).fontFamily("楷体").italic().create());
        map.put("link", Texts.of("链接").color("FF0000").link("http://bilibili.com").create());
        map.put("anchor", Texts.of("回到最顶端").color("8E6000").italic().anchor("anchor: appendix1").create());
        XWPFTemplate render = template.render(map);

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

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

运行结果:

2 图片标签

图片标签以@开始:{{@var}}

数据模型:

  • String :图片url或者本地路径,默认使用图片自身尺寸

  • PictureRenderData

  • ByteArrayPictureRenderData

  • FilePictureRenderData

  • UrlPictureRenderData

推荐使用工厂 Pictures 构建图片模型。

示例代码:

    @Test
    public void testImgLabel() throws Exception{

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

        Map<String, Object> map = new HashMap<>();

        // 1. 指定本地路径图片
        map.put("img", "D:\\Idea-projects\\POI_word\\girl1.jpg");
        // 2. 指定在线图片
        map.put("girlOnline", "https://c-ssl.duitang.com/uploads/blog/202108/21/20210821132505_66c30.jpg");
        // 3.指定本地路径图片, 并设置大小
        map.put("imgSize", Pictures.ofLocal("D:\\Idea-projects\\POI_word\\girl3.jpg").size(100, 100).create());

        // 4. 图片流
        map.put("StreamImg", Pictures.ofStream(new FileInputStream("D:\\Idea-projects\\POI_word\\girl1.jpg"), PictureType.JPEG)
                .size(300, 250).create());


        // 5. 网络图片(注意网络耗时对系统可能的性能影响)
        map.put("urlImg", Pictures.ofUrl("https://c-ssl.duitang.com/uploads/blog/202108/21/20210821132505_66c30.jpg")
                .size(300, 250).create());


        // 6. java图片
        BufferedImage bufferImage = new BufferedImage(300, 250, BufferedImage.TYPE_INT_RGB);

        // 首先需要填充 bufferImage(这一部分根据自身需要展示的图,填充bufferImage)
        // 获取 Graphics2D 对象
        Graphics2D g2d = bufferImage.createGraphics();

        // 绘制红色背景
        g2d.setColor(Color.RED);
        g2d.fillRect(0, 0, bufferImage.getWidth(), bufferImage.getHeight());

        // 绘制黑色文本
        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Arial", Font.BOLD, 20));
        ((Graphics2D) g2d).drawString("Hello World!", 50, 120);

        // 释放 Graphics2D 对象资源
        g2d.dispose();

        map.put("buffered_image", Pictures.ofBufferedImage(bufferImage, PictureType.JPEG)
                .size(300, 250).create());
        XWPFTemplate render = template.render(map);

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

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

3 表格标签

表格标签以#开始:{{#var}}

数据模型: 

·TableRenderData

推荐使用工厂 Tables 、 Rows 和 Cells 构建表格模型。

 

3.1 基础表格示例

    @Test
    public void testTableLabel() throws Exception{

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

        Map<String, Object> map = new HashMap<>();

        // 推荐使用工厂 Tables 、 Rows 和 Cells 构建表格模型。

        // 1. 基础表格示例
        TableRenderData tableRenderData = Tables.of(new String[][]{
                new String[]{"00", "01"},
                new String[]{"10", "11"},
        }).border(BorderStyle.DEFAULT).create();

        map.put("table0", tableRenderData);

        XWPFTemplate render = template.render(map);

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

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

3.2 表格样式示例

    @Test
    public void testTableLabel() throws Exception{

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

        Map<String, Object> map = new HashMap<>();

        // 推荐使用工厂 Tables 、 Rows 和 Cells 构建表格模型。

 
        // 2. 表格样式示例
        RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF").textBold().bgColor("4472C4")
                .center().rowExactHeight(3.0).create();
        RowRenderData row1 = Rows.create("张三", "本科");
        RowRenderData row2 = Rows.create("李四", "硕士");
        TableRenderData tableRenderData1 = Tables.create(row0, row1, row2);
        map.put("table1", tableRenderData1);

        XWPFTemplate render = template.render(map);

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

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

3.3 表格合并示例

    @Test
    public void testTableLabel() throws Exception{

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

        Map<String, Object> map = new HashMap<>();

        // 推荐使用工厂 Tables 、 Rows 和 Cells 构建表格模型。

        // 可以通过这种方式设置单元格样式
        CellStyle cellStyle = new CellStyle();
        cellStyle.setBackgroundColor("006400");
        // 3. 表格合并示例
        RowRenderData row3 = Rows.of("列0", "列1", "列2").center().bgColor(cellStyle.getBackgroundColor()).create();
        RowRenderData row4 = Rows.create("没有数据", null, null);

        //来指定合并规则
        //这里的 (1, 0) 表示第一行第一列的单元格,(1, 2) 表示第一行第三列的单元格
        MergeCellRule rule = MergeCellRule.builder().map(
                MergeCellRule.Grid.of(1, 0), MergeCellRule.Grid.of(1, 2)).build();
        //将合并规则应用到表格中
        TableRenderData tableRenderData2 = Tables.of(row3, row4).mergeRule(rule).create();
        map.put("table2", tableRenderData2);

        XWPFTemplate render = template.render(map);

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

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

4 列表标签

列表标签以*开始:{{*var}}

数据模型:

  • List<String>

  • NumberingRenderData

推荐使用工厂 Numberings 构建列表模型。

代码示例:

    @Test
    public void testListLabel() throws Exception{

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

        Map<String, Object> map = new HashMap<>();

        //推荐使用工厂 Numberings 构建列表模型
        NumberingRenderData numberingRenderData = Numberings.of(LOWER_ROMAN)  // 可以有多种有序、无序编号方式
                .addItem("列表1")
                .addItem("列表2")
                .addItem("列表2")
                .create();
        map.put("list", numberingRenderData);

        XWPFTemplate render = template.render(map);

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

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

运行结果:

5 区块对标签

区块对由前后两个标签组成,开始标签以?标识,结束标签以/标识:{{?sections}}{{/sections}}

5.1 False 或 空集合

如果区块对的值是 null 、false 或者空的集合,位于区块中的所有文档元素将不会显示,这就等同于if语句的条件为 false。

5.2 非False 且不是集合

如果区块对的值不为 null 、 false ,且不是集合,位于区块中的所有文档元素会被渲染一次,这就等同于if语句的条件为 true。

    @Test
    public void testSectionLabel() throws Exception{

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

        Map<String, Object> map = new HashMap<>();

        HashMap<String, HashMap<String, String>> data = new HashMap<>();
        HashMap<String, String> dataMin = new HashMap<>();
        dataMin.put("name", "xiexu");
        data.put("person", dataMin);
        map.put("person",data.get("person"));

        XWPFTemplate render = template.render(map);

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

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

 

5.3 非空集合

如果区块对的值是一个非空集合,区块中的文档元素会被迭代渲染一次或者N次,这取决于集合的大小,类似于foreach语法。

6 嵌套标签

嵌套又称为导入、包含或者合并,以+标识:{{+var}}

数据模型:

·DocxRenderData

推荐使用工厂 Includes 构建嵌套模型。

代码示例:

public class AddrModel {
    public String addr;

    public AddrModel(String addr) {
        this.addr = addr;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }
}

    @Test
    public void testQiantaoLabel() throws Exception{

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

        Map<String, Object> map = new HashMap<>();

        ArrayList<AddrModel> list = new ArrayList<>();

        list.add(new AddrModel("Beijing,China"));
        list.add(new AddrModel("Shanghai,China"));


        map.put("nested", Includes.ofLocal("D:\\Idea-projects\\POI_word\\sub.docx").setRenderModel(list).create());

        XWPFTemplate render = template.render(map);

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

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

运行结果:

  • 30
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小蟹呀^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值