tags:

views:

106

answers:

1

I am using jxl api for generating a excel sheet, in which the alternate rows are highlighted, when I sort the contents of a excel and sort manually, the cell background colors messup, usually this is because I am writing the color cell by cell, is there anyway through which i color the alternate rows of the excel while generating it, in such a way that it doesnt effect the sorting of the contents.

A: 

Have you tried using the RowView:

Sheet s = ...
Colour[] colorings = new Colour[]{Colour.GOLD, Colour.OCEAN_BLUE};
for(int i=0;i<s.getRows();i++){
  CellView rowView = s.getRowView(i);
  WritableCellFormat newFormat = new WritableCellFormat(rowView.getFormat());
  newFormat.setBackground(colorings[i%2]);
  rowView.setFormat(newFormat());
}

I think this should accomplish the effect you are looking for. Note I have copied the existing format above, but if you don't already have formats applied to the rows, you could create two formats and reuse them. The only other caveat is I believe any format applied to a specific cell will override the view format, so you may need to make sure the individual cells are formatted to have color DEFAULT or AUTOMATIC (I'm not entirely sure about this, as I haven't tried it myself).

M. Jessup
Thanks for replying Jessup, I guess the question posted is rather giving different meaning, the solution to my problem can be solved manually by using conditional formatting in excel, the jxl generated excel sheeet which has a header row with unique formatting, remaining rows highlighted alternatively, problem arises when I sort the contents of this excel by selecting the rows->data menu-> sort in excel, the data gets sorted, and the backgroound colors to get sorted, in Manual solution using conditional formatting like "=mod(row(),2)=1", in this case only the contents are sorted, any idea?
afzal
my requirement is the background color should not be sorted instead just the contents get sorted.. Hope i m not confusing you..
afzal
Understood, as far as I know conditional formats are not supported in the current version of the library.
M. Jessup