Java 如何在Servlet中读取Excel文件?

Java 如何在Servlet中读取Excel文件?,java,excel,servlets,Java,Excel,Servlets,我正在尝试使用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 s

我正在尝试使用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();
}
试试看{
文件=新文件(“C:\\new.xls”);
FileInputStream fin=新的FileInputStream(文件);
//获取XLS文件的工作簿实例
HSSF工作手册=新的HSSF工作手册(fin);
//从工作簿中获取第一张工作表
HSSFSheet sheet=工作簿。getSheetAt(0);
//从第一张图纸开始遍历每行
迭代器rowIterator=sheet.Iterator();
while(roweiterator.hasNext())
{
行=行迭代器。下一步();
//对于每一行,遍历每一列
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext())
{
Cell=cellIterator.next();
开关(cell.getCellType())
{
case Cell.Cell\u类型\u布尔值:
System.out.print(cell.getBooleanCellValue()+“\t\t”);
打破
case Cell.Cell\u类型\u数值:
System.out.print(cell.getNumericCellValue()+“\t\t”);
打破
case Cell.Cell\u类型\u字符串:
System.out.print(cell.getRichStringCellValue()+“\t\t”);
打破
}
}
System.out.println(“”);
}
fin.close();
FileOutputStream out=新的FileOutputStream(文件);
练习册。写(出);
out.close();
}
catch(filenotfounde异常)
{
e、 printStackTrace();
} 
捕获(IOE异常)
{
e、 printStackTrace();
}
我在简单的Java项目中编写了上述代码,效果非常好。 但每当我试图在Servlet中编写相同的代码时,就会出现以下错误

**例外情况

javax.servlet.ServletException:servlet执行引发异常

根本原因

java.lang.NoClassDefFoundError:org/apache/poi/hssf/usermodel/HSSFWorkbook ReadExcel.read(ReadExcel.java:30) doPost(ServletDemo.java:23)` javaservlet.http.HttpServlet.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)**


请告诉我如何删除这些错误。

异常只告诉您无法在任何位置(在类路径中)找到HSSF工作簿


org/apache/poi-jar需要在classpath->添加它。

将poi-jar文件放在WEB-INF/lib文件夹中

在Eclipse中创建一个“动态WEB项目”

将源文件放入新项目的“src”文件夹中

将poi-3.9.jar放入项目中的此文件夹: WebContent/WEB-INF/lib/poi-3.9.jar

右键单击项目并选择生成路径->配置生成路径。。。 选择选项卡“库”并展开节点“Web应用程序库”。 在这里,您必须看到poi-3.9.jar的条目。 如果不是,则说明项目设置有问题


测试你的servlet。

我通过“属性->java构建路径->添加外部JAR”添加了POI JAR…您好,先生,我在WEB-INF/lib文件夹中添加了POI JAR,但它仍然给出了相同的错误…我通过“属性->java构建路径->添加外部JAR”添加了POI JAR。。。