I want to generate a pdf report using ejbql ,but I dont know how to do that.
I have tried to do thye following but an error occure:
parameterMap.put(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER, entityManager);
parameterMap.put("priority", "3");
try
{
JasperFillManager.fillReportToFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.jasper", parameterMap);
JasperRunManager.runReportToPdfFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf", parameterMap);
}
catch (JRException ex)
{
ex.printStackTrace();
}
Tehb error is:
net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Documents and Settings\My Documents\NetBeansProjects\JiraMap\src\java\Reports\Test.pdf
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:101)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdfFile(JasperRunManager.java:93)
at Reporting.ReportBean.main(ReportBean.java:38)
Caused by: java.io.EOFException
UPdate
I have use this code (I haven't use any parameter ) but always the report is empty:
JasperReport report = JasperCompileManager.compileReport("C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.jrxml");
JasperPrint print = JasperFillManager.fillReport(report,null);
JasperExportManager.exportReportToPdfFile(print,"C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf");
My report with the Aggregate Function count(I have tried to use the alias as numebr but it didn't work):
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Test" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="priority" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString language="ejbql">
<![CDATA[select count(j) from Jiraissue j where j.priority='3']]>
</queryString>
<field name="count(j)" class="java.lang.Long"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="84" splitType="Stretch">
<staticText>
<reportElement x="28" y="0" width="100" height="20"/>
<textElement/>
<text><![CDATA[count(j)]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="50" y="11" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.Long"> <![CDATA[$F{count(j)}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
The error is :
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : count(j)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDat aSource.java:123)
at net.sf.jasperreports.engine.data.JRJpaDataSource$PropertyReader.getValue(JRJpaDataSource.java:206)
at net.sf.jasperreports.engine.data.JRJpaDataSource.getFieldValue(JRJpaDataSource.java:131)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:821)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:785)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1482)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:126)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:118)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:435)
at Reporting.ReportBean.main(ReportBean.java:41)
Caused by: java.lang.NoSuchMethodException: Unknown property 'count'+ on bean class 'class java.lang.Long'
[EL Info]: 2011-06-29 11:05:12.606--ServerSession(27055962)--file:/C:/Documents and Settings/a497165/My Documents/NetBeansProjects/JiraMap/build/web/WEB-INF/classes/_JiraMapPU logout successful
at org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:624)
at org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:570)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:758)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 10 more
This is part of the code I used for generating with JasperPrint; and sending the file back to a browser response. This using JDBC though, not EJBQL. But the JasperReports part of the code is the same.
FileResolver fileResolver = new FileResolver() {
#Override
public File resolveFile(String fileName) {
return new File(getServletContext().getRealPath("/") + fileName);
}
};
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("REPORT_FILE_RESOLVER", fileResolver);
...... // blah blah
JasperPrint jrprint = JasperFillManager.fillReport(input, parameters, conn);
byte[] reportBytes;
jrprint.setName("MyFile." + fileFormat);
response.setContentType("APPLICATION/OCTET-STREAM");
String disHeader = "Attachment;Filename=\"MyFile.";
ServletOutputStream os = response.getOutputStream();
response.setHeader("Content-Disposition", disHeader);
reportBytes = JasperExportManager.exportReportToPdf(jrprint);
os.write(reportBytes);
You need to pass all the parameters needed as below:
JasperPrint jrprint = JasperFillManager.fillReport(input, parameters, connections);
Related
I have been trying to create a jasper report which should look like the following image:
For my sample report, Out-1 and Out-2 are rows for the outer table and asd, asds, adasd are rows for the inner table.
I am trying to achieve it using nested tables. But the table takes the field and I can not assign field to the inner-table.
I am creating jasper reports in Java code rather than using SQL connection to the table for some reason.
JXML
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ec55a262-fec4-45f4-9b48-5be98088aafa">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="New Dataset 1" uuid="cdef25ec-abc4-45ba-b70e-28d82716626b">
<field name="sachNr" class="java.lang.String"/>
<field name="akundenNr" class="java.lang.String"/>
<field name="nestedTable" class="java.lang.String"/>
</subDataset>
<subDataset name="New Dataset 2" uuid="eec983aa-a227-4a28-9c44-73cbe31fa024">
<field name="packStNr" class="java.lang.String"/>
<field name="prodDatum" class="java.lang.String"/>
<field name="stueck" class="java.lang.String"/>
</subDataset>
<parameter name=" artikeldatenTable" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<componentElement>
<reportElement key="table" x="0" y="28" width="360" height="50" uuid="fae14075-18a8-4fcc-b6f9-c14e3623e75d"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="New Dataset 1" uuid="17a10558-3a43-47c2-809d-6362924e5015">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource(1)]]></dataSourceExpression>
</datasetRun>
<jr:column width="137" uuid="7a8ca2ff-7a1a-41a0-8c30-b02f7be6e2b9">
<jr:tableHeader height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="137" height="30" uuid="0fe0e502-dea8-45f6-9f96-633f5db90cd8"/>
<textFieldExpression><![CDATA[$F{sachNr}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell height="42" rowSpan="1">
<componentElement>
<reportElement key="table" x="0" y="0" width="137" height="42" uuid="9e72ca8c-182d-41c0-a021-98493162c7da"/>
<jr:table>
<datasetRun subDataset="New Dataset 2" uuid="7475e256-4bc5-4e88-be92-b6d2eb2fd4c5">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
</datasetRun>
<jr:column width="90" uuid="cd851138-c894-459f-91c9-636c3602b247">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="ce21338e-c593-4bae-b7c4-08741127575c"/>
<textFieldExpression><![CDATA[$F{packStNr}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="5f2b25a9-e34a-4698-b1ab-e8843dafec3c">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="5487305d-d22f-46fa-97bc-88e671f7085c"/>
<textFieldExpression><![CDATA[$F{prodDatum}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="93c28f55-9bda-4be0-989a-03ce573715ba">
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="20" uuid="53c4e2b3-7591-49b3-9b13-50cc1f16641a"/>
<textFieldExpression><![CDATA[$F{stueck}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</jr:detailCell>
</jr:column>
<jr:column width="90" uuid="f898d2e7-1c2f-48fe-9357-61ee5d767a51">
<jr:tableHeader height="30" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="90" height="30" uuid="16b70095-2900-46f0-96ec-d638edb784f3"/>
<textFieldExpression><![CDATA[$F{akundenNr}]]></textFieldExpression>
</textField>
</jr:tableHeader>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
<jr:column width="111" uuid="0121d9c8-d1f0-447f-84ed-15385693a072">
<jr:tableHeader height="30" rowSpan="1"/>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
<jr:column width="90" uuid="ca27e48c-a1d6-4c32-a1de-2580b6786b3d">
<jr:tableHeader height="30" rowSpan="1"/>
<jr:detailCell height="42" rowSpan="1"/>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Outer Table POJO
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class OuterTable {
private String sachNr;
private String akundenNr;
private String SUBREPORT_DIR;
private JRBeanCollectionDataSource nestedTable;
public String getSachNr() {
return sachNr;
}
public void setSachNr(String sachNr) {
this.sachNr = sachNr;
}
public String getAkundenNr() {
return akundenNr;
}
public void setAkundenNr(String akundenNr) {
this.akundenNr = akundenNr;
}
public String getSUBREPORT_DIR() {
return SUBREPORT_DIR;
}
public void setSUBREPORT_DIR(String sUBREPORT_DIR) {
SUBREPORT_DIR = sUBREPORT_DIR;
}
public JRBeanCollectionDataSource getNestedTable() {
return nestedTable;
}
public void setNestedTable(JRBeanCollectionDataSource nestedTable) {
this.nestedTable = nestedTable;
}
}
Inner Table POJO
public class InnerTable {
private String packStNr;
private String prodDatum;
private Integer stueck;
public String getPackStNr() {
return packStNr;
}
public void setPackStNr(String packStNr) {
this.packStNr = packStNr;
}
public String getProdDatum() {
return prodDatum;
}
public void setProdDatum(String prodDatum) {
this.prodDatum = prodDatum;
}
public Integer getStueck() {
return stueck;
}
public void setStueck(Integer stueck) {
this.stueck = stueck;
}
}
My Controller
private void generateLagerbestand(List<Artikeldaten> artikelList) throws JRException, FileNotFoundException {
JasperReport jasperReport;
Map<String, Object> parameters = new HashMap<String, Object>();
List <OuterTable> outerTableList = new ArrayList<OuterTable>();
for(int i = 0; i < artikelList.size(); i++) {
List <InnerTable> innerTableList = new ArrayList<InnerTable>();
List<Lager> lagersList = artikelList.get(i).getLagers();
for(int j = 0; j < lagersList.size(); j++) {
InnerTable innerTable = new InnerTable();
innerTable.setPackStNr(lagersList.get(j).getPackstnr());
innerTable.setProdDatum(lagersList.get(j).getProddatum().toString());
innerTable.setStueck(lagersList.get(j).getStueck());
innerTableList.add(innerTable);
}
OuterTable outerTable = new OuterTable();
outerTable.setAkundenNr(artikelList.get(i).getAkundennr());
outerTable.setSachNr(artikelList.get(i).getSachnr());
JRBeanCollectionDataSource nestedTable = new JRBeanCollectionDataSource(innerTableList);
outerTable.setNestedTable(nestedTable);
outerTableList.add(OuterTable);
}
try {
jasperReport = JasperCompileManager.compileReport("PathToMyReport\\MyReport.jrxml");
JRBeanCollectionDataSource outerTableItems = new JRBeanCollectionDataSource(outerTableList);
// artikeldatenTable is outerTable
parameters.put("artikeldatenTable", outerTableItems);
JREmptyDataSource jrEmptyDataSource = new JREmptyDataSource();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrEmptyDataSource);
OutputStream outputStream = new FileOutputStream(new File("PathToMyPDF:\\MyPDF.pdf"));
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
} catch(Exception ex) {
ex.printStackTrace();
}
}
You are missing the concept of the detail band, the detail will iterate on the datasource you pass to report
JasperFillManager.fillReport(jasperReport, parameters, jrEmptyDataSource)
That's an empty datasource!, it will not iterate at all.
Instead, pass
JRBeanCollectionDataSource outerTableItems = new JRBeanCollectionDataSource(outerTableList);
To the report, define the fields of OuterTable in your main report (not a subDatasource) including nestedTable
<field name="nestedTable" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
Then
In detail band you put textFields related to first level "Out-1" and "Out-2" in your example.
Bellow you add the jr:table component taking the datasource from the bean
<datasetRun subDataset="New Dataset 1" uuid="17a10558-3a43-47c2-809d-6362924e5015">
<dataSourceExpression><![CDATA[$F{nestedTable}]]></dataSourceExpression>
</datasetRun>
In general within the bean I would not have JRBeanCollectionDatasource but instead store the data in a normal List<>, the reason is that a JRDatasource is consumable you can only use it once, see this to understand better How to use same JRBeanCollectionDataSource on multiple sub reports?
I am trying to pass list of days from bean to jasper plugin in Netbeans but i get an error.
I have followed this guide http://javabeansinjasper.blogspot.cz/2011/01/javabeans-as-datasource-in.html#comment-form
Error:
Error filling print... Error retrieving field value from bean : den
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : den
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:109)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1358)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1259)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1235)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1614)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:150)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:963)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:892)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:114)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:584)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:930)
at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:928)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1423)
at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2033)
Caused by: java.lang.NoSuchMethodException: Unknown property 'den' on class 'class java.lang.String'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1322)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:770)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:846)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111) ... 15 more
Here is a bean:
public class ListDays {
public ListDays(String den) {
this.den = den;
}
public ListDays() {
}
public String den;
public String getDen() {
return den;
}
public void setDen(String den) {
this.den = den;
}
public static Collection getSeznamDnu(){
List<String> seznamDnu = new ArrayList<>();
try {
for (int i = 0; i<LocalDate.now().getMonth().length(LocalDate.now().isLeapYear()) ;i++){
LocalDate prvniDenMesice = LocalDate.now().withDayOfMonth(1);
seznamDnu.add(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd")));}
} catch (Exception e) {
System.out.println(e);
}
return seznamDnu;
}
}
And here is a report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="null" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0f6a9791-50d4-4aff-9b3f-da0e5f528443">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="den" class="java.lang.String">
<fieldDescription><![CDATA[den]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="113" y="2" width="100" height="20" uuid="6b5fe0c5-60a2-4db6-96d6-cb9d065e17ff"/>
<text><![CDATA[den]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="113" y="29" width="100" height="20" uuid="f588ecab-7ede-4e2c-80b9-994efcc45dd8"/>
<textFieldExpression><![CDATA[$F{den}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Thanks in advance. Been stacked on this for quite some time. Hopefully question is understandable.
Apart from some minor adjustments - creating separated package and class for generating collection and initialization of variable den - main problem was in this line:
seznamDnu.add(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd")));}
I was filling the collection with Strings instead of beans. Code changed to:
seznamDnu.add(new ListDays(prvniDenMesice.plusDays(i).format(DateTimeFormatter.ofPattern("EE dd"))));}
And everything works fine. While looking at the code now, I also have to note that I didn't choose bean's name really well.
Hi i'm new to jasper report.i'm using ireport for designing report.
I have created a simple report design, which have 2 fields name,addr.
public class R {
private String name;
private String addr;
public R(String name, String addr) {
super();
this.name = name;
this.addr = addr;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
.jrxml file
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="R" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4fd2f78e-344d-4371-ba1e-f86c2a81ef75">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="296" y="41" width="228" height="20" uuid="19aa39a0-45cf-4b36-b327-25b640f81d8a"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Address]]></text>
</staticText>
<staticText>
<reportElement x="36" y="41" width="215" height="20" uuid="baba8372-2215-439d-8b20-fd4ab054bbe5"/>
<textElement textAlignment="Center"/>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="296" y="0" width="228" height="20" uuid="c4de17e6-e5bc-4454-b38e-cc50acf18f0b"/>
<textFieldExpression><![CDATA[$F{addr}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="36" y="0" width="215" height="20" uuid="8283d8b6-a288-48ec-bd4a-36c2875f5cff"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Here i'm using array list as a data source.
When i click compile button on i report it shows error that
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 1. Field not found : addr 2. Field not found : name
java code
public void runReport(String fileName, String outFileName) throws JRException
{
List<R> list = new ArrayList<R>();
Map parameters = new HashMap();
list.add(new R("a1" ,"a2"));
list.add(new R("b1" ,"b2"));
list.add(new R("c1" ,"c2"));
JasperPrint print = JasperFillManager.fillReport( fileName, parameters, new JRBeanCollectionDataSource(list));
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(
JRExporterParameter.OUTPUT_FILE_NAME,outFileName);
exporter.setParameter(
JRExporterParameter.JASPER_PRINT, print);
JasperExportManager.exportReportToPdfFile(print, outFileName);
print = null;
exporter = null;
}
You should add to your jrxml field:
<field name="addr" class="java.lang.String">
<fieldDescription><![CDATA[addr]]></fieldDescription></field>
You can do it by clicking on "fields" and selecting "create field".
When I tried to generate a report by using JavaBean Data Source and displaying the bean's field values in the table, the PDF report is showing null values.
I have a simple bean (PersonBean) as follows:
public class PersonBean {
private String Field1;
private String Field2;
public String getField1() {
return Field1;
}
public void setField1(String field1) {
Field1 = field1;
}
public String getField2() {
return Field2;
}
public void setField2(String field2) {
Field2 = field2;
}
}
The Person class which populate and return the collection of bean is as follows:
import java.util.ArrayList;
import java.util.List;
public class Person {
public static java.util.List<PersonBean> getReportData() throws Exception {
List<PersonBean> personBeanList = null;
try {
personBeanList = new ArrayList<PersonBean>();
PersonBean personBean1 = new PersonBean();
personBean1.setField1("Hina");
personBean1.setField2("Sachdev");
personBeanList.add(personBean1);
PersonBean personBean2 = new PersonBean();
personBean2.setField1("Swathi");
personBean2.setField2("Singh");
personBeanList.add(personBean2);
} catch (Exception e) {
e.printStackTrace();
}
return personBeanList;
}
}
The PersonReport class which creates report is as follows:
import java.io.FileNotFoundException;
import java.util.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;
public class PersonReport implements JRDataSource {
public static void main(String[] args) throws JRException, FileNotFoundException {
try {
Map<String, Object> params = new HashMap<String, Object>();
JasperDesign jasperDesign = JRXmlLoader.load("C:/Users/sachdevh/Desktop/reports/personReport.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params,
new JRBeanCollectionDataSource(Person.getReportData()));
JasperExportManager.exportReportToPdfFile(jasperPrint, "./Util/bean.pdf");
JasperViewer.viewReport(jasperPrint);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public Object getFieldValue(JRField arg0) throws JRException {
return null;
}
#Override
public boolean next() throws JRException {
return false;
}
}
By using iReport 4.6.0, I designed a report by inserting table component.
The jrxml file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
<subDataset name="New Dataset 1" uuid="50bff6d9-9b98-444b-9851-bdd37ea570e4">
<queryString><![CDATA[]]></queryString>
<field name="field1" class="java.lang.String">
<fieldDescription><![CDATA[field1]]></fieldDescription>
</field>
<field name="field2" class="java.lang.String">
<fieldDescription><![CDATA[field2]]></fieldDescription>
</field>
</subDataset>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\Users\\sachdevh\\Desktop\\reports\\"]]></defaultValueExpression>
</parameter>
<parameter name="parameter1" class="java.lang.String">
<defaultValueExpression><![CDATA[$F{field1}]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="field1" class="java.lang.String">
<fieldDescription><![CDATA[field1]]></fieldDescription>
</field>
<field name="field2" class="java.lang.String">
<fieldDescription><![CDATA[field2]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<componentElement>
<reportElement uuid="b3e3e08d-91d9-4e01-ae74-abdfc270551a" key="table" x="0" y="0" width="555"
height="61"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
<dataSourceExpression>
<![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>
</datasetRun>
<jr:column uuid="9e2a6076-7f96-40c8-86ae-318b521a4f81" width="90">
<jr:tableHeader height="30"/>
<jr:tableFooter height="30"/>
<jr:columnHeader height="30">
<staticText>
<reportElement uuid="99411482-c1d5-44fa-aef7-9d9f4bf392ec" x="0" y="0" width="90"
height="30"/>
<textElement/>
<text><![CDATA[field1]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter height="30"/>
<jr:detailCell height="20">
<textField>
<reportElement uuid="74861d9e-48a8-47ce-a477-81d4f497d483" x="0" y="0" width="90"
height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column uuid="84c3b078-0114-452e-b3a3-f1c7dcd1b8cb" width="90">
<jr:tableHeader height="30"/>
<jr:tableFooter height="30"/>
<jr:columnHeader height="30">
<staticText>
<reportElement uuid="cb87f1a0-bbbf-40b2-bb19-292ecf8d9a18" x="0" y="0" width="90"
height="30"/>
<textElement/>
<text><![CDATA[field2]]></text>
</staticText>
</jr:columnHeader>
<jr:columnFooter height="30"/>
<jr:detailCell height="20">
<textField>
<reportElement uuid="84fd788f-24e8-4dfa-8c83-ce388c663f45" x="0" y="0" width="90"
height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch"/>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
I think the datasource expression below
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>
You can solve this issue in two steps:
Move table component to the Title band (now it lies in Column Header band) - to prevent occurring of net.sf.jasperreports.engine.JRRuntimeException: Infinite loop creating new page due to column header overflow exception in cases when datasource has a lot of elements;
Change the table's subDataset dataSourceExpression to value below, that is relevant to your Java code:
<datasetRun subDataset="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>
I have a problem when I generate a report,the problem is that chart repeats
exp if I have in the X axis 5 elements ,the chart will be repeated 5 times
My report:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="BarChartproject" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<parameter name="SQL" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[$P!{SQL}]]>
</queryString>
<field name="nb" class="java.lang.Long"/>
<field name="priority" class="java.lang.String"/>
<field name="project" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="18" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="178" splitType="Stretch">
<stackedBar3DChart>
<chart>
<reportElement x="70" y="21" width="363" height="132"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<categoryDataset>
<categorySeries>
<seriesExpression><![CDATA[$F{project}]]></seriesExpression>
<categoryExpression><![CDATA[$F{project}]]></categoryExpression>
<valueExpression><![CDATA[$F{nb}]]></valueExpression>
</categorySeries>
</categoryDataset>
<bar3DPlot>
<plot/>
<itemLabel color="#000000" backgroundColor="#FFFFFF"/>
<categoryAxisFormat>
<axisFormat>
<labelFont/>
<tickLabelFont/>
</axisFormat>
</categoryAxisFormat>
<valueAxisFormat>
<axisFormat>
<labelFont/>
<tickLabelFont/>
</axisFormat>
</valueAxisFormat>
</bar3DPlot>
</stackedBar3DChart>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
My code:
public String ConstructSQL()
{
System.out.println("status------"+this.getMyChoiceStatus());
System.out.println("chartssssssssss------"+this.getChartType());
for(int i=0;i<checkbox.length;i++)
{
if (checkbox[i].equals("1"))
{
System.out.println("priority checked");
select ="select count(jiraissue.id) as nb ,jiraissue.priority,jiraissue.project,jiraissue.issuestatus";
from =" from jiraissue,issuestatus";
where =" where issuestatus.id=jiraissue.issuestatus";
and =" and issuestatus.pname ="+"'"+this.getMyChoiceStatus()+"'";
groupBy=" group by jiraissue.priority";
sql =select+from+where+and+groupBy+" ;";
System.out.println("SQL report------"+this.sql);
return sql;
}
else
{
if (checkbox[i].equals("2"))
{
System.out.println("project checked");
select ="select count(jiraissue.id) as nb ,jiraissue.project,jiraissue.priority,jiraissue.issuestatus";
from =" from jiraissue,issuestatus";
where =" where issuestatus.id=jiraissue.issuestatus";
and =" and issuestatus.pname ="+"'"+this.getMyChoiceStatus()+"'";
groupBy=" group by jiraissue.project";
sql =select+from+where+and+groupBy+" ;";
System.out.println("SQL report------"+this.sql);
return sql;
}
}
}
return sql;
}
public void fillReport()
{
try {
// - Connexion à la base
Driver monDriver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(monDriver);
connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jiradb", "","");
// - Chargement et compilation du rapport
JasperDesign jasperDesign = JRXmlLoader.load("C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/"+chartType+chartGrouping()+".jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
Map parameterMap = new HashMap();
parameterMap.put("SQL",ConstructSQL());
// // - Execution du rapport
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameterMap, connection);
// - Création du rapport au format PDF
JasperExportManager.exportReportToPdfFile(jasperPrint, "C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/"+chartType+chartGrouping()+".pdf");
// JasperViewer.viewReport(jasperPrint);
}
I think the issue here (which you have most probably discovered already as this question was aksed 7 months ago) is that you have placed the chart into the 'Detail' area. You will have the detail area repeated for each element returned (i.e. row of data in a query).
You should place the chart into the 'Title' area, or one of the other 'non-repeating' areas on a page.