首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在Apache POI XSSF中看到黑色背景

我在Apache POI XSSF中看到黑色背景
EN

Stack Overflow用户
提问于 2017-03-31 22:09:58
回答 1查看 1.2K关注 0票数 1

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

代码语言:javascript
复制
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);

这段代码对一行中的某些单元格工作得很好。但是,它为单元格中的其他行打印黑色背景。下面是我做核心工作的函数。

代码语言:javascript
复制
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);
        }
EN

回答 1

Stack Overflow用户

发布于 2019-02-27 00:32:46

使用setFillForegroundColor()而不是setFillBackgroundColor()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43142793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档