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 小时前
【苍穹外卖 Day12| Excel 入门】
excel
用户298698530149 小时前
Python 数据处理:XML 与 Excel 互转的实用指南
后端·python·excel
xiaopai9451 天前
采购系统和Excel表格区别:钢结构项目物资管理的真实较量
excel·建米软件·采购系统
协享科技1 天前
2026 年 AI 表格工具怎么选:ChatGPT、原生 Copilot/Gemini 与公式助手
人工智能·chatgpt·excel·copilot
用户298698530141 天前
前端实战:在 React 中使用 JavaScript 一键导出 Excel 图表与形状为图片
javascript·react.js·excel
Q一件事1 天前
arcpy实现分区统计到一张excel中
excel
AI刀刀1 天前
deepseek 内容粘贴后符号丢失怎么办?AI 导出鸭实测解决排版乱码问题
android·人工智能·excel·ai导出鸭
圣光SG2 天前
Excel 函数大全
算法·excel
小小的木头人3 天前
Python 批量解析 Excel 经纬度,调用高德地图 API 获取中文地址
开发语言·python·excel
观远数据3 天前
从离线开发到实时同步:DataFlow如何支撑企业级数据治理闭环
java·windows·microsoft·excel