我正在尝试使用docx4j生成报告。 我已经设法创建了一个单词模板,并根据我的数据创建了xml绑定。
我可以使用 docx4j 基于此生成 docx 输出,但现在我需要做一些条件样式 - 基于值将单元格着色为某种颜色。
我已经查看并尝试实现 ContentControlBindingExtensions 示例。
但是我不断收到一个错误,说在类加载器中找不到XsltCustomFinisher。
我的代码:
private static void bindXMLToTemplateFinisher(WordprocessingMLPackage wordMLPackage, String xml) throws Docx4JException, SAXException, IOException {
XPathFactoryUtil.setxPathFactory(new net.sf.saxon.xpath.XPathFactoryImpl());
Document xmlDoc = convertStringToXMLDocument(xml);
//ClassLoader classloader = Thread.currentThread().getContextClassLoader();
//String url = classloader.getResource("XsltFinishers/XsltFinisherCustom.xslt").getPath();
Docx4jProperties.setProperty("docx4j.model.datastorage.XsltFinisher.xslt", "XsltFinisherReport.xslt");
Docx4J.bind(wordMLPackage, xmlDoc, Docx4J.FLAG_BIND_INSERT_XML | Docx4J.FLAG_BIND_BIND_XML, null, new XsltProviderImpl(), "XsltFinisherReport.xslt", null);
}
我希望能够设置一个 xsltfinisher 文件,但它似乎无法找到我尝试使用的文件。
任何了解如何在docx4j中设置和使用自定义xslt整理器的帮助将不胜感激。
要从基础知识开始,请将 XsltFinisherReport.xslt 放在您的类路径上,就像它所说的那样?在 IDE 中,这可能意味着将包含它的目录添加到类路径中。
您可以为 org.docx4j.utils.ResourceUtils 打开日志记录以获得更多诊断。
根据 XsltFinisher 中的文档:
* As an optional step after binding, apply user-defined XSLT to transform
* this content control.
*
* A template is attached to a content control (a repeat/condition/or normal bind),
* using tag od:call=XYZ where XYZ is the template to call.
*
* For example, by placing this on a repeat, a table tow could be coloured red if
* its contents met some condition.
例如 xslt,请参阅 https://github.com/plutext/docx4j/tree/master/docx4j-samples-resources/src/main/resources