티스토리 뷰

Dev/Java

[Java] Excel 특정 값을 찾아서 원하는 셀에 색상 채우기 (POI)

꿈을 위해 잠을 잊은 그대에게 2020. 3. 12. 05:46

POI 읽기/쓰기(read/write)를 구현하는 중 Excel의 특정 값을 찾아서 원하는 셀에 색상을 채우고 싶었다.

 

참고자료 예제

https://swk3169.tistory.com/entry/Java-Excel-%EC%97%91%EC%85%80-ReadWrite-%EC%9D%BD%EA%B8%B0%EC%93%B0%EA%B8%B0-POI

 

POI 기존버전 아무리 해도 색상이 안채워짐.

 

style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); //채우기 선택 style.setFillPattern(CellStyle.SOLID_FOREGROUND); //채우기 적용(이거안하면 안채운다)

 

 

 

stackoverflow에 의하면 POI의 3.17버전 javadoc에서 해당 버전을 remove 했다고 한다.

따라서 다음과 같이 제시한 해결방법을 찾았다.

 

style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

 

According to the javadoc from version 3.17 that field was removed.

Use FillPatternType.SOLID_FOREGROUND instead.

From source code of apache-poi 3.15 I can see:

 

/** * Fill Pattern: Solidly filled * @deprecated 3.15 beta 3. Use {@link FillPatternType#SOLID_FOREGROUND} instead. */ @Removal(version="3.17") static final short SOLID_FOREGROUND = 1; //FillPatternType.SOLID_FOREGROUND.getCode();

 

참고자료 링크

https://stackoverflow.com/questions/52871563/solid-foreground-cannot-be-resolved-or-is-not-a-field/52873991

 

SOLID_FOREGROUND cannot be resolved or is not a field

SOLID_FOREGROUND cannot be resolved or is not a field style.setFillPattern(CellStyle.SOLID_FOREGROUND); Using 3.7 version of apache-poi I am getting the following issue. Please help on this .

stackoverflow.com

 

해결 소스코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void writeExcelFile(List<AgencyDTO> list) throws EncryptedDocumentException, IOException {
        String filePath = "[첨부2]기관별간접비 고시기준_transfer.xlsx";    // 저장할 파일 경로
        
        FileOutputStream fos = new FileOutputStream(filePath);
        
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("agencyList");    // sheet 생성
        
        XSSFRow curRow;
        
        XSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());  // 배경색
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        
        int row = list.size();    // list 크기
        for (int i = 0; i < row; i++) {
            curRow = sheet.createRow(i);    // row 생성
            
            Cell cell = curRow.createCell(0);
            Cell cell1 = curRow.createCell(1);
            Cell cell2 = curRow.createCell(2);
            if (list.get(i).getName().equals("경북대학교")) {
                cell.setCellStyle(style);
                cell1.setCellStyle(style);
                cell2.setCellStyle(style);
            }
            cell.setCellValue(list.get(i).getName());
            cell1.setCellValue(list.get(i).getRate());
            cell2.setCellValue(list.get(i).getPercent());
        }
        
        workbook.write(fos);
        fos.close();
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크