Page 1 of 1

How to read and modify existing Stylesheed

PostPosted: Fri Feb 20, 2015 12:16 am
by netdieter
Hello,
i use a excel file as a template for a dataexport. Is there a way to read tge ecisting Stylesheed an modify it bevor i write the file back?

Thanx

Re: How to read and modify existing Stylesheed

PostPosted: Fri Feb 20, 2015 11:19 pm
by jason
You'd expect the WorkbookPart to give you a reference to it, but it doesn't. I'll fix that.

So you can either:

1. get it by its expected name

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
org.docx4j.openpackaging.parts.SpreadsheetML.Styles stylesPart = (Styles)pkg.getParts().get(new PartName("xl/styles.xml"))
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


or 2. by looking for a rel of type Namespaces.SPREADSHEETML_STYLES (in all parts, or in the rels of Workbook part)

Re: How to read and modify existing Stylesheed

PostPosted: Sat Feb 21, 2015 1:50 am
by netdieter
Thanx for this hint.
I post my progress here. May be usefull for others:

Code: Select all
Styles styles = (Styles)pkg.getParts().get(new PartName("/xl/styles.xml"));
CTStylesheet st =  styles.getContents();

CTXf xf = Context.getsmlObjectFactory().createCTXf();
xf.setNumFmtId( new Long(0) );
xf.setFontId( new Long(1) );
xf.setFillId( new Long(0) );
xf.setBorderId( new Long(1) );
xf.setXfId( new Long(0) );
st.getCellXfs().getXf().add(xf);
st.getCellXfs().setCount(st.getCellXfs().getCount() + 1L);

Re: How to read and modify existing Stylesheed

PostPosted: Sun Feb 22, 2015 7:29 am
by jason
Following https://github.com/plutext/docx4j/commi ... 708bec249f
you'll be able to do:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
Styles stylesPart = pkg.getWorkbookPart().getStylesPart();
 
Parsed in 0.013 seconds, using GeSHi 1.0.8.4


The result will be null if there is no styles part in the pkg, so check for that...