Page 1 of 1

How to set Text color in a cell?

PostPosted: Wed Feb 04, 2015 8:52 pm
by netdieter
Has anyone an example for me?

Regads Dieter

Re: How to set Text color in a cell?

PostPosted: Wed Feb 04, 2015 9:45 pm
by jason
If you upload an example created in Excel to the webapp (linked above), you'll see something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
            <si>
                <r>
                    <t xml:space="preserve">the </t>
                </r>
                <r>
                    <rPr>
                        <sz val="11.0"/>
                        <color rgb="FFFF0000"/>
                        <rFont val="Calibri"/>
                        <family val="2"/>
                        <scheme val="minor"/>
                    </rPr>
                    <t>cat</t>
                </r>
                <r>
                    <rPr>
                        <sz val="11.0"/>
                        <color theme="1"/>
                        <rFont val="Calibri"/>
                        <family val="2"/>
                        <scheme val="minor"/>
                    </rPr>
                    <t xml:space="preserve"> sat</t>
                </r>
            </si>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


in the shared strings table.

But as per the CreateSimpleSpreadsheet sample:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        /**
                // Note: if you are trying to add characters, not a number,
                // the easiest approach is to use inline strings (as opposed to the shared string table).
                // See http://openxmldeveloper.org/blog/b/open ... heets.aspx
                // and xlsx-java-f15/cells-with-character-values-t874.html
         */

        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;
               
        }
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


See further http://webapp.docx4java.org/OnlineDemo/ ... ML/is.html

Use t for text (as in the code above), or r for rich text.

A CTRElt in CTRst can have run properties, just like in the shared string table.

Untested code, generated by the webapp, then edited:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
import java.lang.Double;
import javax.xml.bind.JAXBElement;
import org.xlsx4j.sml.CTColor;
import org.xlsx4j.sml.CTRElt;
import org.xlsx4j.sml.CTRPrElt;
import org.xlsx4j.sml.CTXstringWhitespace;


public class Foo {
public CTRElt createIt() {

org.xlsx4j.sml.ObjectFactory smlObjectFactory = new org.xlsx4j.sml.ObjectFactory();

CTRElt relt = smlObjectFactory.createCTRElt();
    // Create object for rPr
    CTRPrElt rprelt = smlObjectFactory.createCTRPrElt();
    relt.setRPr(rprelt);
        // Create object for color (wrapped in JAXBElement)
        CTColor color = smlObjectFactory.createCTColor();
        JAXBElement<org.xlsx4j.sml.CTColor> colorWrapped = smlObjectFactory.createCTRPrEltColor(color);
        rprelt.getRFontOrCharsetOrFamily().add( colorWrapped);
            color.setRgb(value); // byte[] value

    // Create object for t
    CTXstringWhitespace xstringwhitespace = smlObjectFactory.createCTXstringWhitespace();
    relt.setT(xstringwhitespace);
        xstringwhitespace.setValue( "cat");

return relt;
}
}
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


or

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting

String openXML = "<r>
                  + "
<rPr>
                        + "<color rgb=\"FFFF0000\"/>"

                  +"</rPr>"

                  + "<t>cat</t>"

            +"</r>";
CTRElt relt = (CTRElt)XmlUtils.unmarshalString(openXML, Context.jcSML);
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

Re: How to set Text color in a cell?

PostPosted: Thu Feb 05, 2015 1:13 am
by netdieter
Sorry, but what shall i do with the CTRElt Object?

Re: How to set Text color in a cell?

PostPosted: Thu Feb 05, 2015 2:29 am
by netdieter
Found the solution.
ctrst.getR().add(createIt());