6

I would like to convert either an html or xhtml document (preferably with styles) to Microsoft .doc and/or .docx format.

There seem to be plenty of examples for doing this the other way around but I haven't found any useful examples for converting to ms document formats.

Can anyone point me to an api or provide an example for doing this please

Many thanks

3
  • do you want to have an actual .doc file or a file that can be opened with word and/or modified/printed?
    – Liviu T.
    Mar 23, 2011 at 9:58
  • actual doc or docx. It looks like it would probably be easier to create a docx but doc would provide better backwards compatability
    – Edd
    Mar 23, 2011 at 10:06
  • There could be something in open/libre office to do this
    – vickirk
    Mar 23, 2011 at 10:30

5 Answers 5

2

docx4j 2.8.0 supports converting XHTML documents and fragments to docx content. Disclosure: I wrote some of the code.

2
1

Yet another solution would be to use jodconverter which seems to basic html to doc conversion... it doesn't claim to do it well though

2
  • Is you find anything which convert both doc and docx into html? Please show me sample code if any? Jan 28, 2012 at 17:13
  • I haven't been able to commit myself to this yet i'm afraid
    – Edd
    Jan 30, 2012 at 10:29
1

I tried docjx4j API 2.8.1 and it works like wonder. It had ConvertinXHTMLinFile and it works fine. If anyone wants the code I will post it.

Here is the link that helped me : ConvertInXHTMLFile

0

In order to work with Microsoft Documents you'll likely have to take a deeper look at Apache's POI Library.

Nevertheless creating .doc files with styling from (X)HTML requires some effort.

2
  • Indeed, Apache POI is pretty much the only port of call. It doesn't do the conversion (which is very, very far from trivial), but it does allow you to create .doc documents.
    – AndyT
    Mar 23, 2011 at 10:13
  • I'm after something that will do the donkey work for me really. Something that will parse and xhtml document and output a doc or docx document
    – Edd
    Mar 23, 2011 at 10:14
0

I've been spending a little time looking into docx4j. It seems to provide nice ways for creating html documents from docx but I can't see anything for the other way round.

At the moment this is still looking like the easiest method as it's just working with jaxb objects (I think).

1
  • I have converted docx4j's HTML (as opposed to any old bit of HTML) back to docx. As a result there is a little stuff in docx4j to help you: classes such as org.docx4j.model.properties.run.Bold have constructors which take a CSSValue. Other bits aren't there (eg the code which uses that, the code for converting an HTML table, and the code to import an image). Mar 24, 2011 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.