读取Excel文件并跳过空行而不是空列
我希望读取一个Excel文件并跳过空行。 我的代码跳过空单元格,但它也跳过空列。 我怎样才能跳过空行,但保持空列? 我正在使用JXL Java。
for (int i = 0; i < sheet.getRows(); i++) { for (int j = 0; j < sheet.getColumns(); j++) { Cell cell = sheet.getCell(j, i); String con = cell.getContents(); if (con != null && con.length() != 0) { System.out.print(con); System.out.print("|"); } else { continue; } } }
尝试这个:
for (int i = 0; i < sheet.getRows(); i++) { boolean rowEmpty = true; String currentRow = ""; for (int j = 0; j < sheet.getColumns(); j++) { Cell cell = sheet.getCell(j, i); String con=cell.getContents(); if(con !=null && con.length()!=0){ rowEmpty = false; } currentRow += con + "|"; } if(!rowEmpty) { System.out.println(currentRow); } }
你在做什么是:
- 循环行
- 循环通过列
- 打印单元格,如果它只是空的,否则跳过它(你的continue语句不做任何事情,因为它是循环中的最后一个指令,break语句只是在它到达空单元格时停止读取行)
- 循环通过列
这是做什么的:
- 循环行
- 循环通过列
- 将单元格附加到行的string,如果不是空的,则将rowEmpty设置为false(因为它至less包含一个非空单元格)
- 仅在非空的情况下打印行
- 循环通过列