Harshal
Harshal

Reputation: 41

Getting exception while reading xlsx file using POI jars in java

I am unable to read xlsx file using java, getting Exception as at the line where I am creating an object XSSFWorkbook(fis);

HashMap<Short, String> keyMap = new HashMap<Short, String>();
HashMap<String, String> valueMap = null;
ArrayList<HashMap<String, String>> rowList = new ArrayList<HashMap<String, String>>();
try {
    FileInputStream fis = new FileInputStream(fileName);
    String fileExtn = getFileExtension(fileName);

    Workbook xssfWorkBook ;
    HSSFWorkbook hssfWorkBook ;
    Sheet sheet=null;
    if (fileExtn.equalsIgnoreCase("xlsx"))
    {
        log.debug("Before creating WorkBook");
        xssfWorkBook = new XSSFWorkbook(fis);
        log.debug("xssfWorkBook object created");
        sheet = xssfWorkBook.getSheetAt(0);
    }else  if (fileExtn.equalsIgnoreCase("xls"))
    {
        hssfWorkBook = new HSSFWorkbook(fis);
        sheet = (Sheet) hssfWorkBook.getSheetAt(0);
    }
    Iterator<Row> rows = sheet.rowIterator();
    int rowCount = 0;
    while (rows.hasNext()) {
        if (rowCount == 0) {
            Row row = rows.next();
            Iterator<Cell> cells = row.cellIterator();
            short cellCounter = 1;
            while (cells.hasNext()) {
                Cell cell = cells.next();
                String cellValue = getCellValueAsString(cell);
                keyMap.put(cellCounter, cellValue);
                cellCounter++;
            }
        } else {
            valueMap = new HashMap<String, String>();
            Row row = rows.next();
            Iterator<Cell> cells = row.cellIterator();
            short cellCounter = 1;
            while (cells.hasNext()) {
                Cell cell = cells.next();
                String cellValue = getCellValueAsString(cell);
                valueMap.put(keyMap.get(cellCounter), cellValue);
                cellCounter++;
            }
            rowList.add(valueMap);
        }
        rowCount++;
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
    log.error("FileNotFoundException" + e);
    throw new Exception("FileNotFoundException while reading Excel file, Message <" + e.getMessage() + ">");
} catch (IOException e) {
    e.printStackTrace();
    log.error("IOException" +e);
    throw new Exception("IOException while reading Excel file, Message <" + e.getMessage() + ">");
}catch(Exception e){
    e.printStackTrace();
    throw new Exception("Exception while reading Excel file, Message <" + e.getMessage() + ">");
}

Getting below exception as stackTrace is :

java.lang.NullPointerException at org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:562) at org.apache.poi.POIXMLDocumentPart.(POIXMLDocumentPart.java:63) at org.apache.poi.POIXMLDocument.(POIXMLDocument.java:58) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:186) at com.covad.portlet.mars.util.ExcelReader.readExcel(ExcelReader.java:72)

Upvotes: 1

Views: 2733

Answers (2)

Amjad Abdul-Ghani
Amjad Abdul-Ghani

Reputation: 154

I had this issue before make sure that the file is saved as ExcelWorkbook(.xlsx) ,the issue may happen if you saved the file as Strict Open XML SpreadSheet(.xlsx),they both have the same extension but the internal structures may vary and POI seems does not support it;to solve the issue on the file re save it again as ExcelWorkbook(*.xlsx) . I hope this may solve your issue.

Upvotes: 7

Harshal
Harshal

Reputation: 41

Yes, the above programs needs few jars as poi-3.7.jar, poi-ooxml-3.7-20101029.jar, poi-ooxml-schemas-3.7-20101029.jar, xmlbeans-2.3.0.jar and dom4j-1.6.1.jar.

Also, the input file for which I was trying had some problem. I created new blank file and copied all data which I needed and tried to read the same. It worked.

Thank you All for your time and help.

Upvotes: 1

Related Questions