easypoi 自定义样式 学生-分数红绿颜色设置

需求

思路:对表格遍历

java 复制代码
// 对导出完成的工作簿进行后处理,设置单元格颜色
setScoreColors(workbook);

// 后处理方法,专门设置分数颜色
    private static void setScoreColors(Workbook workbook) {
        Sheet sheet = workbook.getSheetAt(0);

        // 获取分数列的索引(根据表头查找)
        int scoreColIndex = -1;
        Row headerRow = sheet.getRow(1); // 表头行通常是第二行(索引1)

        for (int i = 0; i < headerRow.getLastCellNum(); i++) {
            Cell cell = headerRow.getCell(i);
            if (cell != null && "分数".equals(cell.getStringCellValue())) {
                scoreColIndex = i;
                break;
            }
        }

        if (scoreColIndex == -1) {
            return; // 没找到分数列
        }

        // 创建红色和绿色样式
        CellStyle redStyle = workbook.createCellStyle();
        redStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        redStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        redStyle.setAlignment(HorizontalAlignment.CENTER);
        redStyle.setBorderBottom(BorderStyle.THIN);
        redStyle.setBorderLeft(BorderStyle.THIN);
        redStyle.setBorderRight(BorderStyle.THIN);
        redStyle.setBorderTop(BorderStyle.THIN);

        CellStyle greenStyle = workbook.createCellStyle();
        greenStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        greenStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        greenStyle.setAlignment(HorizontalAlignment.CENTER);
        greenStyle.setBorderBottom(BorderStyle.THIN);
        greenStyle.setBorderLeft(BorderStyle.THIN);
        greenStyle.setBorderRight(BorderStyle.THIN);
        greenStyle.setBorderTop(BorderStyle.THIN);

        // 从数据行开始遍历(跳过标题行和表头行)
        for (int i = 2; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            if (row != null) {
            		//获取列
                Cell scoreCell = row.getCell(scoreColIndex);
                if (scoreCell != null) {
                    // 安全地获取分数值
                    int score;
                    try {
                        // 尝试直接获取数值
                        if (scoreCell.getCellType() == CellType.NUMERIC) {
                            score = (int) scoreCell.getNumericCellValue();
                        } else {
                            // 如果是字符串,则解析为整数
                            score = Integer.parseInt(scoreCell.getStringCellValue().trim());
                        }

                        // 设置单元格样式
                        if (score < 60) {
                            scoreCell.setCellStyle(redStyle);
                        } else if (score > 90) {
                            scoreCell.setCellStyle(greenStyle);
                        }
                    } catch (Exception e) {
                        // 转换失败,保持原样
                        log.error("处理单元格时出错:", e);
                    }
                }
            }
        }
    }
相关推荐
LAM LAB3 天前
【VBA】Excel指定单元格范围内字体设置样式,处理导出课表单元格
excel·vba
在这habit之下3 天前
Keepalived学习总结
excel
Youngchatgpt3 天前
如何在 Excel 中使用 ChatGPT:自动化任务和编写公式
人工智能·chatgpt·自动化·excel
开开心心就好3 天前
安卓开源应用,超时提醒紧急人护独居安全
windows·决策树·计算机视觉·pdf·计算机外设·excel·动态规划
D_C_tyu3 天前
Vue3 + Element Plus | el-table 多级表头表格导出 Excel(含合并单元格、单元格居中)第二版
vue.js·elementui·excel
骆驼爱记录4 天前
WPS页码设置:第X页共Y-1页
自动化·word·excel·wps·新人首发
Cxiaomu5 天前
Python 文件解析: Excel / Word / PDF 的解析、处理、预览与下载
python·word·excel
2501_930707785 天前
如何使用C#代码从 PDF 中提取表格并另存为Excel文件
pdf·excel
pacong5 天前
B生所学EXCEL
人工智能·excel