我正在使用java创建jasper pdf。我想动态地将子报告添加到主报告中。
使用以下代码创建主报告:
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;
}
然后在编译并填充主报表后,子报表打印为空白。但单独的子报表工作。
即使我已尝试将dataSourceExpression传递为 new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ P {notes}),但仍然打印空白。
我没有得到我错的地方,请帮忙
答案 0 :(得分:1)
编译您的设计。
var urlReq:URLRequest = new URLRequest ("http://api.website.net/check.php");
urlReq.method = URLRequestMethod.POST;
var urlVars:URLVariables = new URLVariables();
urlVars.user_id = Main.instance.userID;
urlReq.data = urlVars;
var loader:URLLoader = new URLLoader (urlReq);
loader.addEventListener(Event.COMPLETE, onCreditComplete);
loader.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS, httpStatusHandler);
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(urlReq);
然后将其作为参数添加到主报告中。
JasperReport subReport = JasperCompileManager.compileReport(subJasperDesign);
在jrxml模板中获取此参数:
parameters.put("SUB_TEMPLATE", subReport);
最后是这个参数的jrxml用法示例:
<parameter name="SUB_TEMPLATE" class="net.sf.jasperreports.engine.JasperReport"/>