gpt4 book ai didi

java - Docx4j 字符串中的换行符

转载 作者:行者123 更新时间:2023-12-02 03:00:31 39 4
gpt4 key购买 nike

我有这个字符串:

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);:

enter image description here

这是文档中的字符串:

enter image description here谢谢。

最佳答案

正如您可能已经发现的那样,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”之类的内容。

或者使用内容控制数据绑定(bind),或导入 XHTML。

关于java - Docx4j 字符串中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42436211/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com