General

Components

Community

Development

TDF

Documents > Cookbook >Table



Overview
This Table API supports to manipulate tables in text and spreadsheet documents. It covers the table definition in ODF Specification 1.2 Committee Draft05

Create Table
Let's create an empty table first.By default,the code below create a table with 5 columns and 2 rows.

            TextDocument document = TextDocument.newTextDocument();
Table table1 = Table.newTable(document);
table1.setTableName("table1");
document.save(filePath);

If you want to create table with specified column and row,you can do like this:

            int row=4;
int column=3;
Table table2=Table.newTable(document, row, column);
table2.setTableName("table2");

If you want to put some numbers into a table while creating it, you can use the constructor Table.newTable(document,rowlabels,columnlabels, data) which you should specify a 2 dimension array as the data and 2 String arrays as table labels,one for row and the other for column.

        int rowcount = 10, columncount = 4;
double[][] data = new double[rowcount][columncount];
String[] rowlabels = new String[rowcount];
String[] columnlabels = new String[columncount];
Table table3=Table.newTable(document,rowlabels,columnlabels, data);
table3.setTableName("dataTable");

You can also fill table with string values while creating it, to do this you should provide a 2 dimension string array instead of double array.

     	String[][] stringData = new String[rowcount][columncount];
Table table4 = Table.newTable(document, rowlabels, columnlabels, stringData);
table4.setTableName("stringTable");


Find Table
To get all the tables in the document,you can do like this:

        List<Table> tableList=document.getTableList();

If you want to get a single table,you can use the table name to find it.If it's not found,the method returns null.

        Table emptyTable=document.getTableByName("table1");


Delete Table
        Table table = document.getTableByName("DeletedTable");
if (table != null) {
table.remove();
}


Set Table
You can set or update table name,which can be regarded as table identifier in a document.

           table1.getTableName();
table1.setTableName("EnglishScore");

If you want to change table width,you can do like this:

        Table tableWidth=document.getTableByName("table1");
if(tableWidth!=null) {
long width=500;
tableWidth.setWidth(width);
tableWidth.getWidth();
}

Each table in the document has a protect attribute to show whether it is protected or not.

           boolean isProtected=table1.isProtected();
table1.setProtected(true);


Format Table
Since version 0.8, new APIs are added to format a table, which will load the formatting from a template table in a foreign document and apply them to corresponding cells of current table. The template table should be a 5*5 table with predefined formatting, includes number format, text format, text alignment, borders and background. The following code will load formatting from a foreign template table - Table1 in TemplateTable.odt, and apply the styles to a new table - Table2 in TargetTable.odt.

          Document doc = TextDocument.loadDocument("TargetTable.odt");
TableTemplate template = doc.LoadTableTemplateFromForeignTable(new FileInputStream("TableTemplate.odt"), "Table1");
Table table = doc.getTableByName("Table2");
table.applyStyle(template);


Impressum (Legal Info) | Privacy Policy (Datenschutzerklärung) | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Apache License, v2.0. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.