简体   繁体   English

使用JAVA将HTML内容写入MS WORD?

[英]Writing HTML content into MS WORD using JAVA?

I am developing an Exam Software in which I have used Subscript and superscript. 我正在开发使用下标和上标的考试软件。 so I have to store the Questions in HTML in the Database, Now i want to write those questions with HTML tags to the word, I tried Using Apache POI Library, here is the sample text : 所以我必须将HTML中的问题存储在数据库中,现在我想将带有HTML标签的问题写到单词上,我尝试使用Apache POI库,这是示例文本:

  <html>
  <head> </head>
  <body><font face="Shruti"> MY QUESTION </font>
  </body>
  </html> 

but when I am trying to write text in word Document using apache poi , it is showing the HTML tags 但是当我尝试使用apache poi在Word Document中写入文本时,它显示的是HTML标签

You can add your HTML as an AltChunk, and have Word convert it to native docx content when the file is first opened. 您可以将HTML添加为AltChunk,并在第一次打开文件时让Word将其转换为本地docx内容。

If you need to convert to native docx content in Java, you can use docx4j-ImportXHTML 如果您需要转换为Java中的本机docx内容,则可以使用docx4j-ImportXHTML

Disclosure: I manage that repo. 披露:我负责该回购。

Using java api docx4j-ImportXHTML that can be achieved by below method: 使用可以通过以下方法实现的java api docx4j-ImportXHTML

public static void xhtmlToDocx(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();
            }
        }
    }

To add dependency docx4j-ImportXHTML , use(3.3.1 is latest version while I am writing this answer. If you seeing it later use latest stable version of your time). 要添加依赖项docx4j-ImportXHTML ,请使用(我正在编写此答案时,最新版本为3.3.1。如果以后看到它,请使用您所在时间的最新稳定版本)。

<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j-ImportXHTML</artifactId>
    <version>3.3.1</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM