I. Overview
We know from the core structure that users are allowed to edit report properties inside a class. In this article, we'll be covering a complete example on how to load, modify and re-export a report in this way. Please see the effect as shown below.
II. Principle
1. Load a Report
There are two ways of loading a cpt file and converting it to a WorkBook object.
Load the file with its absolute path.
Define the report runtime environment, from which load the file with its relative path.
1) Load the file with its absolute path
WorkBook workbook = (WorkBook) TemplateWorkBookIO
.readTemplateWorkBook(
"/doc/Primary/Parameter/Parameter.cpt");
2) Load the file with its relative path from the environment
//Define the report runtime environment to load the report
String envpath = "C:\\FineReport_10.0\\webapps\\webroot\\WEB-INF"; //The project path
SimpleWork.checkIn(envpath);
module.start();
2. Edit Properties
//Get the WorkSheet in WorkBook to modify the foreground color of cell A1 to read
TemplateElementCase report = (TemplateElementCase) workbook
.getReport(0);
//For getCellElement(int column, int row), the "column" and "row" both start from 0, so cell A1 should be column 0, row 0
CellElement cellA1 = report.getCellElement(0, 0);
FRFont frFont = FRFont.getInstance();
frFont = frFont.applyForeground(Color.red);
Style style = Style.getInstance();
style = style.deriveFRFont(frFont);
cellA1.setStyle(style);
3. Export a Report
//Save the template
FileOutputStream outputStream = new FileOutputStream(new File(
"C:\\test\\newParameter1.cpt"));
((WorkBook) workbook).export(outputStream);
III. Instructions
1. The Complete Executable Code
Note: please modify the project path and export path according to your practical situation.
2. View the Exported Result
Compile and execute the above code to find the exported report newParameter.cpt in the defined folder. Open this template with the designer, and you can see that the foreground color of A1 has changed to red.