实现步骤:
1、首先引入jar包,maven依赖如下,可根据需要选择不同版本
<dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j-ImportXHTML</artifactId> <version>3.0.0</version> </dependency>
2、代码实现
html转word
StopWatch stopWatch = new StopWatch(); stopWatch.start("task1"); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); MainDocumentPart mdp = wordMLPackage.getMainDocumentPart(); String xhtml = "<html><head><title>task1</title></head><body><p>长大上课了!</p></body></html>"; mdp.addAltChunk(AltChunkType.Xhtml, xhtml.getBytes()); WordprocessingMLPackage pkgOut = mdp.convertAltChunks(); XmlUtils.marshaltoString(pkgOut.getMainDocumentPart().getJaxbElement(), true, true) pkgOut.save(new java.io.File(System.getProperty("user.dir") + "/test.docx")); S