java - Dynamically change cell color in Excel -
i'm using poi library deal excel file, want change forgroundcolor of particular cells. i'm not satisfied list of indexedcolors want create own substituting 1 existing (in case hssfcolor.blue), problem - saves color last iteration (all cells have same color).
the code (convdata - 2 dim double array, normalized 255):
hssfpalette hssfpalette = excelfile.getcustompalette(); cellstyle cellstyle = excelfile.createcellstyle(); hssfpalette = excelfile.getcustompalette(); cellstyle.setfillpattern(cellstyle.solid_foreground); (int i=0; i<convdata.length; i++) { row row = excelsheet.createrow(i); (int j=0; j<convdata[i].length; j++) { cell cell = row.createcell(j); hssfpalette.setcoloratindex(hssfcolor.blue.index, convdata[i][j].bytevalue(), convdata[i][j].bytevalue(), convdata[i][j].bytevalue()); cellstyle.setfillforegroundcolor(hssfpalette.getcolor(hssfcolor.blue.index).getindex()); cell.setcellstyle(cellstyle); } }
your problem you're creating 1 single cell style, assigning bunch of cells, changing blue part way through. cell style global, blue applies everything
instead, either need move "redefine blue is" outside of loop, or create new cell style + apply colour each differently coloured cell. however, there's limit number of colours , cell styles can have, make sure re-use them if have multiple cells wanting same colour
Comments
Post a Comment