Hi,
The easiest thing to do to apply styles, is to first define them in
OpenOffice (via the F11 shortcut). Then you just call setStyleName()
with the name you gave it. The vertical split is not standard but I've
included in the attachement how OO does it.
HTH,
Sylvain.
import java.io.File;
import java.io.IOException;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;
import org.jopendocument.dom.spreadsheet.Sheet;
import org.jopendocument.dom.spreadsheet.SpreadSheet;
public class Example {
public static void main(String[] args) throws IOException, JDOMException {
final SpreadSheet spread = SpreadSheet.createFromFile(new File(args[0]));
final Sheet sheet = spread.getSheet(0);
// set a style defined in OpenOffice
sheet.getCellAt("A1").setStyleName("myBoldStyle");
// the vertical split is not part of OpenDocument, but this how OpenOffice uses it
// see the whole settings.xml for more
final XPath xp = spread
.getXPath("./office:settings/config:config-item-set[@config:name='ooo:view-settings']/config:config-item-map-indexed[@config:name='Views']//config:config-item-map-entry[@config:name=$sheetName]");
xp.setVariable("sheetName", sheet.getElement().getAttributeValue("name", spread.getNS().getTABLE()));
final Element sheetSettings = (Element) xp.selectSingleNode(spread.getPackage().getDocument("settings.xml").getRootElement());
final Element splitMode = (Element) spread.getXPath("./config:config-item[@config:name='VerticalSplitMode']").selectSingleNode(sheetSettings);
// 0 to disable
splitMode.setText("1");
final Element splitPos = (Element) spread.getXPath("./config:config-item[@config:name='VerticalSplitPosition']").selectSingleNode(sheetSettings);
// height in pixels
splitPos.setText("64");
spread.saveAs(new File("out"));
}
}
Thank you for the detailed description of your needs, to sum up, you
preffer a 100% java approach
because filling a template seems to be like using dynamite to kill flies.
I agree with you that "sheet.getRow(1).getCurrentStyle().setBold(true);"
is a clean solution too,
easy style handling and creation is something planned for the new releases.
Playing with styles is not as simple as you could think because styles
are shared, hierachical, dependent of installed fonts etc...
that's why we always use OpenOffice for such tasks.
The main idea of the API is something like MVC, we separate content and
form.
Content being handled and maintained in java ; form being manipulated
with OpenOffice.
We provide methods to modify the strucuture of the sheet
(add/insert/... rows and columns).
Look at org.jopendocument.dom.spreadsheet.Table , the method "merge"
uses a TableModel
which is what you are looking for easily inject your data.
http://www.jopendocument.org/start_spreadsheet_2.html seems, for us,
easier than doing all the dark stuffs in java
and far more performant. Will you be happy to modify and deploy a new
version of your application if users preffer red titles instead of bold?
Regards,
Guillaume