You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by suriz4u <su...@gmail.com> on 2008/06/09 13:44:09 UTC
Needs help regarding coloring alternate rows.............
In my application there is requirement that alternative rows should be
colored.As iam using different styles for different
objects(Ex:Long,Integer,Float etc..).I tried to use in following way for
different objects
HSSFCellStyle csDouble= wb.createCellStyle();
if(!(rowNumber%2==0))
{ csDouble.setFillForegroundColor(HSSFColor.AQUA.index);
csDouble.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);}
But to my surprise i got all the rows with same color.Iam really confused.
Can any one help me out please..............
--
View this message in context: http://www.nabble.com/Needs-help-regarding-coloring-alternate-rows.............-tp17731174p17731174.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Needs help regarding coloring alternate rows.............
Posted by suriz4u <su...@gmail.com>.
Thanks Steve.But iam using different styles for different objects.For example
for date iam using
HSSFCellStyle csDate = wb.createCellStyle();
HSSFCellStyle csDouble = wb.createCellStyle();
I have different styles for different datatypes such as double etc..
So based on the row count i can set the color,but if iam using following
code.
Iam looping through row and for each cell iam checking the datatype.
if(value instanceof Double)
{
if(!(rownum%2==0))
{
csDouble.setFillForegroundColor(HSSFColor.AQUA.index);
csDouble.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
}
}
So iam using many styles in a row,i mean for different cells i have
different styles.So if iam using above code and setting color.That color is
being applied through out the sheet.
So any solution please??
Steve Klimchuk wrote:
>
> Try this...
>
> HSSFCellStyle altRow = workbook.createCellStyle();
> altRow.setFillForegroundColor(HSSFColor.AQUA.index);
> altRow.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
>
> if((rownum % 2) == 1) {
> cell.setCellStyle(altRow);
> }
>
>
>
>
>
>
>
> suriz4u wrote:
>> In my application there is requirement that alternative rows should be
>> colored.As iam using different styles for different
>> objects(Ex:Long,Integer,Float etc..).I tried to use in following way for
>> different objects
>>
>> HSSFCellStyle csDouble= wb.createCellStyle();
>> if(!(rowNumber%2==0))
>> { csDouble.setFillForegroundColor(HSSFColor.AQUA.index);
>> csDouble.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);}
>>
>>
>> But to my surprise i got all the rows with same color.Iam really
> confused.
>> Can any one help me out please..............
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>
>
--
View this message in context: http://www.nabble.com/Needs-help-regarding-coloring-alternate-rows.............-tp17731174p17747577.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Needs help regarding coloring alternate rows.............
Posted by swati <sw...@gmail.com>.
Just in case someone chances upon this post, the solution is as follows:
if(rowIndex % 2 == 0) {
color = IndexedColors.AQUA.index;
}else {
color = IndexedColors.WHITE.index;
}
colorCell(cell, color);
and then in colorCell,
private void colorCell(Cell cell, short color) {
CellStyle cellStyle = cell.getCellStyle();
CellStyle newStyle = wb.createCellStyle();
newStyle.cloneStyleFrom(cellStyle);
newStyle.setFillForegroundColor((short) color);
newStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(newStyle);
}
make sure you create a new style by cloning whatever style you had earlier.
Do the alternate coloring of rows in the end after you have completed other
styling
--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Needs-help-regarding-coloring-alternate-rows-tp2296869p5710294.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Needs help regarding coloring alternate rows.............
Posted by Steve Klimchuk <kl...@ca.ibm.com>.
Try this...
HSSFCellStyle altRow = workbook.createCellStyle();
altRow.setFillForegroundColor(HSSFColor.AQUA.index);
altRow.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
if((rownum % 2) == 1) {
cell.setCellStyle(altRow);
}
suriz4u wrote:
> In my application there is requirement that alternative rows should be
> colored.As iam using different styles for different
> objects(Ex:Long,Integer,Float etc..).I tried to use in following way for
> different objects
>
> HSSFCellStyle csDouble= wb.createCellStyle();
> if(!(rowNumber%2==0))
> { csDouble.setFillForegroundColor(HSSFColor.AQUA.index);
> csDouble.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);}
>
>
> But to my surprise i got all the rows with same color.Iam really
confused.
> Can any one help me out please..............
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org
Re: Needs help regarding coloring alternate rows.............
Posted by Andy Chien <hs...@utoronto.ca>.
Just for further clarification to the problem... how are you getting
rowNumber, second by all the rows with same color what do you mean do
you mean they are all aqua or they are all white?
I am asking because there might be possibilities that code is not going
into the if block. I think this would help people better understand
your code excerpt.
Cheers,
Andy Chien
suriz4u wrote:
> In my application there is requirement that alternative rows should be
> colored.As iam using different styles for different
> objects(Ex:Long,Integer,Float etc..).I tried to use in following way for
> different objects
>
> HSSFCellStyle csDouble= wb.createCellStyle();
> if(!(rowNumber%2==0))
> { csDouble.setFillForegroundColor(HSSFColor.AQUA.index);
> csDouble.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);}
>
>
> But to my surprise i got all the rows with same color.Iam really confused.
> Can any one help me out please..............
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org