Eclipse 專案引入 POI 依賴,運行報錯 NoClassDefFoundError: com.jacob.com.ComThread 或檔案路徑 D;/ 錯誤。
jacob.jar 加入 Classpath,並將 jacob-1.20-x64.dll (或 x86) 放入 java.library.path (如 System32、專案根目錄或 -Djava.library.path=.)。XWPFDocument) 純 Java 實作,**不需要 Jacob**。String htmlfile = "D;/"; // 錯誤:分號非路徑分隔符
// 正確
String htmlfile = "D:/";
// 或 Windows 雙反斜線
String htmlfile = "D:\\\\";
// 僅需 poi-ooxml 依賴
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
XWPFRun run = p.createRun();
run.setText("Hello POI Word!");
run.setBold(true);
run.setFontSize(16);
try (FileOutputStream out = new FileOutputStream("D:/output.docx")) {
doc.write(out);
}
doc.close();
.docx (OOXML) 格式,避免 Jacob/COM 相容性地獄。<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
XWPFDocument template = new XWPFDocument(new FileInputStream("template.docx"))) 填充資料。