java-DynamicJasper:如何在空白时删除行
作者:互联网
我正在使用DynamicJasper生成报告,当行为空白时,我想删除报告行.我知道如何在JasperReports中进行操作.
但是谁能建议我如何使用Java代码通过DynamicJasper删除空白行.
解决方法:
我还没有找到在DynamicJasper API
的帮助下解决此问题的简单方法.
但是可以在DJ帮助下解决.
这是用于生成报告的主类的源代码.
public class BasicReportTest {
private JasperPrint m_jasperPrint;
private JasperReport m_jasperReport;
private Map m_params = new HashMap();
private DynamicReport m_dynamicReport;
public DynamicReport buildReport() throws Exception {
Style detailStyle = new Style();
detailStyle.setBorder(Border.THIN);
detailStyle.setBlankWhenNull(true);
Style headerStyle = new Style();
headerStyle.setFont(Font.COURIER_NEW_BIG_BOLD);
headerStyle.setBorder(Border.THIN);
headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
headerStyle.setFont(Font.ARIAL_BIG);
FastReportBuilder drb = new FastReportBuilder();
drb.addColumn("State", "state", String.class.getName(), 30, detailStyle, headerStyle)
.addColumn("Branch", "branch", String.class.getName(), 30, detailStyle, headerStyle)
.addColumn("Item", "item", String.class.getName(), 50, detailStyle, headerStyle)
.addColumn("Amount", "amount", Float.class.getName(), 60, detailStyle, headerStyle)
.setTitle("The report with empty rows")
.setUseFullPageWidth(true);
DynamicReport dr = drb.build();
return dr;
}
public void testReport() throws Exception {
m_dynamicReport = buildReport();
JRDataSource dataSource = getDataSource();
m_jasperReport = DynamicJasperHelper.generateJasperReport(m_dynamicReport,
getLayoutManager(), m_params);
m_jasperPrint = JasperFillManager.fillReport(m_jasperReport, m_params, dataSource);
exportReport();
}
protected LayoutManager getLayoutManager() {
return new CustomLayoutManager();
}
/*... Some code ... */
public static void main(String[] args) throws Exception {
BasicReportTest test = new BasicReportTest();
test.testReport();
}
}
此代码字符串detailStyle.setBlankWhenNull(true);使我们能够在文本字段中将空值显示为空白.就像我们将使用表达式
<textField isBlankWhenNull="true">
在jrxml文件中.
但是我们还需要像这样“生成” xml代码:
<textField isBlankWhenNull="true">
<reportElement ... isRemoveLineWhenBlank="true"/>
如前所述,DJ不包含任何用于调用JRElement.setRemoveLineWhenBlank(boolean isRemoveLineWhenBlank)方法的公共方法(包装器).
这就是为什么我使用定制的LayoutManager
类的原因-在我的示例中,它是CustomLayoutManager类.
这是它的源代码:
public class CustomLayoutManager extends ClassicLayoutManager {
@Override
protected void transformDetailBandTextField(AbstractColumn column, JRDesignTextField textField) {
super.transformDetailBandTextField(column, textField);
if (column.getStyle().isBlankWhenNull()) {
textField.setRemoveLineWhenBlank(true);
}
}
}
我已经覆盖了DJ引擎用于构建Detail区域的方法transformDetailBandTextField.
标签:jasper-reports,dynamic-jasper,java 来源: https://codeday.me/bug/20191210/2098158.html