你好这里的人在这个extractTable方法中使用while(true)。根据方法,如果条件失败,它将是一个无限循环,这是一个糟糕的编程实践,你可以帮助我改变,为每个或每个
private List<Map<String, Map<String, Integer>>> extractTable(XSSFSheet sheet, Cell tablePosition) {
List<Map<String, Map<String, Integer>>> table = new ArrayList<>();
int headerRow = tablePosition.getRowIndex();
int valuesHeaderColumn = tablePosition.getColumnIndex();
// read all columns and rows until finding an empty one
int row = headerRow + 1;
int col = valuesHeaderColumn + 1;
// read all columns starting at col
while (true) {
Map<String, Map<String, Integer>> tableColumn = new HashMap<>();
Cell headerCell = sheet.getRow(headerRow).getCell(col);
if (headerCell == null)
break;
String columnName = headerCell.getStringCellValue().toUpperCase();
if ("".equals(columnName))
break;
// read all rows starting at row
Map<String, Integer> values = new HashMap<>();
while (true) {
Row valueHeaderRow = sheet.getRow(row);
if (valueHeaderRow == null)
break;
Cell valueHeaderCell = valueHeaderRow.getCell(valuesHeaderColumn);
String valueHeader = valueHeaderCell.getStringCellValue();
if ("".equals(valueHeader))
break;
Cell valueCell = sheet.getRow(row).getCell(col);
Integer value = (int) valueCell.getNumericCellValue();
values.put(valueHeader, value);
row++;
}
tableColumn.put(columnName, values);
// Add the columns map to the list
table.add(tableColumn);
col++;
row = headerRow + 1;
}
return table;
}
答案 0 :(得分:1)
我不相信(真的)......休息是一种糟糕的编程习惯,看here。但是要使用POI迭代电子表格的行和列,你可以这样做recommended:
for (Row row: sheet) {
for (Cell cell: row) {
// do something here
}
}