而不是在 docx 中呈现表格和其他 html 标签,这些标签使用 docx4j-ImportXHTML 保存为纯文本

Instead of rendering tables and other html tags in docx these are saved as plain text using docx4j-ImportXHTML

我想将 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. 使用 ByteArrayStream 而不是 FileOutputStream 即

而不是

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

使用:

fos = new ByteArrayOutputStream();
  1. 添加wordMLPackage.save(实际文件)

完整代码:

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();
            }
        }
    }