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

后端 未结 9 1483
孤独总比滥情好
孤独总比滥情好 2020-12-01 05:02

I have a huge excel file with tons of columns which looks like this :-

Column1 Column2 Column3 Column4 Column5
abc             def             ghi
        mn         


        
9条回答
  •  遥遥无期
    2020-12-01 06:00

    public String[] rowToString(Row row)
    {
        Iterator cells = row.cellIterator() ;
        String[] data = new String[row.getLastCellNum()] ;
    
        int previousCell = 0 ;
    
        Cell cell = cells.next() ;
        int currentCell = cell.getColumnIndex();
    
        while (true)
        {
            if (previousCell == currentCell) {
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        data[previousCell] = cell.getNumericCellValue()+"" ;
                        break;
                    case Cell.CELL_TYPE_STRING:
                        data[previousCell] = cell.getStringCellValue() ;
                        break;
                        /* // there could be other cases here.
                        case Cell.CELL_TYPE_FORMULA:
                            data[previousCell] =eval.evaluateFormulaCell(cell);
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            data[previousCell] = cell.getBooleanCellValue();
                            break;
                        case Cell.CELL_TYPE_BLANK:
                            data[previousCell] = "";
                            break;
                        case Cell.CELL_TYPE_ERROR:
                            data[previousCell] = "ERROR";
                            break;
                        */
                }
                if(cells.hasNext()){
                    cell = cells.next() ;
                    currentCell = cell.getColumnIndex();
                } else {
                    break ;
                }
    
            } else {
                data[previousCell] = "";
            }
            previousCell++ ;
    
        }
    
        return data ;
    
    }
    

提交回复
热议问题