我在Apache POI XSSF中获得黑色背景

时间:2017-03-31 14:09:58

标签: apache apache-poi

我想为具有不同单元格的行提供自定义颜色。

XSSFWorkbook wb = new XSSFWorkbook();
                String safeName = WorkbookUtil.createSafeSheetName("Main Result"); // returns " O'Brien's sales   "
                XSSFSheet sheet1 = wb.createSheet(safeName);

                XSSFCellStyle[] style=new XSSFCellStyle[12];
style[4] = (XSSFCellStyle)wb.createCellStyle();
                style[4].setFillBackgroundColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
                style[4].setFillPattern(CellStyle.BIG_SPOTS);

此代码适用于连续的某些单元格。但是,它为单元格中的其他行打印黑色背景。以下是我正在做核心事情的功能。

public static void merge(int i,int j,Sheet sheet1,ArrayList<Room> rooms)
    {
        sheet1.addMergedRegion(new CellRangeAddress(
                i, //first row (0-based)
                i+1, //last row  (0-based)
                j+1, //first column (0-based)
                j+3  //last column  (0-based)
        ));
        sheet1.addMergedRegion(new CellRangeAddress(
                i, //first row (0-based)
                i, //last row  (0-based)
                j+4, //first column (0-based)
                j+4+rooms.size()-1  //last column  (0-based)
        ));
    }

    public static void printRooms(int i,int j,Sheet sheet1,ArrayList<Room> rooms)
    {
        Row row2=sheet1.getRow(i+1);
        if(row2==null)
        row2=sheet1.createRow(i+1);
        for(int k=0;k<rooms.size();k++)
        {
            Cell cell=row2.createCell(j+4+k);
            cell.setCellValue(rooms.get(k).getRoom_no());
            //cell.setCellStyle(style[1]);
        }
    }
    //this function prints all the data in excel sheet for a particular SLOT and TIME INTERVAL
    public static void printInExcel(Map<Integer,String> batch_id_name,
            Map<Integer,Integer> line,Map<Integer,Integer> range,Sheet sheet1,int j,XSSFCellStyle[] style,Set<Course> set,TimeInterval t1,Workbook wb,int flag) throws ClassNotFoundException, DAOException, SQLException
    {
        int flag2=0;//to print labels just for first time.
        int border_first=0;
        int border_last=0;
        for(int batch_id=1;batch_id<=batch_id_name.size();batch_id++)
        {
            //check if such batch_id exists
            if(!line.containsKey(batch_id))
                continue;
            //Merge Rows

            int start=line.get(batch_id);
            int end=start+range.get(batch_id)-1;
            int i=start-2;
        //print default messages


            ArrayList<Room> rooms=GeneralDAO.getRooms();

            Row row1=sheet1.getRow(i);
            if(row1==null)
            row1 = sheet1.createRow(i);
            //if flag2==0,it means it's first iteration
            if(flag2==0)
            {   border_first=i;
                if(flag==1)
                {
                    //System.out.println("Working");
                    TimeTable.merge(i, j, sheet1, rooms);
                    row1.createCell(j+1).setCellValue(" 08:30 - 10:30 ");
                    row1.createCell(j+4).setCellValue(" CEP Rooms ");
                    TimeTable.printRooms(i, j, sheet1, rooms);
                    flag2=1;
                }
                else if(flag==2)
                {
                    TimeTable.merge(i, j, sheet1, rooms);
                    row1.createCell(j+1).setCellValue(" 11:00 - 13:00 ");
                    row1.createCell(j+4).setCellValue(" CEP Rooms ");
                    TimeTable.printRooms(i, j, sheet1, rooms);
                    flag2=1;
                }
                else if(flag==3)
                {   TimeTable.merge(i, j, sheet1, rooms);
                    row1.createCell(j+1).setCellValue(" 14:00 - 16:00 ");
                    row1.createCell(j+4).setCellValue(" CEP Rooms ");
                    TimeTable.printRooms(i, j, sheet1, rooms);
                    flag2=1;
                }
                else if(flag==4)
                {
                    TimeTable.merge(i, j, sheet1, rooms);
                    row1.createCell(j+1).setCellValue(" 16:30 - 18:30 ");
                    row1.createCell(j+4).setCellValue(" CEP Rooms ");
                    TimeTable.printRooms(i, j, sheet1, rooms);
                    flag2=1;
                }

            }

            Row row=sheet1.getRow(start);
            if(row==null)
                row=sheet1.createRow(start);
            //System.out.println("batch_id"+batch_id+"start: "+start+"end: "+end);

            if(flag==1)
            {
                if((end-start)!=0)
                {   
                sheet1.addMergedRegion(new CellRangeAddress(
                        start, //first row (0-based)
                        end, //last row  (0-based)
                        j, //first column (0-based)
                        j  //last column  (0-based)
                ));
                }
            // Write Batch Name

            row.createCell(j).setCellValue(batch_id_name.get(batch_id));
            row.getCell(j).setCellStyle(style[batch_id-1]);
            }
            //Start allocating courses
            for(Course course:set)
            {

                if(Integer.parseInt(course.getBatch())==batch_id)
                {
                     Row row_temp = sheet1.getRow(start);
                     if(row_temp==null)
                     {
                         row_temp=sheet1.createRow(start);
                     }

                        row_temp.createCell(j+1).setCellValue(course.getCourse_id());
                        row_temp.getCell(j+1).setCellStyle(style[batch_id-1]);
                        row_temp.createCell(j+2).setCellValue(course.getCourse_name());
                        row_temp.getCell(j+2).setCellStyle(style[batch_id-1]);
                       // sheet1.createRow(80).createCell(j+1).setCellStyle(style[10]);
                        ArrayList<Room> temp_rooms=t1.getRooms();
                         //first column for timeinterval1
                        int first_col=j+4;
                        for(int p=0;p<temp_rooms.size();p++)
                        {

                            System.out.println("Running"+p);

                            if(!t1.getMap().containsKey(temp_rooms.get(p).getRoom_no()))
                                continue;
                            for(OccupationData od:t1.getMap().get(temp_rooms.get(p).getRoom_no()))
                            {           

                                if(od.getCourse().getCourse_id()==course.getCourse_id())
                                {
                                    row_temp.createCell(p+first_col).setCellValue(od.getAllocatedStudents());
                                    row_temp.getCell(p+first_col).setCellStyle(style[batch_id-1]);
                                }
                            }

                        }
                        start++;
                }
            }

            border_last=end;//last iteration will set last end(although it sets this variable multiple times)
            }
         PropertyTemplate pt = new PropertyTemplate();
          // #1) these borders will all be medium in default color
          pt.drawBorders(new CellRangeAddress(border_first, border_last, j+1, j+8),
                  BorderStyle.MEDIUM,BorderExtent.OUTSIDE);
          if(flag==1)
          {
          pt.drawBorders(new CellRangeAddress(border_first, border_last, j, j),
                  BorderStyle.MEDIUM,BorderExtent.OUTSIDE); 
          sheet1.addMergedRegion(new CellRangeAddress(
          border_first, //first row (0-based)
          border_last, //last row  (0-based)
          j-1, //first column (0-based)
          j-1));  //last column  (0-based)
          Row temp_row=sheet1.getRow(border_first);
          if(temp_row==null)
              temp_row=sheet1.createRow(border_first);
          //temp_row.createCell(j-1,CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY).setCellValue("Exam Day");
          TimeTable.createCell(wb, temp_row, j-1, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_CENTER);
          pt.drawBorders(new CellRangeAddress(border_first, border_last, j-1, j-1),
                  BorderStyle.MEDIUM,BorderExtent.OUTSIDE);
          }
          pt.applyBorders(sheet1);
    }
     private static void createCell(Workbook wb, Row row, int column, short halign, short valign) {
            Cell cell = row.createCell(column);
            cell.setCellValue(" Exam Day ");
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setAlignment(halign);
            cellStyle.setVerticalAlignment(valign);
            cellStyle.setRotation((short)90);
            cell.setCellStyle(cellStyle);
        }

1 个答案:

答案 0 :(得分:0)

使用setFillForegroundColor()代替setFillBackgroundColor()