Modify And Export The Template

  • Last update:  2020-12-17
  • 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.

    modify and export template 1.png

    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

    https://github.com/finereport-overseas/example/blob/release/10.0/src/main/java/com/fr/io/SimpleDemo.java

    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.

    1607072288705923.png



    Attachment List


    Theme: Secondary Development
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy