我有一个代码,可以打开Words文档并执行更新,然后使用POI打印其内容。每次我尝试打开或创建Words文档时,都会返回错误。
代码如下:
package read;
import java.io. * ;
import org.apache.poi.hwpf. * ;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
//import com.sun.java.util.jar.pack.Package.File;
public class ReadDocFile {
public static void main(String[] args) {
File file = null;
WordExtractor extractor = null;
try {
file = new File("c:\\ilham\\ilham.docx");
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
HWPFDocument document = new HWPFDocument(fis);
extractor = new WordExtractor(document);
String[] fileData = extractor.getParagraphText();
for (int i = 0; i < fileData.length; i++) {
if (fileData[i] != null) System.out.println(fileData[i]);
}
}
catch(Exception exep) {
exep.printStackTrace();
}
}
}
这是错误:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/compress/archivers/zip/ZipFile
at org.apache.poi.openxml4j.opc.OPCPackage.create(OPCPackage.java:366)
atorg.apache.poi.xwpf.usermodel.XWPFDocument.newPackage(XWPFDocument.java:157)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>
(XWPFDocument.java:148)
at read.test.main(test.java:21)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.compress.archivers.zip.ZipFile
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more