2017-05-10 1 views
0

기존 Excel을 열고 일부 값을 설정하거나 해당 셀에서 배경/전경색을 변경해야합니다.Apache POI, 잘못된 셀에 배경색 변경

이 액자는 회사에서 근무한 날에 대한 것입니다. 따라서 이미 Excel 스타일이 있습니다. 일부 셀에서는 전경을 변경해야합니다. 상상할 수있는 것처럼, 언젠가는 작동하지 않기 때문입니다.

그래서 매일 (셀에 해당) 동안 "사용 가능"하지 않으면 노란색이되어야합니다.

내 코드는 다음과 같습니다

for(int i=0;i<listDays.size();i++) { 

      int indiceColonna = 6+i; 

      /* Setto la cella GIALLA relativa alla desc corta giorno */ 
      if(!listDays.get(i).isEnabled()) { 
       CellStyle cellaGialla = foglio.getRow(5).getCell(indiceColonna).getCellStyle(); 
       cellaGialla.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); 
       cellaGialla.setFillPattern(FillPatternType.SOLID_FOREGROUND); 
       foglio.getRow(5).getCell(indiceColonna).setCellStyle(cellaGialla); 

      /* Colonne gialle : da G8 a G18 - a scendere */ 
       for (int y = 0; y < 11; y++) { 
        Cell cella = foglio.getRow(7 + y).getCell(6 + i); 
        CellStyle cella2Gialla = cella.getCellStyle(); 
        cella2Gialla.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); 
        cella2Gialla.setFillPattern(FillPatternType.SOLID_FOREGROUND); 
        cella.setCellStyle(cella2Gialla); 
       } 
      } 

     } 

내가 셀의 실제 CellStyle을 단지 전경 색상을 변경하고 싶습니다. 이 코드는 전경색을 다른 열에도 설정했기 때문에 부분적으로 작동합니다. wrong columns

어떻게이 오류를 피할 수 있습니까? Apache POI 3.16을 사용하고 있지만 이전 버전을 사용해 보았습니다.

감사합니다.

+1

새 셀 스타일을 만들고 스타일을 적용하고 적용 하시겠습니까? 셀 스타일은 Excel에서 통합 문서 범위입니다. – Gagravarr

답변

1

나는 또한 비슷한 문제에 직면했으며 Gagravarr가 언급 한 통합 문서에 대한 새로운 스타일을 만드는 솔루션을 발견했다. 아래 코드를 변경하십시오.

CellStyle cella2Gialla = WorkbookObject.createCellStyle();