使用JExcel API检查excel中的空单元格

时间:2012-05-11 11:50:23

标签: java api jexcelapi

如何检查Excel工作表中的CELL是否为空且内部没有任何内容?我从左到右穿过一排,想要在遇到空单元时停下来。我正在使用

 cell.getType()==CellType.EMPTY

但它似乎不起作用。另外,

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

检查细胞是否为空白细胞的条件是什么?

由于 Abhishek S

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

String contents = cell.getContents();
if (contents != null && contents.length() != 0)

答案 2 :(得分:0)

从以下链接JExcel API,检查cell.isEmpty()方法以进行测试。