2013-02-13 4 views
3

jXL API를 사용하는 방법을 배웠습니다. 나는 엑셀 시트를 가지고 있는데, 여기서 나는 진실/거짓 조건에 기초하여 세포의 데이터를 채색하고 싶다. 예를 들어, 조건이 참이면 녹색이어야하고 조건이 실패하면 빨간색이어야합니다.jxl을 사용하여 Excel 시트의 셀에 다른 색상 표시

jxl api를 사용하여 데이터를 Excel 시트에 쓰는 동안이 작업을 수행하려고합니다.

내가 완성하려고 시도한 코드 스 니펫은 다음과 같습니다.

엑셀 시트에 쓰기위한 코드 조각. 각 셀의 서식 속성을 정의하는 메서드를 만들고 wcellFormat1은 WritableCellFormat 형식의 변수입니다.

for(int i=1; i11; i++){ 
      String srnum = String.valueOf(rnum); 
      wsheet.addCell(new jxl.write.Label(1, rc, srnum, wcellFormat1)); 
      wsheet.addCell(new jxl.write.Label(2, rc, "b", wcellFormat1)); 
      wsheet.addCell(new jxl.write.Label(3, rc, "c", wcellFormat1)); 
      wsheet.addCell(new jxl.write.Label(4, rc, "d", wcellFormat1)); 
      wsheet.addCell(new jxl.write.Label(5, rc, "e", wcellFormat1)); 
      wsheet.addCell(new jxl.write.Label(6, rc, "f", wcellFormat1)); 

      rnum++; 
      rc++; 
      System.out.println(""+rnum+"\n"+rc); 
     } 
     wbook.write(); 
     wbook.close(); 

이 코드 단편은 앞에서 언급 한 조건을 적용하기위한 것입니다. wfontStatus는 WritableFont 유형이고 fCellstatus는 형식을 지정하는 데 사용한 WritableCellFormat 유형입니다.

내가 직면 한 문제는 위의 방법을 사용하여 시트에 쓰는 동안 필요한 조건을 적용하는 방법을 이해하지 못한다는 것입니다.

도와주세요. 감사합니다.

답변

3

방법은

public WritableCellFormat createFormatCellStatus(boolean b) throws WriteException{ 
    Colour colour = (b == true) ? Colour.GREEN : Colour.RED; 
    WritableFont wfontStatus = new WritableFont(WritableFont.createFont("Arial"), WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, colour); 
    WritableCellFormat fCellstatus = new WritableCellFormat(wfontStatus); 

    fCellstatus.setWrap(true); 
    fCellstatus.setAlignment(jxl.format.Alignment.CENTRE); 
    fCellstatus.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); 
    fCellstatus.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM, jxl.format.Colour.BLUE2); 
    return fCellstatus; 
} 

처럼 보일 것이다 라벨

을 생성하는 루프 내에서
for(int i=1; i11; i++){ 
    String srnum = String.valueOf(rnum); 
    wsheet.addCell(new jxl.write.Label(1, rc, srnum, createFormatCellStatus(true))); //will create green cell 
    wsheet.addCell(new jxl.write.Label(2, rc, "b", createFormatCellStatus(false))); //will create red cell 
    wsheet.addCell(new jxl.write.Label(3, rc, "c", createFormatCellStatus(false))); 
    wsheet.addCell(new jxl.write.Label(4, rc, "d", createFormatCellStatus(true))); 
    wsheet.addCell(new jxl.write.Label(5, rc, "e", createFormatCellStatus(false))); 
    wsheet.addCell(new jxl.write.Label(6, rc, "f", createFormatCellStatus(true))); 

    rnum++; 
    rc++; 
    System.out.println(""+rnum+"\n"+rc); 
} 
wbook.write(); 
wbook.close(); 
+0

정말 고마워요! –

1

이 방법은 fCellstatus 변수를 업데이트합니다. 그래서 다음과 같은 방식으로 사용할 수 있습니다 :

formatCellStatus(condition); 
wsheet.addCell(new jxl.write.Label(columnNumber, rowNumber, "cellvalue", fCellstatus)); 

나는이 같은 상호 작용에 필드를 포함시키는 것은 좋지 않다고 생각합니다.

wsheet.addCell(new jxl.write.Label(columnNumber, rowNumber, "cellvalue", getCellFormat(condition))); 

내가 모든 세포에 대한 새로운 WritableCellFormat 객체를 생성하는 것은이라고 말할 수 있습니다

public WritableCellFormat getCellFormatByCondition(boolean condition) { 
    if(b == true){ 
     wfontStatus = new WritableFont(WritableFont.createFont("Arial"), WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.GREEN); 
    }else{ 
     wfontStatus = new WritableFont(WritableFont.createFont("Arial"), WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED); 
    } 

    WritableCellFormat result = new WritableCellFormat(wfontStatus); 
    result .setWrap(true); 
    result .setAlignment(jxl.format.Alignment.CENTRE); 
    result .setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); 
    result .setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM, jxl.format.Colour.BLUE2); 
    return result; 
} 

이 방법의 사용이 조금 청소기입니다 : 나는 다음과 같은 방법으로 다시 구현이 방법 제안 자원 낭비. 또한 jxl에는 단일 통합 문서에서 사용되는 형식 수가 제한되어 있으므로 큰 시트에서는 잘못된 서식 문제가 발생합니다.

나는 형식 개체를 재사용 제안 :

private WritableCellFormat GREEN_CELL_FORMAT; 
private WritableCellFormat RED_CELL_FORMAT; 

private void createFormats() { 
    //you'll need to call this before writing workbook 
    //JXL has problems with using one format across several workbooks 
    WritableFont greenFont = new WritableFont(WritableFont.createFont("Arial"), WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.GREEN); 
    WritableFont redFont= new WritableFont(WritableFont.createFont("Arial"), WritableFont.DEFAULT_POINT_SIZE, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED); 
    GREEN_CELL_FORMAT = getCellFormat(greenFont); 
    RED_CELL_FORMAT = getCellFormat(redFont); 
} 

private WritableCellFormat getCellFormat(WritableFont font) { 
    WritableCellFormat result = new WritableCellFormat(font); 
    result .setWrap(true); 
    result .setAlignment(jxl.format.Alignment.CENTRE); 
    result .setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); 
    result .setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM, jxl.format.Colour.BLUE2); 
    return result; 
} 

private WritableCellFormat getCellFormatByCondition(boolean condition) { 
    return condition ? GREEN_CELL_FORMAT : RED_CELL_FORMAT; 
} 

그래서, 당신은 각 통합 문서의 두 CellFormat 객체를 사용할 수 있습니다.

+0

감사합니다. 나는 이것을 시험 할 것이다. –

관련 문제