如何在 Docx4j 中读取 MS word xml 文件

How to read an MS word xml file in Docx4j

我有问题。

我想用 Java 创建 MS word 文件。

我会制作简单的word文档。

但我不知道如何进行编码来创建具有复杂形状的单词。

使用xml的代码,在我看来好像是可以的。

如何在 docx4j 中使用原始 xml 代码?

我使用了以下代码风格。但是太难了

static String tblXML = "<w:tbl xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
         "xmlns:v=\"urn:schemas-microsoft-com:vml\" " +
         "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " +
         "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">"
         +"<w:tblPr>"
         +"<w:tblStyle w:val=\"a5\" />"
         +"<w:tblW w:w=\"0\" w:type=\"auto\" />"
         +"<w:tblLook w:val=\"04A0\" w:firstRow=\"1\" w:lastRow=\"0\" w:firstColumn=\"1\" w:lastColumn=\"0\" w:noHBand=\"0\" w:noVBand=\"1\" />"
      +"</w:tblPr>"
      +"<w:tblGrid>"
         +"<w:gridCol w:w=\"9224\" />"
      +"</w:tblGrid>"
      +"<w:tr w:rsidR=\"00F327EA\" w:rsidTr=\"0047463F\">"
         +"<w:trPr>"
            +"<w:trHeight w:val=\"1912\" />"
         +"</w:trPr>"
         +"<w:tc>"
            +"<w:tcPr>"
               +"<w:tcW w:w=\"9224\" w:type=\"dxa\" />"
            +"</w:tcPr>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Plan No. </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Inspection Type Special , Regular , Specially assigned inspector , , , </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t>Planning Period 2015</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t>06</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t>17 ~ 2015</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t>06</w:t>"
               +"</w:r>"
               +"<w:r>"
                  +"<w:softHyphen />"
                  +"<w:t xml:space=\"preserve\">18 </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Individual Target Aerodrome (AD), Major International Airport, </w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellStart\" />"
               +"<w:r>"
                  +"<w:t>GimHae</w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellEnd\" />"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\"> </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\">Inspector Primary: </w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellStart\" />"
               +"<w:r>"
                  +"<w:t>superadmin</w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellEnd\" />"
               +"<w:r>"
                  +"<w:t xml:space=\"preserve\"> </w:t>"
               +"</w:r>"
            +"</w:p>"
            +"<w:p w:rsidR=\"00F327EA\" w:rsidRDefault=\"00F327EA\" w:rsidP=\"0047463F\">"
               +"<w:pPr>"
                  +"<w:ind w:firstLineChars=\"850\" w:firstLine=\"1700\" />"
               +"</w:pPr>"
               +"<w:proofErr w:type=\"spellStart\" />"
               +"<w:r>"
                  +"<w:t>Secondary:superadmin</w:t>"
               +"</w:r>"
               +"<w:proofErr w:type=\"spellEnd\" />"
            +"</w:p>"
         +"</w:tc>"
      +"</w:tr>"
   +"</w:tbl>";

您可以很容易地解组这样的字符串。

创建更复杂代码的最简单方法是使用 docx4j 代码生成器。你给它一个示例 docx,它会生成合适的代码(在字符串形式和使用 JAXB 之间选择你的选择 API)。

代码生成器可用 online, or as a Word Add-In