public class CellCollection extends NodeCollection
Cell nodes.
To learn more, visit the Working with Tables documentation article.
Examples:
Shows how to iterate through all tables in the document and print the contents of each cell.
Document doc = new Document(getMyDir() + "Tables.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
Assert.assertEquals(2, tables.toArray().length);
for (int i = 0; i < tables.getCount(); i++) {
System.out.println(MessageFormat.format("Start of Table {0}", i));
RowCollection rows = tables.get(i).getRows();
for (int j = 0; j < rows.getCount(); j++) {
System.out.println(MessageFormat.format("\tStart of Row {0}", j));
CellCollection cells = rows.get(j).getCells();
for (int k = 0; k < cells.getCount(); k++) {
String cellText = cells.get(k).toString(SaveFormat.TEXT).trim();
System.out.println(MessageFormat.format("\t\tContents of Cell:{0} = \"{1}\"", k, cellText));
}
System.out.println(MessageFormat.format("\tEnd of Row {0}", j));
}
System.out.println(MessageFormat.format("End of Table {0}\n", i));
}
| Modifier and Type | Method and Description |
|---|---|
Node |
get(int index)
Retrieves a
Cell at the given index. |
Node[] |
toArray()
Copies all cells from the collection to a new array of cells.
|
add, clear, contains, getContainer, getCount, getCurrentNode, getNextMatchingNode, indexOf, insert, iterator, remove, removeAtpublic Node get(int index)
Cell at the given index.
Remarks:
The index is zero-based.
Negative indexes are allowed and indicate access from the back of the collection. For example -1 means the last item, -2 means the second before last and so on.
If index is greater than or equal to the number of items in the list, this returns a null reference.
If index is negative and its absolute value is greater than the number of items in the list, this returns a null reference.
Examples:
Shows how to iterate through all tables in the document and print the contents of each cell.
Document doc = new Document(getMyDir() + "Tables.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
Assert.assertEquals(2, tables.toArray().length);
for (int i = 0; i < tables.getCount(); i++) {
System.out.println(MessageFormat.format("Start of Table {0}", i));
RowCollection rows = tables.get(i).getRows();
for (int j = 0; j < rows.getCount(); j++) {
System.out.println(MessageFormat.format("\tStart of Row {0}", j));
CellCollection cells = rows.get(j).getCells();
for (int k = 0; k < cells.getCount(); k++) {
String cellText = cells.get(k).toString(SaveFormat.TEXT).trim();
System.out.println(MessageFormat.format("\t\tContents of Cell:{0} = \"{1}\"", k, cellText));
}
System.out.println(MessageFormat.format("\tEnd of Row {0}", j));
}
System.out.println(MessageFormat.format("End of Table {0}\n", i));
}
get in class NodeCollectionindex - An index into the collection.Cell value.public Node[] toArray()
Examples:
Shows how to iterate through all tables in the document and print the contents of each cell.
Document doc = new Document(getMyDir() + "Tables.docx");
TableCollection tables = doc.getFirstSection().getBody().getTables();
Assert.assertEquals(2, tables.toArray().length);
for (int i = 0; i < tables.getCount(); i++) {
System.out.println(MessageFormat.format("Start of Table {0}", i));
RowCollection rows = tables.get(i).getRows();
for (int j = 0; j < rows.getCount(); j++) {
System.out.println(MessageFormat.format("\tStart of Row {0}", j));
CellCollection cells = rows.get(j).getCells();
for (int k = 0; k < cells.getCount(); k++) {
String cellText = cells.get(k).toString(SaveFormat.TEXT).trim();
System.out.println(MessageFormat.format("\t\tContents of Cell:{0} = \"{1}\"", k, cellText));
}
System.out.println(MessageFormat.format("\tEnd of Row {0}", j));
}
System.out.println(MessageFormat.format("End of Table {0}\n", i));
}
toArray in class NodeCollection