How to convert HTML to a Microsoft Word document ?

帅比萌擦擦* 提交于 2020-01-01 19:26:32

问题


How can I convert HTML from a CKEditor into a Microsoft Word document?


回答1:


CKEditor is just HTML, so what you really should be asking yourself is:

How do I save CKEditor contents as an HTML file and then convert that into a .doc file.

The answer to the first part should be trivial. Protip: You send the contents as a string to your backend and your backend writes that string to an .html file.

As for the conversion, you can try saving the content as a .html file, then opening that in Word and then saving it as a doc. Should be possible although Word is no browser and if your content is complicated, it will fail. You might have some luck automating the process with LibreOffice, which has a command line spell for this.

Also, this has been asked before, just not in this form: Converting HTML to doc(x) and / or PDF




回答2:


I wrote a java method to achieve it using api docx4j-ImportXHTML.

The idea is to create method which takes xhtml, resultant filename and path at which the new file to be saved. This file will process the xhtml and will save it as file with provided filename and at given destination. Check Full code link for complete code.

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).

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


来源:https://stackoverflow.com/questions/20019901/how-to-convert-html-to-a-microsoft-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!