我正在使用iReport
并生成大约20个pdf报告。现在我希望报告中的所有字段都为空,其中包含值0(零)。
为此,我使用代码
设置文本字段表达式$F{diamondQty}.doubleValue() == 0.0 ? null : $F{diamondQty}.doubleValue()
并启用该字段的 Blank when null
选项。
它工作正常但需要对所有20个报告中的所有字段执行相同操作。
还有其他更好的解决方案吗?
答案 0 :(得分:0)
也许你可以使用FormatFactory
格式工厂允许您指定自己的日期和数字格式。您可以将其设置为报告属性(formatFactoryClass
),或将其设置为参数(REPORT_FORMAT_FACTORY
)
答案 1 :(得分:0)
以下似乎有效:
下一步:
src
build
像往常一样创建报告(使用日期,参数或字段的文本字段),然后:
com.company.reports.ReportFormatFactory
接下来,在名为com.company.reports
的包(文件夹)中的“src”目录中创建一些源代码。将以下内容粘贴到保存在该目录中的名为ReportFormatFactory.java
的文件中:
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 );
}
}
运行报告时,日期格式应为YYYY/MM/dd
。
如果要更改格式(例如,更改为dd/MM/YYYY
),请更新源代码中的日期格式行,然后重新启动Jaspersoft Studio(类加载器似乎在修改后不会重新加载ReportFormatFactory类)。
为避免每次日期格式更改时都必须重新启动,请使用资源包:
i18n
的新项目文件夹
i18n
ReportsLocale
将 i18n 目录添加到构建过程:
按如下方式更改createDateFormat
方法:
@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 );
}
并将这些常量添加到类定义中(紧跟在 public class
声明之后,在第15/16行附近):
private final static String DATE_FORMAT = "date.format";
private final static String DATE_FORMAT_DEFAULT = "YYYY/MM/dd";
重新启动Jaspersoft Studio,然后:
date.format
属性dd/MM/YYYY
运行报告时,日期应该与29/02/1976类似。
您可以根据需要调整此值以更改字段值。