2013-02-13 164 views
3

我一直在学习如何使用jXL API,因为我是新手。我有一个Excel表,我想根据真/假条件为单元格的数据着色。例如,如果条件为真,它必须是绿色的,如果条件失败,则为红色。excel表格中使用jxl的单元格的不同颜色

我试图在使用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); 
    } 

我面临的问题是我不理解如何使用上面的方法来应用写入表单时所需的条件。

请帮我解决这个问题。 谢谢。

回答

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

非常感谢! – 2013-02-14 05:54:03

1

此方法只更新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对象为每个工作簿。

+0

谢谢。我会试试这个:) – 2013-02-14 05:54:24