java-使用jxl为Excel工作表中的单元格提供不同的颜色

提问

我一直在学习如何使用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();