Can somebody please explain about the Missing cell policy
of Apache POI
? What are exactly missing cells ? I didn\'t find the Apache POI
Did you read the Apache POI Excel Busy Developer's Guide?
In some cases, when iterating, you need full control over how missing or blank rows and cells are treated, and you need to ensure you visit every cell and not just those defined in the file. (The CellIterator will only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel).
In cases such as these, you should fetch the first and last column information for a row, then call getCell(int, MissingCellPolicy) to fetch the cell. Use a MissingCellPolicy to control how blank or null cells are handled.
If you're iterating over columns in a row, some cells that are blank may not even exist, which may causing unsuspecting code to throw a NullPointerException
. A MissingCellPolicy
, when passed to getCell
, guides and simplifies code that tells Apache POI how to handle these kinds of cells.
Cell
returned doesn't exist, instead of returning null
, create a new Cell
with a cell type of "blank". This can help avoid NullPointerException
s conveniently.null
. This can allow you ignore blank cells that do exist easily.null
for cells that don't really exist and return the blank Cell
if it exists but its cell type is blank. This is the behavior of the getCell overload that doesn't take a MissingCellPolicy.I'm using the code in java as below, it's working good for me :) hope it helps.
ArrayList<ArrayList<String>> cellArrayListHolder = new ArrayList<ArrayList<String>>();
FileInputStream excelFile = new FileInputStream(new File(fileName));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
while (iterator.hasNext())
{
ArrayList<String> cellStoreArrayList = new ArrayList<String>();
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
int column_counting = 0;
int patched_count = 0;
while (cellIterator.hasNext() && column_counting < read_column_size) {
column_counting ++;
Cell currentCell = cellIterator.next();
int missed_column = 1 - column_counting + currentCell.getColumnIndex() - patched_count;
for(int i=0; i<missed_column; i++){
cellStoreArrayList.add("");
patched_count++;
}
switch (currentCell.getCellType()){
case Cell.CELL_TYPE_STRING:
cellStoreArrayList.add(String.valueOf(currentCell).trim());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(currentCell)) {
DateFormat db_df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
cellStoreArrayList.add(db_df.format(currentCell.getDateCellValue()));
} else {
cellStoreArrayList.add(String.valueOf(currentCell.getNumericCellValue()));
}
break;
case Cell.CELL_TYPE_BOOLEAN:
cellStoreArrayList.add(String.valueOf(currentCell.getBooleanCellValue()));
break;
default:
cellStoreArrayList.add("");
break;
}
}
cellArrayListHolder.add(cellStoreArrayList);
}