Missing cell policy of Apache POI Java

一世执手 提交于 2019-12-19 03:37:12

问题


Can somebody please explain about the Missing cell policy of Apache POI ? What are exactly missing cells ? I didn't find the Apache POI docs link to be self-explanatory on what exactly are missing cells.


回答1:


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.

  • CREATE_NULL_AS_BLANK - If the Cell returned doesn't exist, instead of returning null, create a new Cell with a cell type of "blank". This can help avoid NullPointerExceptions conveniently.
  • RETURN_BLANK_AS_NULL - Even if the cell exists but has a cell type of "blank", return null. This can allow you ignore blank cells that do exist easily.
  • RETURN_NULL_AND_BLANK - Don't modify the existing structure; return 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.



回答2:


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);
}



来源:https://stackoverflow.com/questions/36776745/missing-cell-policy-of-apache-poi-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!