[java] Apache POI를 사용하여 Excel 파일의 데이터 유효성 검사 오류 표시 읽기

Apache POI는 Java로 Microsoft Office 파일을 읽고 쓸 수 있는 라이브러리입니다. 이번 포스트에서는 Apache POI를 사용하여 Excel 파일의 데이터 유효성 검사 오류를 표시하는 방법을 알아보겠습니다.

1. Apache POI 라이브러리 추가

먼저, 프로젝트에 Apache POI 라이브러리를 추가해야 합니다. 아래의 Maven 좌표를 참고하여 pom.xml 파일에 의존성을 추가하세요.

<dependencies>
    <!-- Apache POI 라이브러리 -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>

의존성을 추가한 후, Maven 또는 다른 빌드 도구를 사용하여 라이브러리를 다운로드 받도록 합니다.

2. Excel 파일에서 데이터 유효성 검사 오류 읽기

이제 Apache POI를 사용하여 Excel 파일에서 데이터 유효성 검사 오류를 읽는 방법을 살펴보겠습니다. 아래의 예제 코드를 참고하세요.

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelValidationErrorsReader {
    public static void main(String[] args) {
        try (FileInputStream file = new FileInputStream("sample.xlsx")) {
            Workbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheetAt(0);

            // 유효성 검사 규칙을 담고 있는 셀 범위 가져오기
            SheetConditionalFormatting conditionalFormatting = sheet.getSheetConditionalFormatting();
            for (int i = 0; i < conditionalFormatting.getNumConditionalFormattings(); i++) {
                ConditionalFormatting conditionalFormat = conditionalFormatting.getConditionalFormattingAt(i);
                CellRangeAddress[] cellRanges = conditionalFormat.getFormattingRanges();
                for (CellRangeAddress cellRange : cellRanges) {
                    // 셀 범위 내의 유효성 검사 오류 표시 읽기
                    for (int row = cellRange.getFirstRow(); row <= cellRange.getLastRow(); row++) {
                        for (int col = cellRange.getFirstColumn(); col <= cellRange.getLastColumn(); col++) {
                            Cell cell = sheet.getRow(row).getCell(col);
                            DataValidation dataValidation = sheet.getDataValidation(cell.getAddress());
                            if (dataValidation != null) {
                                DataValidationConstraint validationConstraint = dataValidation.getValidationConstraint();
                                if (validationConstraint.getValidationType().equals(DataValidationConstraint.ValidationType.FORMULA)) {
                                    System.out.println("셀(" + cell.getAddress() + ")의 유효성 검사 오류: " + cell.getStringCellValue());
                                } else {
                                    System.out.println("셀(" + cell.getAddress() + ")의 유효성 검사 오류: " + cell.getErrorCellValue());
                                }
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

위의 예제 코드에서 sample.xlsx는 데이터 유효성 검사 규칙이 설정된 Excel 파일의 경로에 맞게 변경해야 합니다.

위의 코드는 Excel 파일에서 데이터 유효성 검사 규칙이 설정된 셀 범위를 가져와 각 셀 내의 유효성 검사 오류를 출력하는 간단한 예제입니다.

결론

Apache POI를 사용하여 Excel 파일의 데이터 유효성 검사 오류를 읽을 수 있습니다. 이를 통해 Excel 파일에 있는 잘못된 데이터를 식별하고 처리할 수 있습니다.