Search code examples
javaapache-poixssf

Apache POI in Java causes trouble (XSSFWorkbook)


I am actually trying to read an XLS file with Apache POI, but my code somehow doesn't work. IntelliJ tells me that, on line 28, creating the XSSFWorkbook causes the trouble. Would you have a brief look and maybe answer if you are in this?

package Parse;

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;

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
public class poi {

    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream(new File("C:\\Users\\jd\\Desktop\\test\\VW_XML\\in_xls.xlsx"));

            //Create workbook instance
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //read sheet
            XSSFSheet sheet = workbook.getSheetAt(0);

            //iterate rows
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                // for each row all columns
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();

                    //check cell type
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "t");
                            break;
                    }
                }
                System.out.println("");

            }
            file.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Solution

  • Place all the following jars in BuildPath and run!

    enter image description here