【问题标题】:Docx4j unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"p")Docx4j 意外元素(uri:“http://schemas.openxmlformats.org/wordprocessingml/2006/main”,本地:“p”)
【发布时间】:2019-05-22 12:46:33
【问题描述】:

我正在尝试将 html 表格添加到另一个单词表格的单元格中。

  1. 我可以将 html 表格添加到另一个单词表格的单元格中。 (好的)
  2. 我可以生成最后一个word文档调用lastDocument.docx(OK)
  3. 我无法再次加载 WordprocessingMLPackage.load(lastDocument.docx),抛出这个 异常 Docx4j 意外元素 (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", 本地:“p”)

这是我的代码:

Tr workingRow = (Tr) XmlUtils.deepCopy(templateRow);
    List<?> textElements = WMLPackageUtils.getTargetElements(workingRow, Text.class);

    List<Tc> tcList = WMLPackageUtils.getTargetElements(workingRow, Tc.class);
    Tc tc = WMLPackageUtils.getTc(tcList, "${Replace_Tex1}");

    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    XHTMLImporter.setParagraphFormatting(FormattingOption.IGNORE_CLASS);
    XHTMLImporter.setTableFormatting(FormattingOption.IGNORE_CLASS);
    for (Object object : textElements) {
        Text text = (Text) object;
        if (!text.getValue().equals("${Replace_Tex1}"))
            continue;
        String replacementValue = (String) replacements.get(text.getValue());
        //text.setValue(replacementValue);
        R r = (R) text.getParent();
        r.getContent().clear();
        r.getContent().addAll(XHTMLImporter.convert(replacementValue, null));

【问题讨论】:

    标签: html-table openxml docx4j


    【解决方案1】:

    我猜你的问题是:

      r.getContent().addAll(XHTMLImporter.convert(replacementValue, null));
    

    在运行中添加 w:p(段落内容),这是不允许的。

    你可以解压你的docx来查看word/document.xml

    【讨论】:

      【解决方案2】:

      你好,我已经这样修复了我的代码

           R r = (R) text.getParent();
           P paragraphOfText = wordMLPackage.getMainDocumentPart().createParagraphOfText("");
           paragraphOfText.getContent().clear();
      
           r.getContent().clear();eviewtable.getContent().add(new Tr(new Tc(itemTable, new Paragraph())));
           tc.getContent().addAll(XHTMLImporter.convert(replacementValue, null));
           tc.getContent().add(paragraphOfText);
      

      它正在工作 :) 谢谢@JasonPlutext

      【讨论】:

        猜你喜欢
        • 2011-07-09
        • 1970-01-01
        • 2013-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多