如何使用Java代码使用HSSF应用单元格背景色 [英] How to apply cell background color using HSSF using Java code

查看:1082
本文介绍了如何使用Java代码使用HSSF应用单元格背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据测试用例执行状态来设置单元格背景颜色的格式,例如如果测试用例通过,则单元格背景应变为绿色,文本颜色应为白色.

I'm trying to format the cell background color based on the Testcase Execution status like if the test case got passed then the cell background should become Green and text color should be White.

对于失败的测试案例,单元格背景颜色:红色和文本颜色:白色

Similarly for Failed test cases cell background color : Red and Text color : White

为此,我尝试了以下脚本.

For that I tried the following script.

背景:

HSSFCellStyle style = wBook.createCellStyle()
style.setFillBackgroundColor(IndexedColors.GREEN.getIndex())

前景:

HSSFFont font = wBook.createFont()
font.setColor(HSSFColor.WHITE.index)
style.setFont(font)
resultCell.setCellStyle(style)

但是在执行了测试用例之后,单元格背景就不再适用于仅适用于前景的地方.

But after executing the test cases, cell background is not applying where as foreground only applies.

仅供参考:我正在使用Excel版本.XLS

FYI: I'm Working with Excel version .XLS

有人提供正确的方法来应用细胞背景吗?

Anyone give the correct method to apply background of the cell?

谢谢

推荐答案

您错在摆弄. Excel的单元格填充是图案填充.填充背景色是图案后面的颜色,填充前景色和图案的颜色.

You are fiddling with the wrong. Excel's cell fills are pattern fills. There fill background color is the color behind the pattern and fill foreground color ist the color of the pattern.

因此,如果设置setFillBackgroundColor,那么您正在设置图案后面的颜色,该颜色仅在有间隙且不是实心的图案时可见.

So if setting setFillBackgroundColor, then you are setting the color behind the pattern which will only be visible if the pattern has gaps and is not solid.

通常使用SOLID_FOREGROUND模式填充一个单元格.因此,需要图案的颜色,而不是图案后面的颜色.

Normally a cell is filled using SOLID_FOREGROUND pattern. So the color of the pattern is needed and not the color behind the pattern.

尝试

style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

这篇关于如何使用Java代码使用HSSF应用单元格背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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