Page 1 of 1

Coloring rows without using a theme

PostPosted: Mon Jun 18, 2012 10:54 pm
by lvdpal
Today I tried to add a background fill to my rows. In order to keep my code simple, I wanted to do this without using theme colors. So I tried the setRgb method on CTColor when creating a fill for my stylesheet. But for some reason, it ignored this color. No matter what color I used, it came out as a shade of grey.

Here's the code I tried:
Code: Select all
CTFill blueFill = new CTFill();
CTPatternFill bluePatternfill = new CTPatternFill();
bluePatternfill.setPatternType(STPatternType.SOLID);
CTColor blue = new CTColor();
blue.setTint(0.6);
byte[] rgbBlue = "4BACC6".getBytes();
blue.setRgb(rgbBlue);
bluePatternfill.setFgColor(blue);
bluePatternfill.setBgColor(bgColor);
blueFill.setPatternFill(bluePatternfill);
styleSheet.getFills().getFill().add(blueFill);


All the code surrounding it should be in order, because as soon as I add the theme and replace setRgb with setTheme, it works perfectly. Any idea why this doesn't work?

Re: Coloring rows without using a theme

PostPosted: Fri Jun 22, 2012 6:28 pm
by jason
When I set a row colour to RGB in Excel, I get an entry like the following in /xl/styles/xml:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <fill>
      <patternFill patternType="solid">
        <fgColor rgb="FFE83E39"/>
        <bgColor indexed="64"/>
      </patternFill>
    </fill>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


and corresponding

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <cellXfs count="3">
    :
    <xf numFmtId="0" fontId="0" fillId="3" borderId="0" xfId="0" applyFill="1"/>
  </cellXfs>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


related by fillId. This is connected to the row by the @s on the row. Do you have this?

Also check when you marshall your blueFill to a string, what do you get?

Re: Coloring rows without using a theme

PostPosted: Thu Nov 01, 2012 3:28 am
by arimmer
I had a similar problem and found that for some reason MS Excel always displayed the first 'fill' as grey whatever actual values were set - now I just create an 'unused' fill and use second and subsequent fills with no problems.

If I remember correctly, if you look at the fills created and used (in the xml files within the xslx) when you use fills in MS Excel you'll see that the fill used is not the first one - which is how I came up with my work-around of the unused fill.