Overview
Application Scenario
FineReport allows you to export reports as files of various general formats (such as PDF, Excel, and Word) during preview. The FineReport designer also allows you to export templates as files of general formats or templates with built-in datasets, as shown in the following figure.
How can you export files directly from the backend without previewing reports or using the designer?
Implementation Method
FineReport provides powerful input and output functions. All the input and output classes are stored in the com.fr.report.io package.
Through report input, you can create a WorkBook object from the template file (in XML format) of a report, which has been introduced in the Report Invoking chapter. Through report output, you can save a report in various file formats, such as CPT, CPT (built-in data), PDF, Excel, Word, SVG, CSV, and PNG/JPG/GIF/BMP images (which is achieved by compiling a Java program to invoke backend project resources and export the template file of the report). Once the export is complete, the process is released.

API Description

1. Export as a Template with Built-in Datasets
The API queries the data source of the original template based on parameter conditions, converts the query results into built-in datasets, and exports the template without performing calculations on the original template. The code is as follows:
/**Export a template workbook as a template with built-in datasets.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"EmbExport.cpt"));
EmbeddedTableDataExporter templateExporter = new EmbeddedTableDataExporter();
templateExporter.export(outputStream, workbook);
2. Export as a Template File
The API allows the original template to be programmatically edited and then exported as a template file, or to be saved from one path to another. The code is as follows:
/**Export a template workbook as a template file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"TmpExport.cpt"));
((WorkBook) workbook).export(outputStream);
3. Export as a 2003 Excel File
The API allows the result workbook (ResultWorkBook) obtained after the execution of a template workbook (WorkBook) to be exported as a 2003 Excel file. The code is as follows:
/**Export a result workbook to a 2003 Excel file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"ExcelExport2003.xls"));
ExcelExporter ExcelExport = new ExcelExporter();
ExcelExport.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
4. Export as a 2007 Excel File
The API allows the result workbook (ResultWorkBook) obtained after the execution of a template workbook (WorkBook) to be exported as a 2007 Excel file. The code is as follows:
/**Export a result workbook as a 2007 Excel file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"ExcelExport2007.xlsx"));
StreamExcel2007Exporter ExcelExport1 = new StreamExcel2007Exporter();
ExcelExport1.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
5. Export as a Word File

/**Export a result workbook as a Word file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"WordExport.doc"));
WordExporter WordExport = new WordExporter();
WordExport.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
6. Export as a PDF File
/**Export a result workbook as a PDF file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"PdfExport.pdf"));
PDFExporter PdfExport = new PDFExporter();
PdfExport.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
7. Export as a TXT File
/**Export a result workbook as a TXT file. (TXT files do not support elements such as tables and charts, and templates to be exported are typically composed of detail tables.)*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"TxtExport.txt"));
TextExporter TxtExport = new TextExporter();
TxtExport.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
8. Export as a CSV File
/**Export a result workbook as a CSV file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"CsvExport.csv"));
CSVExporter CsvExport = new CSVExporter();
CsvExport.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
9. Export as an SVG File
/**Export a result workbook as an SVG file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"SvgExport.svg"));
SVGExporter SvgExport = new SVGExporter();
SvgExport.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
10. Export as an Image File

/**Export a result workbook as an image file.*/
outputStream = new FileOutputStream(new java.io.File(outputUrl+"PngExport.png"));
ImageExporter ImageExport = new ImageExporter();
ImageExport.export(outputStream, workbook.execute(parameterMap, new WriteActor()));
11. Process Release
After you export files using the export APIs, many processes may have been created and can be released by adding the following code after the export.
outputStream.close();
module.stop();
Example
This section describes how to export the GettingStartedEN.cpt template (with a parameter whose default value is specified as New York) from the backend.
Preparing the Compilation Environment
Before compiling the program, you need to create a Java project environment and prepare a Java editor, such as Eclipse or IntelliJ IDEA.
Import the JAR packages of your FineReport project into the editor. The JAR packages include:
13 JAR packages with names starting with fine in the %FR_HOME%\webapps\webroot\WEB-INF\lib path
sqlite-jdbc.jar in the %FR_HOME%\webapps\webroot\WEB-INF\lib path
servlet-api.jar in the %Tomcat_HOME%\libTomcat path
tools.jar in the %JAVA_HOME%\jdk\lib JDK path
slf4j-simple-1.7.25.jar, which can be downloaded by clicking slf4j-simple-1.7.25.jar.
If the report requires database querying, you also need to import the corresponding JDBC driver or JAR packages of plugins. For example, if a JSON dataset is used, you need to import the JAR package of the JSON Dataset plugin.
Writing the Java Program
Write the Java program ExportApi.java in the editor. The program retrieves the template and exports it as a file in a specified format after the necessary classes are imported. You can view the complete codes below.

In the codes, StateServiceActivator is a modification introduced in FineReport with the JAR packages released after 2020.4.26. If an error occurs in earlier versions, you just need to replace StateServiceActivator with StateServerActivator.
Ensure that you have replaced the project paths, template names, and export paths in the example codes with the corresponding items in your own project.
Currently, the codes in the following links are commented in Chinese. You can use the browser's built-in translator to translate the web page to understand the codes.
Code for general reports (in CPT format):
https://code.fanruan.com/demo/example/src/branch/persist/10.0/src/main/java/com/fr/io/ExportApi.java
Code for dashboards (in FRM format):
Compiling the Java File
After the Java program is written completely, compile ExportApi.java in the compiler. Once the compilation is successful, run the Java code, and check whether files in different formats are generated in the folder of the export path specified in the code. If so, the export is successful, as shown in the following figure.
If you want to automatically trigger the program to export templates, you can use Java to write scheduled tasks for program execution. You can also achieve scheduled triggering through the Custom Attachment Processing function in Task Schedule, as shown in the following figure.
Notes
FineDB Locking
Problem
The error message "SQLException: Database lock acquisition failure: lockFile: ..." appears when you run the compiled code, as shown in the following figure.
Cause
Since the built-in database for reports is an HSQL database, which does not support multithreaded access, FineDB will be locked when you open the operating environment (for example, the designer) during export, leading to error messages.
Solution
Solution One
Close the Java editor (for example, IntelliJ IDEA or Eclipse). Delete the db.lck file from the %FR_HOME%\webapps\webroot\WEB-INF\embed\finedb path, reopen the Java editor, and run the code again.
Note: This solution may still result in error messages after one run, requiring you to repeatedly delete the db.lck file.
Solution Two
Since the built-in HSQL database in the report project does not support multithreaded access, you can migrate the data from the HSQL database to another database. For details about the migration method, see External Database Configuration.