Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active November 30, 2021 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/6292eed8c4680f319ff8c909e6728582 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/6292eed8c4680f319ff8c909e6728582 to your computer and use it in GitHub Desktop.
Convert HTML Files to Word DOCX, DOC, DOCM, etc. in Java
// Create a new document
Document doc = new Document();
// Create a document builder
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert HTML
builder.insertHtml("<ul>\r\n" +
"<li>Item1</li>\r\n" +
"<li>Item2</li>\r\n" +
"</ul>");
// Save as DOCX
doc.save("html-string-to-word.docx", SaveFormat.DOCX);
// Load HTML file using Document class
Document document = new Document("template.html");
// Convert HTML file to Word DOCX format
document.save("output.docx", SaveFormat.DOCX);
// Create and initialize URL
URL oracleURL = new URL("https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html");
// Get web page as input stream
InputStream is = oracleURL.openStream();
// Initialize HTML load options
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Load stream into Document object
Document doc = new Document(is, htmloptions);
// Save as DOCX
doc.save("output.docx", SaveFormat.DOCX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment