1

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.

Background:

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

Foreground:

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.

FYI: I'm Working with Excel version .XLS

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

Thanks

2 Answers 2

3

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.

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.

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

Try

style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Sign up to request clarification or add additional context in comments.

4 Comments

Getting error message while executing the above lines thru Groovy script.. groovy.lang.MissingMethodException: No signature of method: org.apache.poi.hssf.usermodel.HSSFCellStyle.setFillPattern() is applicable for argument types: (org.apache.poi.ss.usermodel.FillPatternType) values: [SOLID_FOREGROUND] Possible solutions: setFillPattern(short), getFillPattern() error at line: 146
Answer POSTED for the above issue
@Karunagara: My code is working using the latest stable version of apache poi. Using this is really recommended. You should mentione in the question that you are using an ancient version of apache poi and which. So such misunderstandigs could be avoided.
Thank you for the reply. I have already accepted your answer.
0

It's working for the below lines:

style.setFillForegroundColor(IndexedColors.GREEN.getIndex())
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.