Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any alternate way to avoid deprecation in Apache POI, for HSSF built-in colors?

In my code i want to change the cell colors of a particular column of a HSSFWorkbook, if the text is "PASS". But when i was writing the code, lots of methods and constants like BRIGHT_GREEN.index, setFillPattern, SOLID_FOREGROUND are deprecated. I have searched for an alternative in Apache POI official website, but the code given there is also deprecated. I know there is no problem if i mention @deprecation tag, but sometimes after 100-150 lines(rows), cell-color is not changing. Can anyone please tell me is there any alternative to avoid @deprecation? FYI: i am using poi-bin-3.17-beta1-20170701 jars. Thanks in advance :)

if(cell.getStringCellValue().equalsIgnoreCase("Pass")){
                    HSSFCellStyle style = workbook.createCellStyle();
                    style.setFillForegroundColor(HSSFColor.BRIGHT_GREEN.index);
                    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                    cell.setCellStyle(style);
                }
like image 999
BongCule Avatar asked Nov 02 '17 14:11

BongCule


1 Answers

From their documentation:

/**
 * @deprecated use {@link HSSFColorPredefined} instead
 */
@Deprecated
@Removal(version="3.18")
public static class BRIGHT_GREEN extends HSSFColorRef {
    private static final HSSFColorPredefined ref = HSSFColorPredefined.BRIGHT_GREEN;
    public static final short index = ref.getIndex();
    public static final int index2 = ref.getIndex2();
    public static final short[] triplet = ref.getTriplet();
    public static final String hexString = ref.getHexString();
    public BRIGHT_GREEN() { super(ref); }
}

So in your case: HSSFColor.HSSFColorPredefined.BRIGHT_GREEN

For your setFillPattern:

    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Your new code should look like this

    style = workbook.createCellStyle();
    style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.BRIGHT_GREEN.getIndex())
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
like image 153
Andrei Sfat Avatar answered Sep 28 '22 02:09

Andrei Sfat