我一直在尝试使用IntelliJ和Tomcat构建我的第一个web应用程序,其中一项任务是能够上传和处理Excel表格文件。因此,我在网上查找,找到了可以帮助我解析Excel文件的Apache POI库。但是,当我下载了所有必需的jars,并复制和粘贴了一些测试代码并启动服务器时,它在网页上显示了一个错误,http status500,根本原因是: java.lang.ClassNotFoundException: jars
我在其他jars中也遇到过这个问题,但只需将相应的jars放入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文件夹中。
任何想法,为什么会发生这种情况,以及我应该如何解决它是感激的。
发布于 2019-05-23 03:53:47
要使用Apache POI,您需要以下jar文件。
你可以参考下面的链接,我已经回答了几个问题。Unable to read Excel using Apache POI
发布于 2019-05-23 08:33:09
我想我在将jars移到lib目录时遗漏了一些东西,因为我删除了原始文件并重做了cp命令,现在一切都正常了。我用答案来结束这个问题,谢谢你的帮助!
https://stackoverflow.com/questions/56264002
复制相似问题