Convert Excel To Template CPT

  • Last update:  2020-12-17
  • I. Overview

    When dealing with multiple Excel files, users may need to batch convert them with class datasets into cpt templates, saving the trouble of manually import via the designer.

    II. Solution

    Load the Excel file in the project, convert it into TemplateWorkBook, and then output the cpt file.

    III. Example 

    1. Load an Excel File and Convert to a Template 

    FileInputStream

    File excelFile = new File("D:\\aa.xlsx"); // get the EXCEL file
    FileInputStream a = new FileInputStream(excelFile);

    2. Output the CPT File

    For Excel2003

    TemplateWorkBook tpl = new ExcelReportImporter().generateWorkBookByStream(a);
    OutputStream outputStream = new FileOutputStream(new File("D:\\abc.cpt")); //convert to cpt template
    ((WorkBook) tpl).export(outputStream);

    For Excel2007

    TemplateWorkBook tpl = new Excel2007ReportImporter().generateWorkBookByStream(a);
    OutputStream outputStream = new FileOutputStream(new File("D:\\abc.cpt")); //convert to cpt template
    ((WorkBook) tpl).export(outputStream);

    3. Prepare the Excel File

    531.png

    Save aa.xls and aa.xlsx into corresponding paths, and modify the file paths accordingly in the code below.


    4. Obtain the Complete Executable Code

    Note: this article takes the code for xlsx as an example. For xls files, please rewrite the code according to section III.2, and modify the import package accordingly.

    In the case of implementing tailored features on the basis of FineReport designer, normally you can launch the designer by calling the FineReport startup class in Eclipse or IDEA, in preparation for debugging and custom development.

    Please bring in JARs of the FineReport designer. Refer Start the Designer in Eclipse for detailed information.

    Note: the report runtime environment should be defined according to the practical installation path of the designer. For example, if the designer was installed on C: drive, use String envPath = "C://FineReport_10.0//webapps//webroot//WEB-INF". Also: Excel file paths need to be modified accordingly. For example, D:\\aa.xlsx.

    Please see below for the detailed code: 

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


    5. Compile and Run 

    A template file named "abc.cpt" will be generated in the specified path once this feature is compiled and run. Open the template and see that the data within the Excel file has been saved.

    532.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