Java 如何使用APACHE POI HSSF突出显示excel中的单元格

Java 如何使用APACHE POI HSSF突出显示excel中的单元格,java,excel,apache-poi,Java,Excel,Apache Poi,代码将值从studentList第3行写入studentList.size+2到第0列的每一行,并将值从gradesList第1行从第1列写入gradesList.size+1到文件考勤表 P>我应该编辑什么,我可以用浅绿色、浅黄色、Titleheader黄色和其他浅蓝色的空白盒子来突出所有的学生名单? 我需要帮助将我的工作表从图像1转换为图像2您正在第5行中创建背景色,但在创建边框时将其覆盖。您必须同时应用所有样式才能解决此问题 除此之外,还必须包括FillPattern,例如: pub

代码将值从studentList第3行写入studentList.size+2到第0列的每一行,并将值从gradesList第1行从第1列写入gradesList.size+1到文件考勤表

<> P>我应该编辑什么,我可以用浅绿色、浅黄色、Titleheader黄色和其他浅蓝色的空白盒子来突出所有的学生名单?


我需要帮助将我的工作表从图像1转换为图像2

您正在第5行中创建背景色,但在创建边框时将其覆盖。您必须同时应用所有样式才能解决此问题

除此之外,还必须包括FillPattern,例如:

public class createChart {

public static void main(String[] args) {
    ArrayList<Integer> studentList = new ArrayList<>();
    ArrayList<Integer> gradeList = new ArrayList<>();
    ArrayList<String> header = new ArrayList<>();

    header.add("Attendance Sheet");

    for(int i = 1; i <= 20; i++){
        studentList.add(i);
        if(i <= 20){
            gradeList.add((80+i));
        }

    }

    int bordernum = 2;
    try {
        FileOutputStream fileOut = new FileOutputStream("Attendance Sheet.xls");
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet worksheet = workbook.createSheet("Attendance sheet");



        // row 1 for Prinitng attendance sheet in center
        HSSFRow row0 = worksheet.createRow((short) 0);//1
        HSSFCell cellmid = row0.createCell((short) (gradeList.size()/2)-1);//2
        cellmid.setCellValue(header.get(0));//3
        HSSFCellStyle cellStylem = workbook.createCellStyle();//4
        cellStylem.setFillForegroundColor(HSSFColor.GOLD.index);//5
        cellmid.setCellStyle(cellStylem);//6
        createBorders(workbook, cellmid, 1);
        HSSFCell cellmid2 = row0.createCell((short) (gradeList.size()/2));//2
        createBorders(workbook, cellmid2, 1);



        // row 2 with all the dates in the correct place
        HSSFRow row1 = worksheet.createRow((short) 1);//1
        HSSFCell cell1;
        for(int y = 0; y < gradeList.size(); y++){

            cell1 = row1.createCell((short) y+1);//2
            cell1.setCellValue(gradeList.get(y));//3
            createBorders(workbook, cell1, bordernum);

        }
        HSSFCellStyle cellStylei = workbook.createCellStyle();//4
        cellStylei.setFillForegroundColor(GREEN.index);//5



        // row 3 and on until the studentList.size() create the box.
        int counter = 0;
        for(int stu = 2; stu <= (studentList.size()+1); stu++){
            HSSFRow Row = worksheet.createRow((short) stu);//1
            for(int gr = 0; gr <= gradeList.size(); gr++){
                if(gr == 0){
                    HSSFCell cell = Row.createCell((short) 0);//2
                    cell.setCellValue(studentList.get(counter));//3
                    HSSFCellStyle cellStyle2 = workbook.createCellStyle();//4
                    cellStyle2.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                    cellStyle2.setFillForegroundColor(HSSFColor.GOLD.index);//5
                    cell.setCellStyle(cellStyle2);//6
                    createBorders(workbook, cell, 2);
                }else{
                    HSSFCell Cell = Row.createCell((short) gr);//2
                    createBorders(workbook, Cell, 3);
                }


            }
            counter++;
        }
        workbook.write(fileOut);
        fileOut.flush();
        fileOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void createBorders(HSSFWorkbook workbook,HSSFCell cell, int x){
    if( x == 1){
        HSSFCellStyle style = workbook.createCellStyle();
        //style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.LIGHT_BLUE.getIndex());
        //style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THICK);
        style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THICK);
        style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderTop(BorderStyle.THICK);
        style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        cell.setCellStyle(style);
    }
    else if(x == 2){
        HSSFCellStyle style = workbook.createCellStyle();
        //style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.LIGHT_BLUE.getIndex());
        //style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.MEDIUM);
        style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.MEDIUM);
        style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderRight(BorderStyle.MEDIUM);
        style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderTop(BorderStyle.MEDIUM);
        style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        cell.setCellStyle(style);
    }else {
        HSSFCellStyle style = workbook.createCellStyle();
        //style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.AQUA.getIndex());
        //style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex());
        cell.setCellStyle(style);
    }

}
您可以在

希望这能有所帮助。
谢谢。

我没有投反对票,但对我来说很像家庭作业。还有,你试过什么了吗?你有什么想法吗?你能告诉我在哪一行做什么吗?我试着把style.setFillPatternCellStyle.SOLID\u放在前台//6但没有任何变化同样,Apache Poi快速指南<填充和颜色他们使用XSSF表,而我使用的是HSSFIn您的createBorders方法,您可以添加线条style.setFillForegroundColorHSSFColor.GREEN.index;和style.setFillPatternCellStyle.SOLID\u前景;例如,当X==2时,可以查看如何将背景添加到这些单元格中。然后,您可以使用它作为一个例子来应用样式Hanks for Reply现在的问题是,一旦我这样做,所有单元格都高亮显示为黑色,即使我使用的是不同的颜色,如果x==1{HSSFCellStyle style=workbook.createCellStyle;style.setFillBackgroundColorHSSFColor.HSSFColorPredefined.LIGHT_BLUE.getIndex;style.setFillPatternFillPatternType.SOLID_前台;style.setBorderBottomBorderStyle.THICK;style.SetBottomBorderBorderColorHSSFColor.HSSFColor Predefined.BLACK.getIndex;style.setBorderLeftBorderStyle.THICK;执行此操作后,所有内容都高亮显示为黑色,我认为这与边框颜色有关
        style.setFillForegroundColor(HSSFColor.GOLD.index);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);