How to get an Excel Blank Cell Value in Apache POI? How to get an Excel Blank Cell Value in Apache POI? apache apache

How to get an Excel Blank Cell Value in Apache POI?


If you want to get all cells, no matter if they exist or not, then the iterator isn't for you. Instead, you need to manually fetch the appropriate cells, likely with a missing cell policy

for(Row row : sheet) {   for(int cn=0; cn<row.getLastCellNum(); cn++) {       // If the cell is missing from the file, generate a blank one       // (Works by specifying a MissingCellPolicy)       Cell cell = row.getCell(cn, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);       // Print the cell for debugging       System.out.println("CELL: " + cn + " --> " + cell.toString());   }}

There's more details on all of this in the Apache POI documentation on iterating over cells


I have been frustrated by this same problem. Here is what I found with poi-3.7-20101029 and poi-3.8.

RowIterator and CellIterator do not support iterating over NULL cells or rows -- only physically defined cells (which can be BLANK).

The solution that returns what I expect requires using the 0-based Row.getCell([int], Row.CREATE_NULL_AS_BLANK), much like Chavira's answer alludes to (assuming 8 cell rows). Or you can use the Cell.columnIndex value while iterating to check for jumping numbers...

Annoyingly, after creating blank cells using method #1, the iterators will return the now created BLANK cells. I consider it a bug that MissingCellPolicy is ignored by CellIterator.


The reason is quite simple: Excel files can contain as many rows and as many columns as possibles, so returning all available blank rows and columns will render the cells huge and memory intensive.

Assuming you have a 10x10 sheet, in Excel, it's not "exactly" 10x10 since you can add 11x10 very easily with blank cell, so should POI return the 11th column?

One way to do what you're requesting is to use HSSFCell.getColumnIndex().

Example:

//Assuming your have a 2 dimensional array.String[][] values = ......;// It is assignedPOIFSFileSystem fileSystem = new POIFSFileSystem(new FileInputStream(fileName));HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);//Going through every worksheet.for (int sheetPos = 0; sheetPos < workbook.getNumberOfSheets(); sheetPos++) {    HSSFSheet sheet = workbook.getSheetAt(sheetPos);    int rowPos = 0;    Iterator<Row> rows = sheet.rowIterator();    while (rows.hasNext()) {        HSSFRow row = (HSSFRow) rows.next();        Iterator<Cell> cells = row.cellIterator();        while (cells.hasNext()) {            HSSFCell cell = (HSSFCell) cells.next();            String value = "";            switch (cell.getCellType()) {                case HSSFCell.CELL_TYPE_NUMERIC:                    value = BigDecimal.valueOf(cell.getNumericCellValue()).toPlainString();                    break;                case HSSFCell.CELL_TYPE_STRING:                    value = cell.getStringCellValue();                    break;                case HSSFCell.CELL_TYPE_BLANK:                    value = "";                    break;                case HSSFCell.CELL_TYPE_FORMULA:                    value = cell.getCellFormula();                    break;                default:                    break;            }            values[rowPos][cell.getColumnIndex()] = value;        }        rowPos++;    }}