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