0
我正在使用java創建jasper pdf。我想動態添加子報表到主報表。如何使用java代碼在主報告中添加子報表
主要報道是使用下面的代碼創建:
JasperDesign jasperDesign = JRXmlLoader.load("/home/report1.jrxml");
jasperDesign.setName("sampleDynamicJasperDesign");
jasperDesign.setPageWidth(595);jasperDesign.setPageHeight(842);jasperDesign.setColumnWidth(595-40-40);jasperDesign.setColumnSpacing(0);
jasperDesign.setLeftMargin(40);jasperDesign.setRightMargin(40);jasperDesign.setTopMargin(20);jasperDesign.setBottomMargin(20);
JRDesignStyle normalStyle = new JRDesignStyle();
normalStyle.setName("Noto Sans");normalStyle.setDefault(true); normalStyle.setFontName("Noto Sans");normalStyle.setFontSize(12);normalStyle.setPdfFontName("Helvetica");normalStyle.setPdfEncoding("Cp1252");normalStyle.setPdfEmbedded(false);jasperDesign.addStyle(normalStyle);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(createHeaderSubreport(parameters));
//創建報表
private JRBand createHeaderSubreport(Map<String, Object> parameters) throws JRException {
JasperDesign jasperDesign = new JasperDesign();
jasperDesign.setLanguage("java");
jasperDesign.setPageWidth(595); jasperDesign.setPageHeight(842);jasperDesign.setColumnWidth(595-40-40);jasperDesign.setColumnSpacing(0);jasperDesign.setLeftMargin(40);jasperDesign.setRightMargin(40);jasperDesign.setTopMargin(20); jasperDesign.setBottomMargin(20);
jasperDesign.setTitle(null);jasperDesign.setColumnHeader(null);
JRDesignParameter designParameter = new JRDesignParameter(); designParameter.setName("LeftText");designParameter.setValueClass(String.class);
jasperDesign.addParameter(designParameter);
JRDesignStyle normalStyle = new JRDesignStyle();
normalStyle.setName("Noto Sans");normalStyle.setDefault(true);normalStyle.setFontName("Noto Sans");normalStyle.setFontSize(12); normalStyle.setPdfFontName("Helvetica");normalStyle.setPdfEncoding("Cp1252"); normalStyle.setPdfEmbedded(false);jasperDesign.addStyle(normalStyle);
JRDesignBand band = new JRDesignBand();
band.setHeight(50);
JRDesignStaticText designStaticText = new JRDesignStaticText();
designStaticText.setText("Header");
designStaticText.setHeight(10);
designStaticText.setWidth(100);
designStaticText.setX(0);designStaticText.setY(0);
band.addElement(designStaticText);
JRDesignTextField jrDesignTextField = new JRDesignTextField();
JRDesignExpression jrExpression = new JRDesignExpression();
jrExpression.setText("$P{LeftText}");
jrDesignTextField.setExpression(jrExpression);
jrDesignTextField.setY(0);
jrDesignTextField.setHeight(20);
jrDesignTextField.setWidth((38*515)/100);
jrDesignTextField.setMarkup("html");
jrDesignTextField.setHorizontalTextAlign(HorizontalTextAlignEnum.LEFT);
jrDesignTextField.setStretchWithOverflow(true);
band.addElement(jrDesignTextField);
((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JRDesignSubreport jSubreport = new JRDesignSubreport(jasperDesign);
jSubreport.setUsingCache(false);
jSubreport.setRemoveLineWhenBlank(true);
jSubreport.setPrintRepeatedValues(false);
jSubreport.setWidth(500);
jSubreport.setHeight(80);
jSubreport.setX(0);jSubreport.setY(0);
JRDesignExpression expression = new JRDesignExpression();
expression.setText("new net.sf.jasperreports.engine.JREmptyDataSource(1)");
jSubreport.setDataSourceExpression(expression);
jSubreport.setExpression(expression);
JRDesignSubreportParameter designSubreportParameter = new JRDesignSubreportParameter(); designSubreportParameter.setName("REPORT_CONNECTION");
JRDesignExpression exp = new JRDesignExpression("$P{REPORT_CONNECTION}"); designSubreportParameter.setExpression(exp);
jSubreport.addParameter(designSubreportParameter);
designSubreportParameter = new JRDesignSubreportParameter(); designSubreportParameter.setName("LeftText");
exp = new JRDesignExpression("$P{LeftText}"); designSubreportParameter.setExpression(exp);
jSubreport.addParameter(designSubreportParameter);
band = new JRDesignBand();
band.setHeight(80);
band.addElement(jSubreport);
return band;
}
然後compling和填充主報表後,子報表打印成blank.But單獨子報表的作品。
即使我已經通過傳遞dataSourceExpression爲新net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ P {}筆記)還是其打印空白的嘗試。
我沒有得到我錯的地方,請幫忙
我如何使用Java報表添加,我不慣於在jrxml中執行任何配置,因爲報告元素的高度和寬度將在運行時決定。 –
如何通過'JasperReport subReport'作爲使用java的子報表表達式? –