반응형
POI를 통한 Excel 시트 헤더 필터 설정
꽤 엉성한 표준 헤더와 데이터 열을 생성합니다.
시트의 「필터」기능을 온으로 해, 유저가 데이터를 간단하게 정렬해 필터링 할 수 있도록 하고 싶다.
POI를 사용해서 해도 될까요?
필터 영역에서 첫 번째 셀과 마지막 셀을 저장하고 다음을 수행합니다.
sheet.setAutoFilter(new CellRangeAddress(firstCell.getRow(), lastCell.getRow(), firstCell.getCol(), lastCell.getCol()));
예를 들어, 아래 시트에서 볼 수 있습니다.
>x (x, y)
0123456
0|--hhh--| h = header
1|--+++--| + = values
2|--+++--| - = empty fields
3|--+++--|
4|-------|
첫 번째 셀은 첫 번째 셀 위의 헤더가 됩니다.+
(2,1) 셀마지막이 마지막이 될 것이다.+
셀(5,3)
헤더에 필터를 추가하는 가장 쉬운 방법:
sheet.setAutoFilter(new CellRangeAddress(0, 0, 0, numColumns));
sheet.createFreezePane(0, 1);
프로그래밍 방식으로 필터를 설정하는 경우에도 다음을 사용할 수 있습니다.
void setAutoFilter(final XSSFSheet sheet, final int column, final String value) {
sheet.setAutoFilter(CellRangeAddress.valueOf("A1:Z1"));
final CTAutoFilter sheetFilter = sheet.getCTWorksheet().getAutoFilter();
final CTFilterColumn filterColumn = sheetFilter.addNewFilterColumn();
filterColumn.setColId(column);
final CTFilter filter = filterColumn.addNewFilters().insertNewFilter(0);
filter.setVal(value);
// We have to apply the filter ourselves by hiding the rows:
for (final Row row : sheet) {
for (final Cell c : row) {
if (c.getColumnIndex() == column && !c.getStringCellValue().equals(value)) {
final XSSFRow r1 = (XSSFRow) c.getRow();
if (r1.getRowNum() != 0) { // skip header
r1.getCTRow().setHidden(true);
}
}
}
}
}
관련 Gradle 의존관계:
// https://mvnrepository.com/artifact/org.apache.poi/poi
compile group: 'org.apache.poi', name: 'poi', version: '3.9'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.9'
// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas
compile group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '3.9'
// https://mvnrepository.com/artifact/org.apache.poi/ooxml-schemas
compile group: 'org.apache.poi', name: 'ooxml-schemas', version: '1.3'
NPOI로 어떻게 하는지 알아냈어
CT_AutoFilter를 CT_Table에 추가합니다.
POI도 NPOI와 동일하게 동작한다고 생각합니다.
cttable.autoFilter = new CT_AutoFilter();
cttable.autoFilter.@ref = "A1:C5"; // value is data and includes header.
사용하다sheet.setAutoFilter(CellRangeAddress.valueOf("B1:H1"));
표 형식의 데이터의 헤더 셀만 지정해야 합니다.이 예의 헤더는 셀 B1에서 시작하여 셀 H1에서 종료됩니다.
Excel은 자동으로 아래의 데이터를 검색하여 필터 옵션에 표시합니다.
언급URL : https://stackoverflow.com/questions/13703441/setting-filter-on-headers-of-an-excel-sheet-via-poi
반응형
'it-source' 카테고리의 다른 글
SQL Server GROUP BY 날짜 및 합계 값이 포함된 선택 시간 무시(분 단위) (0) | 2023.04.26 |
---|---|
WPF/C#: 사용자 기본 설정 파일을 어디에 저장해야 합니까? (0) | 2023.04.21 |
iPhone의 MAC 주소를 프로그래밍 방식으로 취득하려면 어떻게 해야 합니까? (0) | 2023.04.21 |
UNIX pwd와 동등한 Windows (0) | 2023.04.21 |
전달된 인수를 Windows 배치 파일로 리디렉션 (0) | 2023.04.21 |