如何使用Java从excel base中某一列的数据中读取特定行

如何使用Java从excel base中某一列的数据中读取特定行,java,Java,我有一个excel,格式如下所示: FileName,Type,Size,DateModified,Completed,Count File1,text,111,04/22/2014,Yes,1 File2,text,222,04/23/2014,No,2 File3,word,333,04/24/2014,No,3 File4,excel,444,04/24/2014,Yes,4 File5,ppoint,555,04/25/2014,Yes,5 我有一个程序,可以逐行读取excel文件,然

我有一个excel,格式如下所示:

FileName,Type,Size,DateModified,Completed,Count
File1,text,111,04/22/2014,Yes,1
File2,text,222,04/23/2014,No,2
File3,word,333,04/24/2014,No,3
File4,excel,444,04/24/2014,Yes,4
File5,ppoint,555,04/25/2014,Yes,5
我有一个程序,可以逐行读取excel文件,然后逐行读取列。但我需要打印的只是带有特定日期的行。对于本例,我想打印DateModified为2014年4月24日的行,但找不到过滤它们或创建条件的方法

我尝试使用DateModified列进行过滤,但找不到一种方法,当它检测到所需的日期时,它仍然会打印第1列的整行

Java代码:

HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.rowIterator(); 

while( rows.hasNext() ) {   
    HSSFRow row = (HSSFRow) rows.next();
    System.out.println("\n");
    Iterator cells = row.cellIterator();

    while( cells.hasNext() ) {
        HSSFCell cell = (HSSFCell) cells.next();

               //missing condition

            if(HSSFCell.CELL_TYPE_STRING==cell.getCellType()) {
                System.out.print( cell.getStringCellValue()+"     " );
            } else if (HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) {
                System.out.print( cell.getNumericCellValue()+"     "+cell.getColumnIndex() );
            } else if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) {
                System.out.print( cell.getBooleanCellValue()+"     " );
            } else if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType());
                System.out.print( "BLANK     " );
    }

}

不要在找到后立即打印单元格值。 而是附加到StringBuilder中,并最终仅在条件匹配时打印

像这样

StringBuilder builder = new StringBuilder();
          while( rows.hasNext() ) {   
                HSSFRow row = (HSSFRow) rows.next();
                System.out.println("\n");
                Iterator cells = row.cellIterator();
                boolean isValidRow = true;
                while( cells.hasNext() ) {
                    HSSFCell cell = (HSSFCell) cells.next();

                   //missing condition
                    if (condition match){
                        isValidRow = false;
                        break;
                    }

                    if(HSSFCell.CELL_TYPE_STRING==cell.getCellType()) {
                        builder.append( cell.getStringCellValue()+"     " );
                    } else if (HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) {
                        builder.append( cell.getNumericCellValue()+"     "+cell.getColumnIndex() );
                    } else if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) {
                        builder.append( cell.getBooleanCellValue()+"     " );
                    } else if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType());
                    builder.append( "BLANK     " );
                }
                if (isValidRow){
                    System.out.println(builder.toString());

                }
                builder.setLength(0);
          }
更新 注**。如果您知道要检查的单元格的外部位置

你也可以这样检查

HSSFCell cell = (HSSFCell) row.getCell(3);
if ( "04/24/2014".equals(cell.getStringCellValue())){
    Iterator cells = row.cellIterator();
                while( cells.hasNext() ) {
                    HSSFCell cell = (HSSFCell) cells.next();

                   //missing condition

                    if(HSSFCell.CELL_TYPE_STRING==cell.getCellType()) {
                        builder.append( cell.getStringCellValue()+"     " );
                    } else if (HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) {
                        builder.append( cell.getNumericCellValue()+"     "+cell.getColumnIndex() );
                    } else if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) {
                        builder.append( cell.getBooleanCellValue()+"     " );
                    } else if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType());
                    builder.append( "BLANK     " );
                }

}