Poi-tl 多级列表渲染

描述 【Poi-tl 多级列表渲染】poi-tl版本v1.8.2
效果 Poi-tl 多级列表渲染
文章图片

模板 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 amapList = new ArrayList<>(); Map bmap = new HashMap(16); bmap.put("pictures", numbericRenderData1); bmap.put("no", "A"); bmap.put("content", "这是一个题目"); amapList.add(bmap); Map bmap2 = new HashMap(16); bmap2.put("pictures", numbericRenderData2); bmap2.put("no", "B"); bmap2.put("content", "这是第二个个题目"); amapList.add(bmap2); main.setListMap(amapList); XWPFTemplate template = XWPFTemplate.compile("D:\\problem.docx", builder.build()).render(main); FileOutputStream out = new FileOutputStream("D:\\output.docx"); template.write(out); }}

public class TestPoiMain { List listMap; public List getListMap() { return listMap; }public void setListMap(List listMap) { this.listMap = listMap; } }

    推荐阅读