如何使用freemarker在docx中生成表格?

时间:2016-01-27 12:24:53

标签: freemarker xdocreport

我正在使用XDocReports和FreeMarker模板引擎来生成基于模板的文档。我想从Java List生成一个表。我的代码如下所示:

    try {
        // 1) Load Docx file by filling Freemarker template engine and cache
        // it to the registry
        InputStream in = ReportWriter.class.getResourceAsStream("/template/teszt_jegyzokonyv_template.docx");
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker);

        FieldsMetadata metadata = report.createFieldsMetadata();
        metadata.load( "testCaseGroups", TestCaseGroup.class, true );
        metadata.load( "testCases", TestCase.class, true );


        // 2) Create context Java model
        IContext context = report.createContext();

        TestCaseGroup testCaseGroup1 = new TestCaseGroup("TestCaseGroup1", Lists.newArrayList(new TestCase("description1"), new TestCase("description3")));
        TestCaseGroup testCaseGroup2 = new TestCaseGroup("TestCaseGroup2", Lists.newArrayList(new TestCase("description1"), new TestCase("description3")));
        context.put("testCaseGroups", Lists.newArrayList(testCaseGroup1, testCaseGroup2));

        // 3) Generate report by merging Java model with the Docx
        OutputStream out = new FileOutputStream(new File("DocxProjectWithFreemarker_Out2.docx"));
        report.process(context, out);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XDocReportException e) {
        e.printStackTrace();
    }

实体看起来像这样:

public class TestCaseGroup {

public String name;

public List<TestCase> testCases;

public TestCaseGroup(String name, List<TestCase> testCases) {
    this.name = name;
    this.testCases = testCases;
}

public String getName() {
    return name;
}

public List<TestCase> getTestCases() {
    return testCases;
    }
}

测试用例

public class TestCase {

public String description;

public TestCase(String description) {
    this.description = description;
}

public String getDescription() {
    return description;
}
}

模板:

template:

但我有以下例外:

  

引起:freemarker.core.InvalidReferenceException:以下内容   已评估为null或缺失:   ==&GT;测试用例

我做错了什么?

0 个答案:

没有答案