-1
  • Java (Apache POI) を使用して xlsx ファイルを読み込んでいます。
  • Document クラスを作成しました (すべての Excel 列見出しを変数として持つ)
  • Document クラスのコレクションを作成して、Excel の各行を読み取り、Document クラスにマップする必要があります。
  • 私が直面している問題は、2 行目から 7 列目から 35 列目まで読み取りを開始し、対応する値をドキュメント クラスにマップする必要があることです。

  • コードがどうあるべきか正確に把握できませんか?

  • 次のコード行を書きました。

 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);
        }

4

2 に答える 2

1

私があなたの質問を正しく理解していると仮定すると、行 2 からファイルの最後まで存在するすべての行を処理したいと思います。これらの行のそれぞれについて、7 列目から 35 列目のセルを検討してください。これらの値を処理する必要がありますが、あなたはその方法を説明していないので、この例ではそれらを文字列のリストに詰め込み、最善を尽くします...

これは、行とセルを反復処理するための Apache POI ドキュメントに基づいています。

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();

ビジネス要件に応じて、空白の行とセルを処理するロジックを組み込みます。次に、ビジネス要件に従って、見つけた値を使用して必要なことをすべて行います。

于 2017-11-14T13:14:10.183 に答える