Java把word轉HTML格式,兩種方式
方式一:
maven引入依賴,pom.xml
<dependency><groupId>e-iceblue</groupId><artifactId>spire.office.free</artifactId><version>5.3.1</version>
</dependency>
然后代碼讀取DOC內容,保存成HTML,然后再讀取HTML。
(input.doc這個要輸入完整路徑,例如D:/input.doc)
java:
Document doc = new Document();
doc.loadFromFile("input.doc", FileFormat.Doc);
doc.saveToFile("output.html", FileFormat.Html);
String htmlContent = Files.readString(Paths.get("output.html"));
------------------
方式二:
另外一種方式,先轉換成DOCX,然后再提取HTML
pom.xml
<dependencies><!-- Apache POI --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.2</version> <!-- 使用最新版本或適合你項目的版本 --></dependency>
</dependencies>
java:
XWPFDocument docx = new XWPFDocument(new FileInputStream("input.doc"));
ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();
Document.save(htmlStream, SaveFormat.HTML);
String html = htmlStream.toString();