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);
                    }
                }
            }
        }
    }
相关推荐
泡海椒4 小时前
JQuick-Excel dateFormat 转换实战:日期值与 FORMAT 显示格式的边界
开发语言·python·excel
2501_933670796 小时前
2027风控策略岗秋招准备:SQL、Excel、建模的优先级与项目路径
人工智能·sql·excel
流形填表7 小时前
在线考试平台导入题库:Excel格式与字段对照
excel
开开心心就好1 天前
办公软件卸载不干净?专用工具一键清残留
java·前端·人工智能·智能手机·kafka·excel·memcache
全速向光1 天前
Excel函数系列09:公式又长又乱?LET+LAMBDA+MAP+REDUCE,自定义函数一次讲透
excel
鲲穹AI种草1 天前
办公表格批量处理:多款 Excel 工具能力客观记录
excel
开开心心就好1 天前
二维码批量生成导出工具,离线可用完全免费
java·前端·人工智能·智能手机·github·excel·visual studio
泡海椒1 天前
JQuick-Excel FORMAT 导出格式实战:日期、金额显示与 TRANSFORM 的边界
前端·python·excel
流形填表2 天前
题库迁移实战:Excel中间格式与列映射
python·excel
泡海椒2 天前
JQuick-Excel JQuickRow 数据准备实战:JObjectConverter、JQuickRow.toRows 与 XML 导出链路
xml·windows·excel