而不是在docx中呈现表和其他html标记,而是使用docx4j-ImportXHTML将它们保存为纯文本

时间:2017-01-25 11:51:04

标签: java docx4j html-to-text

我想将HTML代码呈现给docx。它不是呈现html(即表格格式的表格),而只是将html代码作为纯文本写入其中。我正在使用docx4j-ImportXHTML jar。我使用了here中的代码并将其修改为保存在文件中。

我做错了什么?

public static void xhtmlToDocx(String xhtml, String destinationPath, String fileName)
    {
        File dir = new File (destinationPath);
        File actualFile = new File (dir, fileName);

        WordprocessingMLPackage wordMLPackage = null;
        try
        {
            wordMLPackage = WordprocessingMLPackage.createPackage();
        }
        catch (InvalidFormatException e)
        {

            e.printStackTrace();
        }

        XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
        //XHTMLImporter.setDivHandler(new DivToSdt());
        //OutputStream os = null;
        OutputStream fos = null;
        try
        {
            fos = new FileOutputStream(actualFile);
            wordMLPackage.getMainDocumentPart().getContent().addAll( 
                    XHTMLImporter.convert( xhtml, null) );

            System.out.println(XmlUtils.marshaltoString(wordMLPackage
                    .getMainDocumentPart().getJaxbElement(), true, true));
            // Back to XHTML

            HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
            htmlSettings.setWmlPackage(wordMLPackage);


            // output to an OutputStream.
            //os = new ByteArrayOutputStream();

            // If you want XHTML output
            Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML",
                    true);
            Docx4J.toHTML(htmlSettings, fos, Docx4J.FLAG_EXPORT_PREFER_XSL);
        }
        catch (Docx4JException | FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我更正了我的代码如下:

  1. 使用ByteArrayStream而不是FileOutputStream,即
  2. 而不是

    fos = new FileOutputStream(actualFile);
                wordMLPackage.getMainDocumentPart().getContent().addAll( 
                        XHTMLImporter.convert( xhtml, null) );
    

    使用:

    fos = new ByteArrayOutputStream();
    
    1. 添加wordMLPackage.save(actualFile)
    2. 完整代码

      public static void xhtmlToDocx1(String xhtml, String destinationPath, String fileName)
          {
              File dir = new File (destinationPath);
              File actualFile = new File (dir, fileName);
      
              WordprocessingMLPackage wordMLPackage = null;
              try
              {
                  wordMLPackage = WordprocessingMLPackage.createPackage();
              }
              catch (InvalidFormatException e)
              {
                  e.printStackTrace();
              }
      
      
              XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
      
              OutputStream fos = null;
              try
              {
                  fos = new ByteArrayOutputStream();
      
                  System.out.println(XmlUtils.marshaltoString(wordMLPackage
                          .getMainDocumentPart().getJaxbElement(), true, true));
      
                              HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
                  htmlSettings.setWmlPackage(wordMLPackage);
        Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML",
                          true);
                  Docx4J.toHTML(htmlSettings, fos, Docx4J.FLAG_EXPORT_PREFER_XSL);
                  wordMLPackage.save(actualFile); 
              }
              catch (Docx4JException e)
              {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              finally{
                  try {
                      fos.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }