Roudy Tarabay
Roudy Tarabay

Reputation: 29

POI,color row not working

 shouldn't this be working? fac is the uncolored workbook and facsheet is its uncolored sheet

sheet is the colored sheet,i'm trying to get the color from the workbook of one and set them into the other one

for(int i=2;rowIterator.hasNext();i++){
                      CellStyle style=fac.createCellStyle();
                      style.setFillForegroundColor(sheet.getRow(i).getRowStyle().getFillForegroundColor());
                      style.setFillBackgroundColor(sheet.getRow(i).getRowStyle().getFillBackgroundColor());
               facsheet.getRow(i).setRowStyle(style);

                  }

Upvotes: 0

Views: 1804

Answers (1)

rgettman
rgettman

Reputation: 178243

How was the Excel spreadsheet where sheet comes from created? Did you create it in Excel or POI? If POI, did you use setRowStyle?

Usually, individual cells are styled, not entire rows, so getRowStyle() will return null.

Quoting the Javadocs from Row#getRowStyle():

Returns the whole-row cell styles. Most rows won't have one of these, so will return null. Call isFormatted() to check first.

You will need to loop over all individual cells in each row, copying the CellStyle for each Cell. But be careful, there is a limit to the number of CellStyles that can be created for a spreadsheet. So if you already have created an identical CellStyle, then reuse it.

Upvotes: 1

Related Questions