我有这个字符串:
Prueba
Lista:
- li1
- li2
- li3
- li4
Tabulado
Tabulado Tabulado Tabulado Tabulado
Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado
Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado
但是当我准备 .docx 时,字符串打印如下:
Prueba Listas: - li1 - li2 - li3 - li4 Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado
Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado Tabulado
这是我获取字符串的地方:
String escDesc = report.getDescription();
if (escDesc.contains("\n")){
//escDesc = escDesc.replaceAll("\\n", System.getProperty("line.separator"));
//escDesc = escDesc.replaceAll("\\n", "<w:br/>");
}
escDesc = StringEscapeUtils.escapeXml(escDesc);
valuesAdd.put("DESCRIPCION", escDesc);
这是我添加所有变量的地方:
private void replacePlaceholders()
throws Exception {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
VariablePrepare.prepare(wordMLPackage);
documentPart.variableReplace(valuesAdd);
List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
String xml = null;
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
if (hfp != null) {
HeaderPart headerPart = hfp.getDefaultHeader();
if (headerPart != null) {
xml = XmlUtils.marshaltoString(headerPart.getJaxbElement());
}
Object obj = null;
try {
obj = XmlUtils.unmarshallFromTemplate(xml, valuesAdd);
} catch (JAXBException e) {
e.printStackTrace();
}
// Inject result into docx
headerPart.setJaxbElement((Hdr) obj);
}
}
}
我想保留 \n 但我不知道我现在必须做什么我希望有人能给我一些提示。
这是一个System.out.println(escDesc);
:
这是文档中的字符串:
Thanks.
您可能已经发现,WordML 使用不同的元素来表示制表符和换行符(软回车)。
XML 看起来像:
<w:r>
<w:t>Tabulado</w:t>
<w:tab/>
<w:t>Tabulado</w:t>
<w:br/>
<w:t>Tabulado</w:t>
</w:r>
以及相应的Java代码:
org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();
// Create object for tab (wrapped in JAXBElement)
R.Tab rtab = wmlObjectFactory.createRTab();
JAXBElement<org.docx4j.wml.R.Tab> rtabWrapped = wmlObjectFactory.createRTab(rtab);
// Add it to the run...
run.getContent().add( rtabWrapped);
// Create object for br
Br br = wmlObjectFactory.createBr();
这就是基础知识。您也许可以替换为“TabuladoTabulado”之类的内容。
或者使用内容控制数据绑定,或导入 XHTML。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)