【发布时间】:2019-10-09 08:46:58
【问题描述】:
我一直在尝试使用 IntelliJ 和 Tomcat 构建我的第一个 Web 应用程序,其中一项任务是能够上传和处理 Excel 工作表文件。于是,我上网查了一下,发现可以帮助我解析 Excel 文件的 Apache POI 库。但是当我下载了所有需要的jar并复制粘贴了一些测试代码,并启动服务器时,它在网页上显示http状态500的错误,根本原因是:java.lang.ClassNotFoundException:org.apache.poi .openxml4j.opc.internal.marshallers.PackagePropertiesMarshaller$NamespaceImpl.
我遇到过其他jar的问题,但是都通过将相应的jar放在tomcat的lib文件夹中解决了,除了这个。
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
public class ExcelParser {
private String pathname;
public ExcelParser(String pathname) {
this.pathname = pathname;
}
public void parse() {
try {
FileInputStream file = new FileInputStream(new File("/Users/JohnDoe/Desktop/test.xlsx"));
//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
//Check the cell type and format accordingly
switch (cell.getCellType()) {
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}
System.out.println();
}
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我只是在测试 Excel 解析的功能,所以不用担心路径名。
顺便说一句,我可以看到这个(内部)类是在 poi-ooxml4-4.1.0.jar 中声明的,它也包含在我的 Tomcat lib 文件夹中。
感谢任何想法为什么会发生这种情况,以及我应该如何解决它。
【问题讨论】:
标签: java tomcat servlets apache-poi classnotfoundexception