draco1023 / poi-tl-ext

Extensions for poi-tl
Apache License 2.0
211 stars 53 forks source link

html无法渲然模版 #37

Closed hubStarzy closed 2 years ago

hubStarzy commented 2 years ago

老哥,帮忙看一下我这个是什么原因

主要代码

HtmlRenderPolicy htmlRenderPolicy = new HtmlRenderPolicy();

    Configure configure = Configure.builder()
            .bind("key", htmlRenderPolicy)
            .build();
    XWPFTemplate template = XWPFTemplate.compile(new File("./first.docx"), configure);
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("bigTitle", "大标题");
    data.put("smallTitle", "小标题");
    data.put("tableBigTitle", "表格标题");
    data.put("key", "<p>Hello <b>world<b>!</p>");
    template.render(data);

pom文件

<dependency>

        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.11.1</version>
    </dependency>
    <dependency>
        <groupId>io.github.draco1023</groupId>
        <artifactId>poi-tl-ext</artifactId>
        <version>0.3.13-beta</version>
    </dependency>

报错信息 13:16:43.728 [main] INFO com.deepoove.poi.render.processor.ElementProcessor - Start render Template {{bigTitle}}, Sign:�, policy:TextRenderPolicy 13:16:43.731 [main] INFO com.deepoove.poi.render.processor.ElementProcessor - Start render Template {{smallTitle}}, Sign:�, policy:TextRenderPolicy 13:16:43.732 [main] INFO com.deepoove.poi.render.processor.ElementProcessor - Start render Template {{tableBigTitle}}, Sign:�, policy:TextRenderPolicy 13:16:43.732 [main] INFO com.deepoove.poi.render.processor.ElementProcessor - Start render Template {{key}}, Sign:�, policy:HtmlRenderPolicy Exception in thread "main" com.deepoove.poi.exception.RenderException: Unable to render template {{key}} at com.deepoove.poi.policy.AbstractRenderPolicy.reThrowException(AbstractRenderPolicy.java:83) at com.deepoove.poi.policy.AbstractRenderPolicy.render(AbstractRenderPolicy.java:65) at com.deepoove.poi.render.processor.ElementProcessor.visit(ElementProcessor.java:76) at com.deepoove.poi.render.processor.ElementProcessor.visit(ElementProcessor.java:67) at com.deepoove.poi.template.run.RunTemplate.accept(RunTemplate.java:64) at com.deepoove.poi.render.processor.DocumentProcessor.visit(DocumentProcessor.java:95) at com.deepoove.poi.template.run.RunTemplate.accept(RunTemplate.java:64) at com.deepoove.poi.render.processor.DocumentProcessor.lambda$process$0(DocumentProcessor.java:59) at java.util.ArrayList.forEach(ArrayList.java:1259) at com.deepoove.poi.render.processor.DocumentProcessor.process(DocumentProcessor.java:59) at com.deepoove.poi.render.DefaultRender.renderTemplate(DefaultRender.java:82) at com.deepoove.poi.render.DefaultRender.render(DefaultRender.java:64) at com.deepoove.poi.XWPFTemplate.render(XWPFTemplate.java:209) at com.scan.common.util.wordDomo.main(wordDomo.java:26) Caused by: java.lang.NullPointerException at org.ddr.poi.html.HtmlRenderContext.(HtmlRenderContext.java:194) at org.ddr.poi.html.HtmlRenderPolicy.doRender(HtmlRenderPolicy.java:156) at com.deepoove.poi.policy.AbstractRenderPolicy.render(AbstractRenderPolicy.java:62) ... 12 more

hubStarzy commented 2 years ago

我找到原因了,可能是我的这个word文档格式有点儿问题,导致doRender的时候

HtmlRenderContext htmlRenderContext = new HtmlRenderContext(context);

CTPageMar pgMar = sectPr.getPgMar(); long top = POIXMLUnits.parseLength(pgMar.xgetTop());

方法init的时候获取页面样式的时候导致null 新建了一个word文档再次执行就好了。