如何使用java检查xlsx中的行是否为空

如何使用java检查xlsx中的行是否为空,java,apache-poi,Java,Apache Poi,我使用ApachePOI来读取xlsx文件,它工作得很好。当发现行为空时,我有个问题要问你们,我如何处理它?我的文件只包含50行,但它显示1000行,其余行为空 try { FileInputStream file = new FileInputStream(new File("D://productData.xlsx")); Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.

我使用ApachePOI来读取xlsx文件,它工作得很好。当发现行为空时,我有个问题要问你们,我如何处理它?我的文件只包含50行,但它显示1000行,其余行为空

try {
    FileInputStream file = new FileInputStream(new File("D://productData.xlsx"));
    Workbook workbook = WorkbookFactory.create(file);
    Sheet sheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();
    CountryCode countryCode = userDao.getCode(sheet.getRow(1).getCell(4).toString());
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        if (row.getRowNum() == 0) {
            continue;
        } else if (row.getRowNum() == 1) {
            //some operation
        } else if (row.getRowNum() == 2) {
            continue;
        } else {
            ExcelData excelData = new ExcelData();
            try {
                for (int i = 0; i <= 6; i++) {
                    row.getCell(i).setCellType(Cell.CELL_TYPE_STRING);
                }
                //some operation
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    file.close();
    response.setResponse(data);
} catch (Exception e) {
    e.printStackTrace();
}
试试看{
FileInputStream文件=新的FileInputStream(新文件(“D://productData.xlsx”);
工作簿=WorkbookFactory.create(文件);
工作表=工作簿。getSheetAt(0);
迭代器rowIterator=sheet.Iterator();
CountryCode CountryCode=userDao.getCode(sheet.getRow(1.getCell(4.toString());
while(roweiterator.hasNext()){
行=行迭代器。下一步();
if(row.getRowNum()==0){
继续;
}else if(row.getRowNum()==1){
//一些手术
}else if(row.getRowNum()==2){
继续;
}否则{
ExcelData ExcelData=新ExcelData();
试一试{

对于(inti=0;i如果您获取一行,并返回null,那么这意味着该行的文件中没有存储任何数据-它是完全空白的


POI默认为您提供文件中的内容。对于单元格,您可以设置MissingCellPolicy来控制丢失单元格和空白单元格的处理方式。Apache POI文档中有一些使用此方法的示例。对于行,它们要么在那里,要么不在那里,因此在获取行时需要检查空值。

如果获取行并返回空值,则t表示该行的文件中没有存储任何数据-它是完全空白的

POI默认为您提供文件中的内容。对于单元格,您可以设置MissingCellPolicy来控制丢失单元格和空白单元格的处理方式。Apache POI文档中有一些使用此方法的示例。对于行,它们要么在那里,要么不在,因此在获取行时需要检查空值