解决Excel表格行中有格式但是没有数据的问题
public class test {
public static void main(String[] args) {
XSSFWorkbook wb = null;
try {
wb = new XSSFWorkbook(new FileInputStream("G:\\xxx.xlsx"));
} catch (Exception e) {
e.printStackTrace();
}
//读取第一页的内容
XSSFSheet sheet = wb.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
System.out.println("-----------------------------------" + i + "----------------------------------------------");
XSSFRow row = sheet.getRow(i);
boolean flag = false;
if (row != null) {
for (Cell cell : row) {
//判断该单元格是否为空
if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
flag = true;
break;
}
}
if (flag) {
//如果为空,删除整个单元格
sheet.removeRow(row);
//如果在if外面有业务可以打开注释
// continue;
}
}
}
}
}
注意:每个Excel单元格中的数据都不能为空!