I am trying to read excel file using POI library.
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();
}
I wrote above code in simple Java project and it worked very fine. But whenever I am trying to write same code in Servlet , I am getting following errors.
* *exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
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.service(HttpServlet.java:641) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)**
Please tell me what to do in order to remove these errors.
the exception only tells you that it is not able to find the HSSFWorkbook anywhere (in your classpath)
org/apache/poi jar needs to be in the classpath -> add it.
将poi jar文件放在WEB-INF / lib文件夹中
Create a "Dynamic Web Project" in Eclipse.
Put your source files into the "src" folder of the new project.
Put poi-3.9.jar into this folder in your project: WebContent/WEB-INF/lib/poi-3.9.jar
Right-Click your project and select Build Path -> Configure Build Path... Select the tab "Libraries" and expand the node "Web App Libraries". Here you must see an entry for poi-3.9.jar. If not, there is something wrong with the project setup.
Test your servlet.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.