셀 병합을 위해 사용하는 클래스 CellRangeAddress, addMergedRegion

 

사용하면서 느낀 특징을 간단히 요약하면

 

셀을 병합할때는 데이터가 들어가는 셀의 위치값은 변하지 않는다.
셀에 들어갈 값을 먼저 넣어주고 
가로(열) 병합은 병합하고자하는 첫 셀을 만든 후
세로(행) 병합은 병합하고자하는 마지막행의 셀을 만든 후에 한다

 

 

정의>

HSSFSheet sheet = wb.createSheet("sheet명");

sheet.addMergedRegion(new CellRangeAddress(첫행, 마지막행, 첫열, 마지막열));

 

 

사용>

for 문을 사용해서 데이터를 저장하겠지만 풀어보면 이렇게 사용될 것이다.

 

- 가로(열) 병합: B1, C1 셀 병합

...

// 1행 생성

row = sheet.creatRow(0);

// A열 생성
cell = row.creatCell(0);

// 엑셀의 스타일 적용
cell.setCellStyle(style);

// B열 생성
cell = row.creatCell(1);

// 엑셀의 스타일 적용
cell.setCellStyle(style);
// B1에 들어갈 값 저장
cell.setCellValue("값");

// B1, C1 셀 병합
sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 2));

// C열 생성
cell = row.creatCell(2);

// 엑셀의 스타일 적용
cell.setCellStyle(style);

...

 

 

출처>

apache 공식사이트 문서

https://poi.apache.org/apidocs/dev/org/apache/poi/ss/util/CellRangeAddress.html

 

public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)

Creates new cell range. Indexes are zero-based.

Parameters:

firstRow - Index of first row

lastRow - Index of last row (inclusive), must be equal to or larger than firstRow

firstCol - Index of first column

lastCol - Index of last column (inclusive), must be equal to or larger than firstCol

 

 

 

+ Recent posts