我正在尝试使用 POI 库读取 excel 文件。
try {
File file=new File("C:\\new.xls");
FileInputStream fin = new FileInputStream(file);
//Get the workbook instance for XLS file
HSSFWorkbook workbook = new HSSFWorkbook(fin);
//Get first sheet from the workbook
HSSFSheet sheet = workbook.getSheetAt(0);
//Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while(rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue() + "\t\t");
break;
}
}
System.out.println("");
}
fin.close();
FileOutputStream out =new FileOutputStream(file);
workbook.write(out);
out.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
我在简单的 Java 项目中编写了上面的代码,它工作得很好。 但是,每当我尝试在Servlet中编写相同的代码时,都会出现以下错误。
* *例外
javax.servlet.ServletException:Servlet 执行引发异常
根本原因
java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook com.ReadExcel.read(ReadExcel.java:30) com.ServletDemo.doPost(ServletDemo.java:23)` javax.servlet.http.HttpServlet。服务(HttpServlet.java:641)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)**
请告诉我该怎么做才能消除这些错误。