我有这个字符串:
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);

这是文档中的字符串:

谢谢。
发布于 2017-02-27 19:38:40
正如您可能已经了解到的,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。
https://stackoverflow.com/questions/42436211
复制相似问题