Documents > Cookbook >Table

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);

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);

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);

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);

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) {

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


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

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

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

           boolean isProtected=table1.isProtected();

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");

Powered by the Apache CMS.

Apache "ODF Toolkit" is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

Copyright © 2011 The Apache Software Foundation Licensed under the Apache License, Version 2.0. Contact Us
Apache and the Apache feather logos are trademarks of The Apache Software Foundation.
Other names appearing on the site may be trademarks of their respective owners.