【问题标题】:How to trace blank cells of a Excel row in Java如何在 Java 中跟踪 Excel 行的空白单元格
【发布时间】:2013-11-30 05:28:22
【问题描述】:

我正在从 Excel 文件中获取数据并将它们存储在数组列表中。除空白单元格外,所有单元格均已正确读取。我想跟踪空白单元格。

例如,我有一个包含 7 列的 Excel 文件,其中 7 列中的 5 个填充了数据,2 个单元格为空,因此将它们存储在 arraylist 中后,我得到的列表大小为 12 而不是 14。

我假设在迭代过程中只考虑填充数据的单元格并忽略空白单元格。

这是我的代码

  ArrayList arrayList=new ArrayList(); 
    int rownum=0;
    try{
        FileInputStream fis=new FileInputStream(new File("D:\\emsAttendancesheet_26-Nov-2013.xls"));
        HSSFWorkbook hssfWorkbook=new HSSFWorkbook(fis);
        HSSFSheet hssfSheet=hssfWorkbook.getSheetAt(0);
        Iterator<Row> rowIterator=hssfSheet.iterator();
        while(rowIterator.hasNext()){
            Row row=rowIterator.next();
            Iterator<Cell> cellIterator=row.iterator();
            while(cellIterator.hasNext()){
                Cell cell=cellIterator.next();
                if(cell.getCellType()==Cell.CELL_TYPE_STRING){
                 System.out.println(cell.getStringCellValue());
                 arrayList.add(cell.getStringCellValue());  
                }
                else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
                    arrayList.add(cell.getNumericCellValue());
                    System.out.println(cell.getNumericCellValue()); 

                }
            }
        }

        System.out.println("Size of List::"+arrayList.size());
     }catch(Exception ex){
         System.out.println("Exception in Exel   creation"+ex.getMessage());            
    }

我的 Exel 文件如下所示:

Date         EmpId  EmpName Status In-Time  Out-Time  Comment

14-Nov-2013   112    Akas   Absent                    illness

这里 In-Time 和 Out-time 是空白的,所以它没有读取空单元格。我需要考虑空单元格。任何人都可以建议代码吗?

【问题讨论】:

标签: java excel


【解决方案1】:

检查 NULL 或空白单元格:

       for( int cellCounter = 0 ; cellCounter < maxNumOfCells ; cellCounter ++)
         { 
            HSSFCell cell;

            if( row.getCell(cellCounter ) == null ){
                cell = row.createCell(cellCounter);
            } else {
                cell = row.getCell(cellCounter);
            }
         }

循环遍历单元格。从单元格 0 到最后一个单元格编号。如果发现单元格为空,它将创建具有空白值的单元格。之后将您的单元格添加到列表中,然后遍历列表。

【讨论】:

    【解决方案2】:
     XSSFWorkbook book = new XSSFWorkbook(fis);
            XSSFSheet sheet = book.getSheetAt(0);
            Iterator<Row> itr = sheet.iterator();
            while(itr.hasNext()){
                Row row = itr.next();
                Iterator<Cell> cellItrator = row.iterator();
                if(row.getRowNum()==0){
                    System.out.println(row.getRowNum());
                    while(cellItrator.hasNext()){
                        Cell  cell = cellItrator.next();
                        System.out.println("CM Comumns "+ cell.toString());
                        tempList.add(cell.toString());
    
                        }
                    }
                if(row.getRowNum()==1){
                    Cell  cell = cellItrator.next();
                    for(int i=0;i<tempList.size();i++){
                        int temStr= (int) Math.round(cell.getNumericCellValue());
                        System.out.println("Cell Value  " + temStr);
                            if (temStr==0) {   
                                System.out.println("Null Cell");
                                cell = cellItrator.next();
                            }
                            else {
                                    System.out.println("Add Comumns "+cell.getNumericCellValue());
                                    cell = cellItrator.next();
                                    System.out.println(cell);
                            }
                        }
                    }
                }
            }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多