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);
                    }
                }
            }
        }
    }
相关推荐
Access开发易登软件2 天前
数据处理中的两大基石:何时选择Excel,何时考虑Access
数据库·信息可视化·excel·vba·access
开开心心就好2 天前
PDF密码移除工具,免费解除打印编辑复制权限
java·网络·windows·websocket·pdf·电脑·excel
weixin_462446232 天前
Python 实战:将 HTML 表格一键导出为 Excel(xlsx)
linux·python·excel·pandas
Data-Miner2 天前
Excel-Agent永久买断,一款结合AI Agent的excel数据处理智能体
人工智能·excel
weixin_462446232 天前
Python 使用 openpyxl 从 URL 读取 Excel 并获取 Sheet 及单元格样式信息
python·excel·openpyxl
wtsolutions2 天前
MCP Server Integration - JSON to Excel for AI and Automation
json·excel
开开心心就好2 天前
音频编辑工具,多端支持基础剪辑易操作
java·网络·windows·java-ee·电脑·maven·excel
vfvfb2 天前
excel多个合并 xlsx工作表合并 多个excel合并到一张表
excel
金融小白数据分析之路3 天前
msoffcrypto-tool库 Excel 加密
python·excel
开开心心_Every3 天前
文件数量统计工具:支持多层文件夹数量统计
游戏·微信·pdf·excel·语音识别·swift·lisp