为 HSSFCellStyle 设置前景色总是黑色 [英] Setting foreground color for HSSFCellStyle is always coming out black

查看:85
本文介绍了为 HSSFCellStyle 设置前景色总是黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 POI 在 Java 中创建 Excel 电子表格.我有以下代码用于创建标题行:

I am using POI to create an Excel spreadsheet in Java. I have the following code used for creating a header row:

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Report");

// some more code

HSSFRow row = sheet.createRow(0);

HSSFCell cell = row.createCell(cellNumber);
HSSFCellStyle cellStyle = wb.createCellStyle();

cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFFont font = wb.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.WHITE.index);

cellStyle.setFont(font);
cell.setCellStyle(cellStyle);

我遇到的问题是,无论我选择什么颜色,在单元格上设置填充背景颜色总是黑色.我究竟做错了什么?如果我不使用setFillPattern"行,则根本不会显示任何颜色.

The issue I am having is that setting the fill background color on the cell always comes out black, no matter what color I pick. What am I doing wrong? If I don't use the "setFillPattern" line, no color shows up at all.

推荐答案

我让它起作用了.我必须设置前景色才能使背景色起作用 (??).

I got this to work. I had to set the foreground color to make the background color work (??).

所以我改变了:

cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

到:

cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

它奏效了!

这篇关于为 HSSFCellStyle 设置前景色总是黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆