I have been learning how to use jXL API as I'm new to it. I have an excel sheet, where I want to color the cells' data based on an true/false condition. For ex, if the condition is true, it has to be green and if the condition fails, red.
I'm trying to achieve this while writing data into excel sheet using jxl api.
The snippets of the code I have been trying to complete is as follows.
Code snippet for writing into excel sheet. I have created a method to define format properties for each cell and wcellFormat1 is a variable for the same, which is of type 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();
This code snippet is for applying the conditions which I have mentioned before. wfontStatus is of type WritableFont and fCellstatus is of type WritableCellFormat which i have used for specifying formats.
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);
}
The problem I'm facing is that I'm not understanding how to use the above method to apply the conditions necessary while writing into sheet.
Please help me out with this. Thank you.
The method should look something like
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;
}
and inside the loop that creates labels
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();