描述
【Poi-tl 多级列表渲染】poi-tl版本v1.8.2
效果
文章图片
模板 problem.docx
{{?listMap}}
{{no}}. {{content}}
{{*pictures}}
{{/listMap}}
代码实现
public class PoitlTest {public static void main(String[] args) throws IOException {
test();
}private static void test() throws IOException {
// 创建列表策略
NumbericRenderPolicy policy = new NumbericRenderPolicy();
ConfigureBuilder builder = Configure.newBuilder();
// 为pictures指定列表策略
builder.bind("pictures", policy).build();
// 多级列表数据,由于列表中可能是Text或者Picture,所以类型采用RenderData
List detailList = new ArrayList<>();
List detailList2 = new ArrayList<>();
TextRenderData textRenderData1 = new TextRenderData();
TextRenderData textRenderData2 = new TextRenderData();
textRenderData1.setText("选项1");
textRenderData2.setText("选项2");
BufferedImage bufferedImage1 = ImageIO.read(new URL("https://dev-nfs.sf8cloud.com/receive/EZ-NFS-22a36097-47de-451a-877d-730598ba9166.png"));
BufferedImage bufferedImage2 = ImageIO.read(new URL("https://dev-nfs.sf8cloud.com/receive/EZ-NFS-c8a9d8fd-9a4b-4376-a44c-00e87030aa71.gif"));
PictureRenderData pictureRenderData1 = new PictureRenderData(100, 120, ".png",bufferedImage1);
PictureRenderData pictureRenderData2 = new PictureRenderData(100, 120, ".png",bufferedImage2);
detailList.add(textRenderData1);
detailList.add(pictureRenderData1);
detailList2.add(textRenderData2);
detailList2.add(pictureRenderData2);
// 使用NumbericRenderData包装列表数据,这样模板中可以直接使用{{*pictures}}机型渲染;
NumbericRenderData numbericRenderData1 = new NumbericRenderData(Pair.of(STNumberFormat.DECIMAL, ""),detailList);
NumbericRenderData numbericRenderData2 = new NumbericRenderData(Pair.of(STNumberFormat.DECIMAL, ""),detailList2);
TestPoiMain main = new TestPoiMain();
List
public class TestPoiMain {
List