I am working with iReport
and generate around 20 pdf report also.Now开发者_运维技巧 I want all the field blank in the report where its contain value 0 (zero).
For that I set text field expression with the code
$F{diamondQty}.doubleValue() == 0.0 ? null : $F{diamondQty}.doubleValue()
and enable Blank when null
option of the field.
It's working fine but require to do same for all the field of all 20 reports.
Is there any other better solution for that ?
Maybe you can use a FormatFactory.
A format factory allows you to specify your own date and number formats. You can set it as a report property (formatFactoryClass
) , or set it as a parameter (REPORT_FORMAT_FACTORY
)
The following seems to work:
- Click Window >> Prefereces
- Select Capabilities
- Check Development
- Click OK
Next:
- Open the Project Explorer
- Right-click Project name
- Select Properties
- Select Java Build Path
- Click the Source tab
- Click Add Folder
- Select Create New Folder
- Set Folder Name to:
src
- Click Finish
- Select src
- Click OK
- Set Default output folder: Project Name/
build
- Click OK
Create a report as usual (with a text field that uses a date, either a parameter or a field), then:
- Select the report in the Outline panel
- Open the Properties panel
- Set Format Factory Class to:
com.company.reports.ReportFormatFactory
Next, create some source code inside the "src" directory in a package (folder) named com.company.reports
. Paste the following into a file named ReportFormatFactory.java
that is saved in that directory:
import java.text.DateFormat;
import java.util.Locale;
import java.util.TimeZone;
import net.sf.jasperreports.engine.util.DefaultFormatFactory;
/**
* Delegates creation of date and number formatters to JasperReports' default
* formatters. This class ensures that dates are formatted consistently across
* all reports.
*/
public class ReportFormatFactory extends DefaultFormatFactory {
/**
* Returns a DateFormat instance that creates dates in YYYY/MM/dd format.
*
* @param pattern Unused.
* @param locale Passed to the DefaultFormatFactory instance.
* @param timezone Passed to the DefaultFormatFactory instance.
*
* @return An object that can format dates.
*/
@Override
public DateFormat createDateFormat(
String pattern, Locale locale, TimeZone timezone ) {
return super.createDateFormat( "YYYY/MM/dd", locale, timezone );
}
}
When you run the report, the date should be formatted as YYYY/MM/dd
.
If you want to change the format (e.g., to dd/MM/YYYY
), update the date format line in the source code and then restart Jaspersoft Studio (the classloader does not seem to reload the ReportFormatFactory class after modification).
To avoid having to restart each time the date format changes, use a resource bundle:
- Create a new project folder called
i18n
- Right-click on the project name
- Select New >> Folder
- Set Folder name to
i18n
- Click Finish
- Right-click on i18n
- Select New >> Other
- Expand Messages Editor
- Select ResourceBundle
- Click Next
- Set the name to:
ReportsLocale
- Add a Locale (e.g., en_US)
- Click Finish
Add the i18n directory to the build process:
- Right-click i18n
- Select Build Path >> Configure Build Path
- Click Add Folder
- Check i18n
- Click OK
- Click OK again
Change the createDateFormat
method as follows:
@Override
public DateFormat createDateFormat(
String pattern, Locale locale, TimeZone timezone ) {
String dateFormat = DATE_FORMAT_DEFAULT;
try {
ResourceBundle rb = ResourceBundle.getBundle( "EducationReports" );
String df = rb.getString( DATE_FORMAT );
if( df != null ) {
dateFormat = df;
}
}
catch( Exception e ) {
// If the resource bundle isn't found, use the default date format.
// TODO: Pass this exception into a logger.
}
return super.createDateFormat( dateFormat, locale, timezone );
}
And add these constants to the class definition (immediately after the public class
declaration, around line 15/16):
private final static String DATE_FORMAT = "date.format";
private final static String DATE_FORMAT_DEFAULT = "YYYY/MM/dd";
Restart Jaspersoft Studio, then:
- Edit the ReportsLocale file
- Add a
date.format
property - Set the property to
dd/MM/YYYY
- Set the property value for all locales.
When the report is run, the date should look like 29/02/1976, for example.
You can adapt this to change the field value as required.
精彩评论