Skip to content
Julien Férard edited this page Mar 9, 2018 · 4 revisions

Q1. How to create a minimal spreadsheet document?

final OdsFactory factory = OdsFactory.create();
final AnonymousOdsFileWriter writer = factory.createWriter();
final OdsDocument document = writer.document();
writer.saveAs(new File(...));

This will create an empty document (without any sheet). LibreOffice will create a blank sheet on opening.

Q2. How to add a table, a row, a cell?

Option 1: without indices

final Table table = document.addTable("table1");
final TableRow row = table.nextRow(); // could be table.getRow(10) or table.getRow("A10");
TableCellWalker cell = row.getWalker();
// do something with cell A1
cell.next()
// do something with cell A2

Option 2: with indices

final Table table = document.addTable("table1");
final TableRow row = table.getRow(0)
TableCell cell = row.getOrCreateCell(0);
// do something with cell A1

Q3. What if I know in advance the size of the table?

You can tell it to FastODS: this will fasten the writing.

final Table table = document.addTable("table1", rowCapacity, columnCapacity);

Q4. What is the difference between a NamedOdsFileWriter and an AnonymousOdsFileWriter?

When you pass a name to OdsFactory.createWriter, you get a NamedOdsFileWriter, when you don't you get an AnonymousOdsFileWriter. They are very different:

  • with an anonymous writer, all the document will be stored in memory before writing. This is easier and faster.
  • with a named writer, the data is written by bunches. This is slower and you have to register styles before you start creating the tables. This may be the only solution when you have a very low memory.

Q5. How do I register the styles?

You have a NamedOdsDocument from a NamedOdsFileWriter. You will use the various NamedOdsDocument.addXXXStyle to register styles, then you will use NamedOdsDocument.freezeStyles. If you try to use a style that was not registred, you will get an IllegalStateException. With NamedOdsDocument.debugStyles, the exception is replaced by a log message of level severe.

Q6. How do I use global custom data styles?

The data styles are what LO calls "formats". First, you have to create the data styles:

final DataStylesBuilder dsb = DataStylesBuilder.create(Locale.XXX);
// now, use the builders. See the API for data styles buiders
dsb.XXXStyleBuilder().field1(value1).field2(value2). ...
final DataStyles ds = dsb.build();

Then, you will pass the data styles to the factory:

final AnonymousOdsFileWriter writer = OdsFactory.create().dataStyles(ds).createWriter();

Q7. How do I use a custom data style for one cell?

You just have to build the data style, and set it on the cell.

final DataStyle intStyle = new FloatStyleBuilder("custom-int-datastyle", this.locale).decimalPlaces(8)
        .groupThousands(true).build();
cell.setDataStyle(intStyle);

Q8. Styles: what is the difference between build and buildHidden ?

This question is outdated: buildHidden() was replaced by hidden().build() if the style is not hidden by default, build() else.

Q9. Why is my style not visible when I hit F11 under LO?

FastODS create some styles as "visible styles" and some others as "hidden styles" by default:

  • data styles are hidden by default, but can be visible (visible() method of the builder)
  • cell, row, column and table styles are visible by default, but can be hidden (hidden() method of the builder)
  • page styles are visible and cannot be hidden.

## Q10. How do I create a link?

Link to a table:

String targetTableName;
cell.setText(
Text.builder().par().span("<before link>").link("link text", targetTableName).span("<after link>")
        .build());

Link to an URL:

URL url;
cell.setText(
Text.builder().par().span("<before link>").link("link text", url).span("<after link>")
        .build());

Link to a file:

File file;
cell.setText(
Text.builder().par().span("<before link>").link("link text", file).span("<after link>")
        .build());

Q11. How do I set the style of a cell?

First, create the style:

TableCellStyle style = TableCellStyle.builder("my-style").backgroundColor(SimpleColor.GRAY).fontWeightBold().build(); // hidden().build() for automatic styles

For a cell:

cell.setStyle(style);

For a row:

row.setDefaultCellStyle(style);

For a column:

final TableColumnStyle style = TableColumnStyle.builder("col-style").defaultCellStyle(cellStyle).build();
table.setColumnStyle(0, style);