0

在此处输入图像描述

在这段代码中,我从列表元素列表中搜索,该元素包含在列表中。为什么当我找到需要的元素时,它开始工作不好。如果我找到另一个包含在列表中的元素,则此单元格应填充为蓝色,但这不会发生。

 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);}
            }
                                                     }
                                                                 }

现在的结果:

(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!

为什么不填蓝色???

4

1 回答 1

0

发现了问题。

 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);}

第二个 else 覆盖第一个 else,真可怜(

于 2013-08-13T11:36:10.687 回答