开发者

set blank in the field when its contain value 0 (zero)

开发者 https://www.devze.com 2023-01-21 16:04 出处:网络
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).

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:

  1. Click Window >> Prefereces
  2. Select Capabilities
  3. Check Development
  4. Click OK

Next:

  1. Open the Project Explorer
  2. Right-click Project name
  3. Select Properties
  4. Select Java Build Path
  5. Click the Source tab
  6. Click Add Folder
  7. Select Create New Folder
  8. Set Folder Name to: src
  9. Click Finish
  10. Select src
  11. Click OK
  12. Set Default output folder: Project Name/build
  13. Click OK

Create a report as usual (with a text field that uses a date, either a parameter or a field), then:

  1. Select the report in the Outline panel
  2. Open the Properties panel
  3. 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:

  1. Create a new project folder called i18n
    1. ​Right-click on the project name
    2. Select New >> Folder
    3. Set Folder name to i18n
    4. Click Finish
  2. Right-click on i18n
  3. Select New >> Other
  4. Expand Messages Editor
  5. Select ResourceBundle
  6. Click Next
  7. Set the name to: ReportsLocale
  8. Add a Locale (e.g., en_US)
  9. Click Finish

Add the i18n directory to the build process:

  1. Right-click i18n
  2. Select Build Path >> Configure Build Path
  3. Click Add Folder
  4. Check i18n
  5. Click OK
  6. 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:

  1. Edit the ReportsLocale file
  2. Add a date.format property
  3. Set the property to dd/MM/YYYY
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消