我的jrxml和Java代碼如下.我正在嘗試使用一些文本和值創(chuàng)建報(bào)告.但是,將生成空白報(bào)告.我正在粘貼地圖以將報(bào)告填充為參數(shù).我必須添加4-5條靜態(tài)行,然后添加一個(gè)動(dòng)態(tài)變量.我在詳細(xì)的樂(lè)隊(duì)中添加了這些東西.錯(cuò)了什么
<?xml version="1.0" encoding="UTF-8" ?> <!-- Created with iReport - A designer for JasperReports --> <!DOCTYPE jasperReport PUBLIC "http://JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="Untitled_report_2" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842" columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages" isTitleNewPage="false" isSummaryNewPage="false"> <property name="ireport.scriptlethandling" value="0" /> <property name="ireport.encoding" value="UTF-8" /> <import value="java.util.*" /> <import value="net.sf.jasperreports.engine.*" /> <import value="net.sf.jasperreports.engine.data.*" /> <field name="Field" class="java.lang.String"/> <background> <band height="0" isSplitAllowed="true" > </band> </background> <title> <band height="50" isSplitAllowed="true" > </band> </title> <pageHeader> <band height="50" isSplitAllowed="true" > </band> </pageHeader> <columnHeader> <band height="30" isSplitAllowed="true" > </band> </columnHeader> <detail> <band height="100" isSplitAllowed="true" > <staticText> <reportElement x="20" y="10" width="180" height="30" key="staticText-1"/> <box></box> <textElement> <font/> </textElement> <text><![CDATA[4-5 lines text]]></text> </staticText> <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" > <reportElement x="20" y="40" width="60" height="20" key="textField-1"/> <box></box> <textElement> <font/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{Field}]]></textFieldExpression> </textField> <staticText> <reportElement x="20" y="70" width="160" height="30" key="staticText-2"/> <box></box> <textElement> <font/> </textElement> <text><![CDATA[4-5 lines text]]></text> </staticText> </band> </detail> <columnFooter> <band height="30" isSplitAllowed="true" > </band> </columnFooter> <pageFooter> <band height="50" isSplitAllowed="true" > </band> </pageFooter> <lastPageFooter> <band height="50" isSplitAllowed="true" > </band> </lastPageFooter> <summary> <band height="50" isSplitAllowed="true" > </band> </summary> </jasperReport>
Java代碼:
InputStream fileInput = getApplicationContext().getResource("/WEB-INF/reports/myjrxml.jrxml").getInputStream(); JasperReport jasperReport = JasperCompileManager.compileReport(fileInput); HashMap map = new HashMap(); map.put("Field", "test"); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map); byte[] output1 = JasperExportManager.exportReportToPdf(jasperPrint); String filename = "d:/test.pdf"; FileOutputStream fo = new FileOutputStream(filename); fo.write(output1); fo.close();
解決方法:
您正在將報(bào)告數(shù)據(jù)與參數(shù)混淆.我看不到您有任何報(bào)告數(shù)據(jù),這就是為什么您得到空?qǐng)?bào)告的原因.此外,您要填充一個(gè)名為“ Field”的參數(shù),而不是實(shí)際的報(bào)告數(shù)據(jù).因此,您需要在報(bào)表中創(chuàng)建一個(gè)名為“字段”的參數(shù),然后在詳細(xì)信息區(qū)域中對(duì)其進(jìn)行引用,或者您需要將報(bào)表數(shù)據(jù)傳遞到報(bào)表中,例如
JRBeanCollectionDataSource data = new JRBeanCollectionDataSource(dataSet);jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, data);
其中dataSet是一個(gè)集合,例如List<?>.在列表中,您將擁有一個(gè)對(duì)象,其中屬性之一是字段.
來(lái)源:https://www.icode9.com/content-1-519251.html聯(lián)系客服