Lang/Java

poi excel 로 빈 값 처리 시 주의해야 하는 부분

hamaganatanadda 2024. 12. 15. 13:29

테스트 버전

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>

 

간단한 poi excel 소스이다. 

try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Sheet");
Cell cell = null;
for (int i = 1; i < 20; i++) {
Row row = sheet.createRow(i);
if (i % 2 == 0) {
cell = row.createCell(0);
cell.setCellValue(i);
cell = row.createCell(1);
cell.setCellValue(i);
} else {
cell = row.createCell(0);
cell.setBlank();
cell = row.createCell(1);
cell.setCellValue("");
}
}
try (FileOutputStream fos = new FileOutputStream("c:\\test.xlsx")) {
workbook.write(fos);
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

 

원하는 결과는 A, D로 생성 된 차트여야 하는데 ""로 값을 넣을 시 B처럼 차트가 생성된다.

해당 셀은 어떤 상태인지는 알 수 없으나 C와 같이 엑셀에서 ' 하나만 넣었을 때와 비슷하게 동작한다.

ctrl + 방향키 아래로 하면 다음 값으로 이동되지 않고 마지막으로 이동을 한다 (아래 gif 참고)

빈 값을 넣었을 때만 생기는 문제로 처리하는 방법은 다양하다.

1. 값을 넣지 않기

2. null로 넣기

3. cell.setBlank(); 

String result = null;
cell.setCellValue(result);
cell.setBlank();

 

Stackoverflow에서는 답변을 주지 않았고 poi 측에 물어보려고 했지만 당연한거를 고민하는것 같아 하지 않았다.

찾아보면 빈 값은 setBlank로 처리한다고 하였다. 버전에 따라서 조금 다를 수 있다.

엑셀에서 값에 따라서 자동으로 처리한다고 생각했고 빈 값을 구분 없이 한 번에 처리하고 싶어서 사용했는데 이런 문제가 있는 줄은 몰랐다..

이전 버전에는 setCellType 로 하는 걸로 보이는데 5.0 에서 보면 Deprecated 로 되어 다른 걸 사용하라고 되어있다.

@Deprecated
@Removal(version="5.0")
void setCellType(CellType cellType)
Deprecated. This method is deprecated and will be removed in POI 5.0. Use explicit setCellFormula(String), setCellValue(...) or setBlank() to get the desired result.
Set the cells type (blank, numeric, boolean, error or string).
If the cell currently contains a value, the value will be converted to match the new type, if possible. Formatting is generally lost in the process however.
Conversion rules:
to NUMERIC: numeric value is left as is. True converts to 1.0, false converts to 0. otherwise, the value is set to 0. Formula is removed.
If what you want to do is get a String value for your numeric cell, stop! This is not the way to do it. Instead, for fetching the string value of a numeric or boolean or date cell, use DataFormatter instead.
If cell is a member of an array formula group containing more than 1 cell, an IllegalStateException is thrown. If the array formula group contains only this cell, it is removed.
Passing CellType.FORMULA is illegal and will result in an IllegalArgumentException.
Throws:
java.lang.IllegalArgumentException - if the specified cell type is invalid (null, _NONE or FORMULA)
java.lang.IllegalStateException - if the current value cannot be converted to the new type or if the cell is a part of an array formula group containing other cells

 

 

https://poi.apache.org/apidocs/5.0/org/apache/poi/ss/usermodel/Cell.html

 

Cell (POI API Documentation)

High level representation of a cell in a row of a spreadsheet. Cells can be numeric, formula-based or string-based (text). The cell type specifies this. String cells cannot conatin numbers and numeric cells cannot contain strings (at least according to our

poi.apache.org

 

- 엑셀 참고

 

- ctrl + 방향키 아래