Page 1 of 1

Is there a simple example of formatting a cell?

PostPosted: Wed Feb 12, 2014 5:02 pm
by brad_c
I've searched for xlsx4j examples for this, but can't find anything other than this post, which is not a simple example
http://www.docx4java.org/forums/xlsx-java-f15/adding-style-information-crashes-excel-t647.html

I see how the styles are created and added to the workbook, but I can't figure out how you set a style on a cell. I've used the online code generator with a simple spreadsheet I created, but since it doesn't output the code for the entire xlsx file at once, I can't see how styles are associated with a cell.

Any simple example would be greatly appreciated. The version of xlsx4j is irrelevant, as I can use any version in my application.


Thanks.

Re: Is there a simple example of formatting a cell?

PostPosted: Wed Feb 12, 2014 6:38 pm
by jason
The key to it is that the cell c has an attribute s which gives the style index.

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

So for example, @s="1" references the second entry in:

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


and that entry in turn references other bits of the stylesheet (/xl/styles.xml)

So in the webapp, its really the stylesheet you probably want to be looking at.

Reading this as equivalent to CellFormat in the Open XML SDK, the following post might help you: http://stackoverflow.com/questions/1111 ... eadsheetml

It would be good if xlsx4j had a high level API which made cell styling easier. It wouldn't be hard to add; contributions as always are welcome.

Re: Is there a simple example of formatting a cell?

PostPosted: Thu Feb 13, 2014 1:52 am
by brad_c
Thanks. I didn't realize that S is the style index. Maybe I've missed it on your site, but do you have a prominent reference to any documentation for using xlsx4j?

That javadoc in the code does not help at all for figuring out attributes, but I imagine that is due to code generation tools used to generate the source from a schema.

I found the ECMA reference this morning by following the links on the page for the online demo, but I would expect this to be featured somewhere on your site.
http://webapp.docx4java.org/OnlineDemo/ ... index.html

Once I get my application working, I will contribute a sample that show how to do basic styling that can be inlcuded in the org.xlsx4j.samples package.