Tables are one of the most common ways to display structured data in documents. GroupDocs.Assembly for Java makes it easy to generate dynamic tables by iterating over collections and populating table rows with data.
Here are the steps to generate tables from collections:
Create a table in your template document with a header row
Add a data band row using <<foreach>> syntax to iterate over your collection
Reference collection item members in table cells
Close the data band with <</foreach>>
Assemble the document with your collection data
Note
Table-row data bands automatically repeat the table row for each item in the collection, creating a complete table with all data rows.
Generate Table from Java Object Collection
Create a table from a collection of custom objects:
importcom.groupdocs.assembly.*;publicclassProduct{privatefinalStringname;privatefinaldoubleprice;privatefinalintquantity;publicProduct(Stringname,doubleprice,intquantity){this.name=name;this.price=price;this.quantity=quantity;}publicStringgetName(){returnname;}publicdoublegetPrice(){returnprice;}publicintgetQuantity(){returnquantity;}}publicstaticvoidgenerateTableFromCollection()throwsException{// Create sample data
Product[]products={newProduct("Laptop",999.99,5),newProduct("Mouse",29.99,20),newProduct("Keyboard",79.99,15)};// Assemble document
DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("TableTemplate.docx","TableOutput.docx",newDataSourceInfo(products,"products"));System.out.println("Table generated successfully.");}
Note
Because of Java type erasure, the engine cannot determine the item type of a java.util.List passed directly as a data source, so calling item methods such as getName() fails. Pass an array (as shown above), or expose the list through a getter of a wrapper object (for example, public List<Product> getProducts()) and iterate over it in the template: <<foreach [in data.getProducts()]>>.
Template Structure for Tables
In your template document (TableTemplate.docx), create a table with this structure:
Product Name
Price
Quantity
<<foreach [in products]>><<[getName()]>>
<<[getPrice()]>>
<<[getQuantity()]>><</foreach>>
The opening <<foreach>> tag must be placed at the beginning of the first cell of the data row, and the closing <</foreach>> tag at the end of the last cell, so that the data band spans the entire row. Members of Java objects are accessed through their getter methods. After assembly, you’ll get:
Product Name
Price
Quantity
Laptop
999.99
5
Mouse
29.99
20
Keyboard
79.99
15
Generate Table with Formatting
You can format table cell values using format strings:
The :"$#,##0.00" format string formats the price as currency (for example, $999.99). Number format strings follow the java.text.DecimalFormat pattern syntax, and the decimal and grouping separators depend on the current locale.
DataTable columns are referenced by name rather than through getters, so the template (DataTableTemplate.docx) looks as follows:
Product Name
Price
Quantity
<<foreach [in products]>><<[Name]>>
<<[Price]>>
<<[Quantity]>><</foreach>>
Generate Table with Totals
Add a total row after the data rows:
importcom.groupdocs.assembly.*;publicstaticvoidgenerateTableWithTotals()throwsException{Product[]products={newProduct("Laptop",999.99,5),newProduct("Mouse",29.99,20),newProduct("Keyboard",79.99,15)};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("TableWithTotalsTemplate.docx","TableWithTotals.docx",newDataSourceInfo(products,"products"));System.out.println("Table with totals generated successfully.");}
Template structure:
Product Name
Price
Quantity
<<foreach [in products]>><<[getName()]>>
<<[getPrice()]:"$#,##0.00">>
<<[getQuantity()]>><</foreach>>
Total
<<[products.sum(p => p.getPrice())]:"$#,##0.00">>
<<[products.sum(p => p.getQuantity())]>>
Generate Table in Spreadsheet
Tables work similarly in spreadsheet templates:
importcom.groupdocs.assembly.*;publicstaticvoidgenerateTableInSpreadsheet()throwsException{Product[]products={newProduct("Laptop",999.99,5),newProduct("Mouse",29.99,20)};DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("TableTemplate.xlsx","TableOutput.xlsx",newDataSourceInfo(products,"products"));System.out.println("Table generated in spreadsheet successfully.");}
Warning
In spreadsheet templates, the data band is a row of cells: put the opening <<foreach>> tag at the beginning of the first cell of the row and the closing <</foreach>> tag at the end of the last cell of the row. The row is repeated for each item in the collection.
Advanced Usage Topics
To learn more about filtering, sorting, grouping table data, conditional table rows, and advanced table formatting, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: