Checking for an empty cell in excel using JExcel API

London guy picture London guy · May 11, 2012 · Viewed 8.1k times · Source

How do I check if a CELL in excel sheet is empty and doesn't have any content inside it? I am traversing a row from left to right, and want to stop when I encounter an empty cell. I am using

 cell.getType()==CellType.EMPTY

but it doesn't seem to work. Also,

if(cell.getContents() == "") is also not working.

What is the condition to check if the cell is a blank cell?

Thanks Abhishek S

Answer

Praveen picture Praveen · Jun 13, 2012
    List cellDataList = new ArrayList();
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

                        XSSFSheet sheet = workbook.getSheetAt(0);

                        Iterator rows = sheet.rowIterator();

                        int number=sheet.getLastRowNum();


                        int lineNumber = 0;

                        while (rows.hasNext())

                        {
                            XSSFRow row = ((XSSFRow) rows.next());
                            lineNumber++;
                            if(lineNumber==1){continue;}




                            Iterator cells = row.cellIterator();
                            List cellTempList = new ArrayList();    
                            int current = 0, next =1;
                            while(cells.hasNext())

                            {

                                XSSFCell cell = (XSSFCell) cells.next();


                                current = cell.getColumnIndex();


                                if(current<next){

                                }
                                else{

                                    int loop = current-next;

                                    for(int k=0;k<loop+1;k++){

                                        cellTempList.add(null);
                                        next = next + 1;
                                    }
                                }
                                switch (cell.getCellType()) {
                                            case Cell.CELL_TYPE_STRING:
                                                System.out.println(cell.getRichStringCellValue().getString());
                                                cellTempList.add(cell.getRichStringCellValue().getString());
                                                break;
                                            case Cell.CELL_TYPE_NUMERIC:                                                    
                                                    System.out.println(cell.getNumericCellValue());
                                                    cellTempList.add(String.valueOf(cell.getNumericCellValue()));                                                   
                                                break;
                                            case Cell.CELL_TYPE_BOOLEAN:
                                                System.out.println(cell.getBooleanCellValue());
                                                break;
                                            case Cell.CELL_TYPE_FORMULA:
                                                System.out.println(cell.getCellFormula());
                                                cellTempList.add(cell.getCellFormula());
                                                break;                                              

                                            default:
                                                System.out.println("Inside default");
                                }
                                next = next + 1;

                            }
                            cellDataList.add(cellTempList); 
                         }