Frage

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();
        }
    }
}
War es hilfreich?

Lösung

Place all the following jars in BuildPath and run!

enter image description here

Andere Tipps

How do you add POI to your project? Do you use Maven or something like that? You might be missing some dependencies.

Cut out from my dependency:tree :

org.apache.poi:poi-ooxml:jar:3.10-FINAL:compile
 +- org.apache.poi:poi:jar:3.10-FINAL:compile
 |  \- commons-codec:commons-codec:jar:1.5:compile
 \- org.apache.poi:poi-ooxml-schemas:jar:3.10-FINAL:compile
    \- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile
        \- stax:stax-api:jar:1.0.1:compile

Do you have all that jars in your classpath?

How to read excel(.xlsx) in java using poi?

this link and your comments helped me a lot.

i needed to add more jar files to my project.

poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.7.jar xmlbeans-2.3.0.jar dom4j-1.6.1.jar

ty a lot for replys and have a nice day.

you need to add these jar files ::

CLASSPATH:

“C:\poi-3.9\poi-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;”

“C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;”

“C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;”

Click the below link for the above jar files:: http://www.java2s.com/Open-Source/Java_Free_Code/Database/Download_wca_workbook_assistant_Free_Java_Code.htm

Best of Luck..

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top