0
0

java Apach AOI でExcelのシートを部分一致で取得する

Last updated at Posted at 2023-10-05
import org.apache.poi.ss.usermodel.*;

public class PartialSheetDataExtractor {

    public static void main(String[] args) {
        // Excelファイルのパスを指定してワークブックを開く
        try (Workbook workbook = WorkbookFactory.create(new FileInputStream("example.xlsx"))) {
            // 部分一致のシート名を探し、シートを取得
            for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
                Sheet sheet = workbook.getSheetAt(i);
                String sheetName = sheet.getSheetName();

                // 部分一致を検証
                if (sheetName.contains("部分一致の文字列")) {
                    // シート上のデータを取得し、処理を委任
                    processSheetData(sheet);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** シートからセルの値を取得する  */
    private static void processSheetData(Sheet sheet) {
        for (Row row : sheet) {
            for (Cell cell : row) {
                processCellData(sheet, cell);
            }
        }
    }

    /** セルの値を取得する  */
    private static void processCellData(Sheet sheet, Cell cell) {
        if (cell.getCellType() == CellType.STRING) {
            String cellValue = cell.getStringCellValue();
            System.out.println("シート名: " + sheet.getSheetName() + ", セルの内容: " + cellValue);
        }
    }
}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0