如何从 excel sheet 中检索某些特定的行和列?

How to retrieve some specific rows and columns from an excel sheet?

 List sheetData = new ArrayList();
        InputStream excelFile = new BufferedInputStream(new FileInputStream("D:\Excel file\data.xlsx"));
        Workbook workBook = new XSSFWorkbook(excelFile); // Creates Workbook
        XSSFSheet sheet = (XSSFSheet) workBook.getSheet("Daily");
        DataFormatter formatter = new DataFormatter();
        for (int i = 7; i <= 35; i++) {
            XSSFRow row = sheet.getRow(i);
            Cell cell = row.getCell(i);
            String val = formatter.formatCellValue(cell);
            sheetData.add(val);
        }

您可以在文档中使用 Iterator 进行迭代,但还有一个函数 "getRow() and getCell()"

  Workbook workbook = new XSSFWorkbook(excelFile);

  // defines the standard pointer in document in the first Sheet
  XSSFSheet data = this.workbook.getSheetAt(0);
  // you could iterate the document with an iterator
  Iterator<Cell> iterator = this.data.iterator();

  // x/y pointer at the document
  Row row = data.getRow(y);
  Cell pointingCell = row.getCell(x);

  String pointingString = pointingCell.getStringCellValue();

假设我正确理解了你的问题,我相信你想要处理从第 2 行开始到文件末尾存在的每一行,并且对于这些行中的每一行,请考虑第 7 列到第 35 列中的单元格。我相信您可能还需要处理这些值,但您没有说明如何处理,因此对于这个示例,我将把它们填充到一个字符串列表中并希望最好...

这是基于Apache POI documentation for iterating over rows and cells

File excelFile = new File("D:\Excel file\data.xlsx");
Workbook workBook = WorkbookFactory.create(excelFile);
Sheet sheet = workBook.getSheet("Daily");
DataFormatter formatter = new DataFormatter();

// Start from the 2nd row, processing all to the end
// Note - Rows and Columns in Apache POI are 0-based not 1-based
for (int rn=1; rn<=sheet.getLastRowNum(); rn++) {
   Row row = sheet.getRow(rn);
   if (row == null) {
      // Whole row is empty. Handle as required here
      continue;
   }
   List<String> values = new ArrayList<String>();
   for (int cn=6; cn<35; cn++) {
      Cell cell = row.getCell(cn);
      String val = null;
      if (cell != null) { val = formatter.formatCellValue(cell); }
      if (val == null || val.isEmpty()) {
         // Cell is empty. Handle as required here
      }
      // Save the value to list. Save to an object instead if required
      values.append(val);
   }
}
workBook.close();

根据您的业务需求,加入处理空白行和单元格的逻辑。然后,再次根据您的业务需求,对您找到的值做任何您需要做的事情!