0

I am working on generating a excel in my app for reporting purposes. I can apply styles to all rows but when I try and target specific rows (as shown below), the style does not work.

 public void postProcessXLS(Object document) {

    HSSFWorkbook wb = (HSSFWorkbook) document;
    HSSFSheet sheet = wb.getSheetAt(0);
    CellStyle cs = wb.createCellStyle();

    cs.setFillBackgroundColor(IndexedColors.RED1.index);
    cs.setFillPattern(FillPatternType.SOLID_FOREGROUND);    
    cs.setFillForegroundColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());

    sheet.getRow(0).setRowStyle(cs);
}

Trying to format the data in my table so the users have an easier time reading / handling it. I want to display different styles for alternate rows. If there is an easier way to do this in HSSF without having to manually write the styles, I am open to suggestions.

mrJay
  • 27
  • 10
  • I think you first have to create a cell prior applying styles – Nico Van Belle Dec 18 '18 at 08:27
  • In https://stackoverflow.com/questions/49270297/how-to-get-cell-style-of-empty-cell-apache-poi/49282369#49282369 I have provided a method `getPreferredCellStyle` which should be used when a cell was new created. This considers row styles. – Axel Richter Dec 18 '18 at 08:31
  • 1
    Possible duplicate of [How to apply bold text style for an entire row using Apache POI?](https://stackoverflow.com/questions/12286662/how-to-apply-bold-text-style-for-an-entire-row-using-apache-poi) – Dezso Gabos Dec 18 '18 at 08:31
  • @mrJay: You should mention in your question that the goal is to differ even and odd rows, as shown in the accepted answer. For reaching this goal, conditional formatting will really be the best approach. But this is not clear from your question up to now. So further readers will be confused abourt this accepted answer. – Axel Richter Dec 18 '18 at 08:59

1 Answers1

1

If you want to apply styles based on some condition or pattern , then go for conditional formatting , i faced the same challenges during my tussle with Apache POI and this post helped http://www.javavillage.in/fills-and-colors-using-conditional-formate-using-apache-poi.php , hope this helps you too

Yugansh
  • 365
  • 3
  • 9