簡體   English   中英

使用jxl的excel表格中的單元格的不同顏色

[英]different colors for cells in excel sheet using jxl

我一直在學習如何使用jXL API,因為我是新手。 我有一張excel表,我想根據真/假條件為單元格數據着色。 例如,如果條件為真,則必須為綠色,如果條件失敗,則為紅色。

我正在嘗試使用jxl api將數據寫入excel表時實現此目的。

我試圖完成的代碼片段如下。

用於寫入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,我用它來指定格式。

public void formatCellStatus(Boolean b) throws WriteException{

        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);
        }

        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);
    }

我面臨的問題是,我不理解如何使用上述方法在寫入工作表時應用必要條件。

這個你能幫我嗎。 謝謝。

該方法應該類似於

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();

此方法只更新fCellstatus變量。 所以它可以通過以下方式使用:

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

我認為將字段納入這樣的交互並不是一個好主意。 我建議以下列方式重新實現此方法:

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;
}

這種使用方式有點清潔:

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

我不得不說為每個單元創建新的WritableCellFormat對象是浪費資源。 此外,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對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM