Page 1 of 1

Background color for cell

PostPosted: Thu Aug 22, 2019 4:27 am
by borja
Hello,

Is there an easy way to apply a fill color to a cell? Im creating the cells with this code:

Code: Select all
private static Cell createCell(String content) {

        Cell cell = Context.getsmlObjectFactory().createCell();
       

        CTXstringWhitespace ctx = Context.getsmlObjectFactory().createCTXstringWhitespace();
        ctx.setValue(content);

        CTRst ctrst = new CTRst();
        ctrst.setT(ctx);

        cell.setT(STCellType.INLINE_STR);
        cell.setIs(ctrst); // add ctrst as inline string

        return cell;
      
    }


That i copied from some example here. Is there a way i can modify that to apply a fill color to the cell. I dont want an entire row to be colored, only one cell.

Thank you very much!

Re: Background color for cell

PostPosted: Tue Aug 27, 2019 5:10 pm
by jason
In your worksheet, your cell needs a style attribute (@s):

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<c r="B2" s="1"/>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Per http://webapp.docx4java.org/OnlineDemo/ ... L/c_1.html

s (Style Index): The index of this cell's style. Style records are stored in the Styles Part.


In the Styles part, in this example, you then have:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <fills count="3">
    <fill>
      <patternFill patternType="none"/>
    </fill>
    <fill>
      <patternFill patternType="gray125"/>
    </fill>
    <fill>
      <patternFill patternType="solid">
        <fgColor theme="5"/>
        <bgColor indexed="64"/>
      </patternFill>
    </fill>
  </fills>
  <cellXfs count="2">
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
    <xf numFmtId="0" fontId="0" fillId="2" borderId="0" xfId="0" applyFill="1"/>
  </cellXfs>
 
Parsed in 0.005 seconds, using GeSHi 1.0.8.4


The index into cellXfs is zero-based, so here we are using fillId 2, which is the last of the fills.

To see whether your SpreadsheetMLPackage contains a org.docx4j.openpackaging.parts.SpreadsheetML.Styles part, invoke workbookPart.getStylesPart()

If your SpreadsheetMLPackage doesn't contain a Styles part, you'll need to instantiate one, then add it using workbookPart.addTargetPart(stylesPart). You'll need to create a CTStylesheet then call stylesPart.setContent(yourCTStylesheetObj)

Re: Background color for cell

PostPosted: Mon Sep 30, 2019 4:51 pm
by DneprM750
Great, I had trouble implementing background colors as well, but I did that and it worked fine. I was really at a loss there...