Java 设置背景自定义颜色,XSSF工作簿

Java 设置背景自定义颜色,XSSF工作簿,java,excel,apache,Java,Excel,Apache,我使用此代码将excel中的字体更改为我定义的颜色 Color sColor = new Color (value,0,0); XSSFColor userColor = new XSSFColor(sColor); CellStyle style = wb.createCellStyle(); XSSFFont font = wb.createFont(); font.setColor(userColor);

我使用此代码将excel中的字体更改为我定义的颜色

        Color sColor = new Color (value,0,0);
        XSSFColor userColor = new XSSFColor(sColor);

        CellStyle style = wb.createCellStyle();
        XSSFFont font = wb.createFont();

        font.setColor(userColor);
        style.setFont(font);
        cell.setCellStyle(style);
我可以用同样的方法更改单元格的背景吗

我在这里看到了这个问题 我使用了代码:

        XSSFCellStyle cellStyle = wb.createCellStyle();
        XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));
        ((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);

        cell.setCellStyle(cellStyle);
背景仍然是白色的

我确信代码的所有其他部分都写得很好,因为当我更改字体时它就可以工作了


我的计算机上有office 2010

创建单元格样式对象:

CellStyle backgroundStyle = workbook.createCellStyle(); 
设置自定义颜色:

backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
将样式添加到单元格:

cell.setCellStyle(backgroundStyle);

创建单元样式对象:

CellStyle backgroundStyle = workbook.createCellStyle(); 
设置自定义颜色:

backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
将样式添加到单元格:

cell.setCellStyle(backgroundStyle);
谢谢大家 我现在找到了解决办法

        XSSFCellStyle cellStyle = wb.createCellStyle();

        XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));

        ((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(color); 
        cell.setCellStyle(cellStyle);
谢谢大家 我现在找到了解决办法

        XSSFCellStyle cellStyle = wb.createCellStyle();

        XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));

        ((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(color); 
        cell.setCellStyle(cellStyle);

但我在“什么错误?”?编译器错误?运行时错误?什么是错误/它应该做什么而不是做什么?@scigs编辑后你能看到这个问题吗?编译器错误?运行时错误?什么是错误/它应该做什么而不是它?@scigs编辑后你能看到这个问题吗。不幸的是,在现代POI 4.1.2中不起作用。我们无法再将
XSSFColor
设置为
setFillForegroundColor
:(不幸的是,在现代POI 4.1.2中不起作用。我们无法再将
XSSFColor
设置为
setFillForegroundColor
:(