【问题标题】:I want to display the line with the highest metric. But nothing comes out for me我想显示具有最高度量的行。但对我来说什么都没有
【发布时间】:2021-05-03 16:24:12
【问题描述】:

这是我的桌子

Name Age
Marsel 22
Lewis 33
Andrew 23
Harry 45

我需要在控制台上显示 Harry 45 岁的名字 '''

public class Main {

  public static Scanner in = new Scanner(System.in);

  public static void main(String[] args) throws IOException {

    FileInputStream file = new FileInputStream(new File("C:\\Users\\User\\Desktop\\school.xlsx"));
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheet("Director");
    List<Double> list = new ArrayList<>();
    Iterator iterator=sheet.iterator();

    
    while (iterator.hasNext()) {
        XSSFRow row = (XSSFRow) iterator.next();

        Iterator cellIterator = row.cellIterator();

        while (cellIterator.hasNext()) {
            XSSFCell cell = (XSSFCell) cellIterator.next();

            switch (cell.getCellType()) {
                case NUMERIC:
                    list.add(cell.getNumericCellValue());
                    break;

            }
        }
    }
    Double max=Collections.max(list);
    int ind=list.indexOf(max);

    while(iterator.hasNext()){
        XSSFRow row = sheet.getRow(ind+1);
        Iterator cellIterator=row.cellIterator();
        System.out.println(row);
        while(cellIterator.hasNext())
        {
            XSSFCell cell=(XSSFCell) cellIterator.next();

            switch(cell.getCellType())
            {
                case STRING: System.out.print(cell.getStringCellValue()); break;
                case NUMERIC: System.out.print(cell.getNumericCellValue());break;
                case BOOLEAN: System.out.print(cell.getBooleanCellValue()); break;
            }
        }
    }

    }
}
'''

【问题讨论】:

  • 您是否已经使用调试器单步调试您的代码?如果您知道表格的布局,即哪一列包含什么数据,为什么要使用如此复杂的迭代方案?我有一段时间没有使用 POI,但它应该有方法可以更轻松地处理单元格和单元格范围。

标签: java excel apache-poi


【解决方案1】:

您只能遍历一次迭代器。如果您想再次找到该行,则必须从顶部再次读取该文件。您正在使用的迭代器已经在 iterator.hasNext () 上返回 false。所以根本不会进入第二个循环。

【讨论】:

猜你喜欢
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-06
相关资源
最近更新 更多