20

In order to read an xlsx file I'm using apache POI, I've downloaded the zip and placed the following jsrs in my servlet location webcontent/web-inf/lib and configured build path through eclipse

enter image description here

and my code looks as follows,

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;

File uploadedFile = new File(fpath, fileName);
item.write(uploadedFile);
String mimeType = (Files.probeContentType(uploadedFile.toPath())).toString();
System.out.println(mimeType);
if(mimeType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
{
FileInputStream file = new FileInputStream(uploadedFile);
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    for (int i =0; i < workbook.getNumberOfSheets(); i++)
    {
       XSSFSheet sheet = workbook.getSheetAt(i);
       Iterator<Row> row = sheet.iterator();
       while(row.hasNext()) {
   Iterator<Cell> cellIterator = ((Row) row).cellIterator();
       while(cellIterator.hasNext()) {
       Cell cell1 = cellIterator.next();
       switch(cell1.getCellType()) 
         {
    case Cell.CELL_TYPE_BOOLEAN:
    System.out.print(cell1.getBooleanCellValue() + "\n");
    break;
    case Cell.CELL_TYPE_NUMERIC:
    System.out.print(cell1.getNumericCellValue() + "\n");
    break;
    case Cell.CELL_TYPE_STRING:
    System.out.print(cell1.getStringCellValue() + "\n");
    break;
    }
     }

Though this does not show and errors on eclipse it shows the following errors when I try to run the code

enter image description here

What is my mistake? How to solve this?

4 Answers4

44

You need to add the XML beans dependency to your class path.

The library is usually called xmlbeans-x.x.x.jar

Mena
  • 47,782
  • 11
  • 87
  • 106
  • Okay. But how to add it in eclipse?? –  Apr 15 '14 at 10:51
  • 2
    Manually: get the jar, go to Project, properties, Java build path, add external jar and select the .jar file. – Mena Apr 15 '14 at 10:53
  • I have a Swing app working fine, reading Excel files on Windows but on my Linux Virtual Machine, I get java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException ???? The xmlbeans jar is included. – iltaf khalid Feb 02 '16 at 10:08
8

I have downloaded the latest poi-3.17 binaries and xmlbeans-x.x.x.jar is included in the downloaded package itself.

Attached the screenshots FYR.

Primary jars required for xlsx xmlbeans-x.x.x.jar under the folder ooxml-lib

Suresh
  • 1,491
  • 2
  • 22
  • 27
5

Add xmlbeans-xpath.jar to your libraries.

Jay
  • 9,189
  • 12
  • 56
  • 96
0

It looks as though you might be trying to produce Office's 2007 format with the POI version for the old formats. Use the poi-ooxml jar for the new formats.

Gregor Petrin
  • 2,891
  • 1
  • 20
  • 24