I'm using java with the api poi library to create an excel spreadsheet.
I need to keep alternating colors, from the 1st column (that is from department), between yellow and blue, when the data are different too.Ex .:
Dep = 1 - Color = yellow
Dep = 1 - Color = yellow
Dep = 2 - Color = blue
Dep = 2 - Color = blue
Dep = 3 - Color = yellow
(and so on)
Every time I change a department I need to change the color, however with the algorithm I thought or got the whole result in yellow or all in blue. I can not think of another algorithm.
HSSFCellStyle style = workbook.createCellStyle();
HSSFPalette palette = workbook.getCustomPalette();
int mudou = 0;
for (int i=0; i <teste.length; i++)
{
row = sheet.createRow(i+5);
if (mudou == 0 )
{
palette.setColorAtIndex(palette.getColor(36).getIndex(), (byte) 255, (byte) 255 , (byte) 204 ); //amarelo
style.setFillForegroundColor(palette.getColor(36).getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
if (i>0 && !teste[i].getDepto().equals(teste[i-1].getDepto()) )
{
mudou = 1;
}
}
if (mudou == 1)
{
palette.setColorAtIndex(palette.getColor(8).getIndex(), (byte) 204, (byte) 255, (byte) 255); //azul
style.setFillForegroundColor(palette.getColor(8).getIndex());
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
if(i+1<teste.length && !teste[i].getDepto().equals(teste[i+1].getDepto()) )
{
mudou = 0;
}
}
row.createCell(0).setCellValue(teste[i].getDepto());
row.getCell(0).setCellStyle(style);
row.createCell(1).setCellValue(teste[i].getSigla());
row.createCell(2).setCellValue(teste[i].getCiclo());
row.createCell(3).setCellValue(teste[i].getState());
row.createCell(4).setCellValue(teste[i].getDataEntradaEstado());
row.createCell(5).setCellValue("");
row.createCell(6).setCellValue(teste[i].getId());
}