I. Overview
1.1 Requirement
FineReport has the built-in export function, but does not support exporting a custom file name. How can we export a custom file name?
1.2 Solution
Method 1: Modify the title in the Web Attributes interface to export a custom file name (the file name exported by default is the same as the report title).
Method 2: Add an event to the Export button and assign the custom file name to be exported as the value of the parameter __filename__ in the event (note: there are two single underscores before and after filename).
You will learn |
---|
|
II. Sample
2.1 Method 1: Modify the title in the Web Attributes interface
1) Open %FR_HOME%\webapps\webroot\WEB-INF\reportlets\GettingStarted.cpt
2) Click [Template]-[Template Parameter] and add date as a template parameter.
3) Click [Template]-[Web Attributes] and type the title: ${region+"_region_"+date}
4) Modify the title to a dynamic one: $region+"Region Sales Situation"
5) Edit the parameter pane and delete the Query button.
6) Add a Label widget and set the widget value as date:. Add a Date widget and name it date. Bind the Date widget to the template parameter and set the widget value as Formula. Input the formula TODAY() in the pop-up Formula Definition window.
7) Add an After Editing event respectively to the Drop-down Box widget and the Date widget to make query run automatically:
Input the following JavaScript codes:
_g().parameterCommit();
8)Add a Button widget and name it Export
Add a Click event:
Input the following JavaScript codes:
var REGION = this.options.form.getWidgetByName("region").getValue();
var DATE = this.options.form.getWidgetByName("date").getValue();
var REPORT_URL = '${servletURL}?viewlet=GettingStarted1.cpt®ion=' + REGION + '&date=' + DATE + '&format=excel';
window.location = (FR.cjkEncode(REPORT_URL));
9)Click the blank area of the parameter pane. Click the Properties tab in the right-hand pane and uncheck [Display Nothing Before Query].
2.2 Method 2: Add an event to the Export button
1) Make modifications on the basis of the steps mentioned in Method 1. Click [Template]-[Web Attributes] and remove the title.
2) Add a Click event
Click on the Export Button, add a Click event, and rewrite JavaScript codes:
Input the following JavaScript codes:
var REGION = this.options.form.getWidgetByName("region").getValue();
var DATE = this.options.form.getWidgetByName("date").getValue();
var name = REGION + "_region_" + DATE;
var REPORT_URL = '${servletURL}?viewlet=GettingStarted2.cpt®ion=' + REGION + '&date=' + DATE + '&format=excel' + '&__filename__=' + name;
window.location = (FR.cjkEncode(REPORT_URL));
2.3 Preview
Save the template and click [Pagination Preview]. The preview effect on a PC is shown as below: