首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符串中的Docx4j换行符

问字符串中的Docx4j换行符
EN

Stack Overflow用户
提问于 2017-02-24 18:25:00
回答 1查看 1.2K关注 0票数 0

我有这个字符串:

代码语言:javascript
复制
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时,字符串是这样打印的:

代码语言:javascript
复制
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

下面是我获取字符串的地方:

代码语言:javascript
复制
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);  

下面是我添加所有变量的地方:

代码语言:javascript
复制
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);

​

​

这是文档中的字符串:

​

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-27 19:38:40

正如您可能已经了解到的,WordML对制表符和换行符使用了不同的元素(软回车)。

XML看起来像这样:

代码语言:javascript
复制
        <w:r>
            <w:t>Tabulado</w:t>
            <w:tab/>
            <w:t>Tabulado</w:t>
            <w:br/>
            <w:t>Tabulado</w:t>
        </w:r>

以及相应的Java代码:

代码语言:javascript
复制
            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。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42436211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档