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