What is wrong with this cycle. HSSF

133 Views Asked by At

enter image description here

In this code i'm searching from List of Lists element, which contains in List. Why when i found needed elements, it's starting work bad. If i find another element, which contains in List, this cell should be fill in BLUE color, but that do not happens.

 HSSFWorkbook workbook = new HSSFWorkbook(file);
        CreationHelper createHelper = workbook.getCreationHelper();
        //Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(IndexedColors.LIME.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        CellStyle style2 = workbook.createCellStyle();
        style2.setFillForegroundColor(IndexedColors.RED.getIndex());
        style2.setFillPattern(CellStyle.SOLID_FOREGROUND);
        CellStyle style3 = workbook.createCellStyle();
        style3.setFillForegroundColor(IndexedColors.BLUE.getIndex());
        style3.setFillPattern(CellStyle.SOLID_FOREGROUND);



  for (int i=0;i<ListOfList.size();i++)                      {
        int count=0;
        label1:
        for (int j=0;j<ListOfList.get(i).size();j++) {
            Row row = sheet.getRow(i);
            for (int k=0;k<List.size();k++){
                if (List.get(k).equals(ListOfList.get(i).get(j))) {
                    count++;
                    if (count==1) {
                        System.out.println("("+List.get(k)+") ASU <--> ("+ListOfList.get(i).get(j)+") RZS "+"String: "+i+" Column: "+j);
                        row.createCell(2+j).setCellValue(ListOfList.get(i).get(j));
                        row.getCell(2+j).setCellStyle(style);
                        Row row2 = sheet.getRow(k);
                        row2.createCell(0).setCellValue(List.get(k));
                        row2.getCell(0).setCellStyle(style);
                        continue label1;     }
                    else{                     // count!=1
                        System.out.println("Another element contain!");
                        row.createCell(2+j).setCellValue(ListOfList.get(i).get(j));
                        row.getCell(2+j).setCellStyle(style3);}

                }
                else {row.createCell(2+j).setCellValue(ListOfList.get(i).get(j));
                    row.getCell(2+j).setCellStyle(style2);}
            }
                                                     }
                                                                 }

The result now:

(222) ASU <--> (222) RZS String: 0 Column: 1
Another element contain!
Another element contain!
Another element contain!
(233) ASU <--> (233) RZS String: 1 Column: 1
(244) ASU <--> (244) RZS String: 2 Column: 1
Another element contain!
Another element contain!
Another element contain!
Another element contain!

Why it not filled in BLUE????

1

There are 1 best solutions below

0
On

Found the problem.

 else{                     // count!=1
                System.out.println("Another element contain!");
                row.createCell(2+j).setCellValue(ListOfList.get(i).get(j));
                row.getCell(2+j).setCellStyle(style3);}

        }
        else {row.createCell(2+j).setCellValue(ListOfList.get(i).get(j));
            row.getCell(2+j).setCellStyle(style2);}

the 2nd else overwrite 1st else, what a pitty(