如何向 ColumnBuilder 添加多个 columnProperties(字段)?
How to add multiple columnProperties (fields) to ColumnBuilder?
我正在使用以下代码在 jasper 报告中显示列属性。但是我无法将三个属性值获取到单列。是否有可能在单个列中使用“,”显示三个属性。
该文件包含以下代码:
DynamicReportBuilder drb = new DynamicReportBuilder();
drb.setTitle("Transaction List Export")
.setSubtitle("This report was generated at " + new Date())
.setDetailHeight(15) // defines the height for each record of the report
.setPrintColumnNames(true)
.setIgnorePagination(true) // for Excel, we may don't want pagination, just a plain list
.setMargins(30, 20, 0, 15) // define the margin space for each side (top, bottom, left and right)
.setDefaultStyles(titleStyle, subtitleStyle, headerStyle, detailStyle)
.setColumnsPerPage(1, 10)
.setUseFullPageWidth(true) // we tell the report to use the full width of the page. this resizes
// the columns width proportionally to meat the page width.
.setAllowDetailSplit(false)
.setReportName("Client List");
AbstractColumn columnClientLocation = ColumnBuilder.getNew()
.setColumnProperty("ClientAddress", String.class.getName()+Constants.COMMA)
.setColumnProperty("ClientCity",String.class.getName()+Constants.COMMA)
setColumnProperty("ClientPostalCode",String.class.getName())
.setTitle(messages.getMessage(locale, "group.terminalinfo"))
.setWidth(80)
.build();
width = width + 80;
/**
* We add the columns to the report (through the builder) in the
* order we want them to appear
*/
if(myContainer.getServiceProvider().equalsIgnoreCase("GOOG")) {
drb.addColumn(columnTransactionActivity)
.addColumn(columnClientLocation);
}
我无法在 jasper 报告的单个列中获取 ClientAddress
、ClientCity
和 ClientPostalCode
的值。
我想在一个列中显示所有这三个属性。
您可以不能使用","
您需要使用CustomExpression
才能达到预期效果
例子
首先将您的字段添加到报告中,以便可以访问它们
drb.addField("ClientAddress", String.class.getName());
drb.addField("ClientCity", String.class.getName());
drb.addField("ClientPostalCode", String.class.getName());
然后用 CustomExpression
创建 AbstractColumn
AbstractColumn columnClientLocation = ColumnBuilder.getNew().setCustomExpression(
return new CustomExpression() {
public Object evaluate(Map fields, Map variables, Map parameters) {
String clientAddress = (String) fields.get("ClientAddress");
String clientCity = (String) fields.get("ClientCity");
String clientPostalCode = (String) fields.get("ClientPostalCode");
return clientAddress + ", " + clientCity + ", " + clientPostalCode;
}
public String getClassName() {
return String.class.getName();
}
}
).build();
我正在使用以下代码在 jasper 报告中显示列属性。但是我无法将三个属性值获取到单列。是否有可能在单个列中使用“,”显示三个属性。
该文件包含以下代码:
DynamicReportBuilder drb = new DynamicReportBuilder();
drb.setTitle("Transaction List Export")
.setSubtitle("This report was generated at " + new Date())
.setDetailHeight(15) // defines the height for each record of the report
.setPrintColumnNames(true)
.setIgnorePagination(true) // for Excel, we may don't want pagination, just a plain list
.setMargins(30, 20, 0, 15) // define the margin space for each side (top, bottom, left and right)
.setDefaultStyles(titleStyle, subtitleStyle, headerStyle, detailStyle)
.setColumnsPerPage(1, 10)
.setUseFullPageWidth(true) // we tell the report to use the full width of the page. this resizes
// the columns width proportionally to meat the page width.
.setAllowDetailSplit(false)
.setReportName("Client List");
AbstractColumn columnClientLocation = ColumnBuilder.getNew()
.setColumnProperty("ClientAddress", String.class.getName()+Constants.COMMA)
.setColumnProperty("ClientCity",String.class.getName()+Constants.COMMA)
setColumnProperty("ClientPostalCode",String.class.getName())
.setTitle(messages.getMessage(locale, "group.terminalinfo"))
.setWidth(80)
.build();
width = width + 80;
/**
* We add the columns to the report (through the builder) in the
* order we want them to appear
*/
if(myContainer.getServiceProvider().equalsIgnoreCase("GOOG")) {
drb.addColumn(columnTransactionActivity)
.addColumn(columnClientLocation);
}
我无法在 jasper 报告的单个列中获取 ClientAddress
、ClientCity
和 ClientPostalCode
的值。
我想在一个列中显示所有这三个属性。
您可以不能使用","
您需要使用CustomExpression
才能达到预期效果
例子
首先将您的字段添加到报告中,以便可以访问它们
drb.addField("ClientAddress", String.class.getName());
drb.addField("ClientCity", String.class.getName());
drb.addField("ClientPostalCode", String.class.getName());
然后用 CustomExpression
AbstractColumn
AbstractColumn columnClientLocation = ColumnBuilder.getNew().setCustomExpression(
return new CustomExpression() {
public Object evaluate(Map fields, Map variables, Map parameters) {
String clientAddress = (String) fields.get("ClientAddress");
String clientCity = (String) fields.get("ClientCity");
String clientPostalCode = (String) fields.get("ClientPostalCode");
return clientAddress + ", " + clientCity + ", " + clientPostalCode;
}
public String getClassName() {
return String.class.getName();
}
}
).build();