Pregunta

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?

¿Fue útil?

Solución

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

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

Otros consejos

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

Add xmlbeans-xpath.jar to your libraries.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top