I. Overview
Use cases: sometimes users may have a demand for processing every single cell in a report. For this purpose, is there a way to traverse all the cells using codes?
Introduction: the method report.cellIterator( )Finereport provides has access to all cells that are placed inside the Iterator of Report, which makes it possible to do cell traversal as with any other Iterator objects, so as to process the cells or take other actions.
In this way, you can traverse all or part of the cells as needed. The specific method and a practical use case will be demonstrated hereinafter.
II. Principle
1. Obtain The Report
Obtain the Report (the container of cells) before cell traversal. Here we take Group.cpt as an example:
// Read and save the template as WorkBook object
workbook = TemplateWorkBookIO.readTemplateWorkBook("/doc/Primary/GroupReport/Group.cpt");
2. Traverse Cells
// Traverse cells
int col = 0, row = 0;
byte direction = 0;
java.util.Iterator it = report.cellIterator();
while (it.hasNext()) {
TemplateCellElement cell = (TemplateCellElement) it.next();
// Get the row and column numbers and exchange values
col = cell.getColumn();
row = cell.getRow();
cell.setColumn(row);
cell.setRow(col);
// Acquire the expansion direction of the original cell, with 0 standing for vertical while 1 for horizontal.
direction = cell.getCellExpandAttr().getDirection();
if (direction == 0) {
cell.getCellExpandAttr().setDirection((byte) 1);
} else if (direction == 1) {
cell.getCellExpandAttr().setDirection((byte) 0);
}
// Add the altered cell into a new WorkSheet
newworksheet.addCellElement(cell);
}
III. Example
1. Switch Reports Horizontally And Vertically
To make it happen, use the input variable to decide whether a horizontal-vertical switch is needed. If so, go on with cell traversal and exchange the numbers of columns and rows.
Please see the Group.cpt below:
Load the template into a class, and control the horizontal/vertical direction using the value of parameter "change".
If the "change" value is 0 or empty, display without change.
If the "change" value is 1, switch the columns and rows of the report, and convert it into a web report.
Please see the detailed code here:
2. Release and Preview
Place the complied class file ChangeRowAndCol.class into %FR_HOME%\webapps\webroot\WEB-INF\classes\com\fr\demo.
Startup the server and go to the following URL
http://localhost:8075/webroot/decision/view/report?viewlet=com.fr.demo.ChangeRowAndCol, with its result as shown below:
Add &change=1 to the end of the URL:
http://localhost:8075/webroot/decision/view/report?viewlet=com.fr.demo.ChangeRowAndCol&change=1 and see the result as follows:
So, the feature of switching report direction has been implemented.