我正在尝试传递一个项目列表,它被转换为“JRBeanCollectionDataSource”到iReport中。以下是我的java代码:
主要课程:
package javaapplication1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
public class JavaApplication1 {
public static void main(String[] args)
{
try {
/* User home directory location */
String userHomeDirectory = System.getProperty("user.home");
/* Output file location */
String outputFile = userHomeDirectory + File.separatorChar + "JasperTableExample.pdf";
/* List to hold Items */
List<Item> listItems = new ArrayList<Item>();
/* Create Items */
Item iPhone = new Item();
iPhone.setName("iPhone 6S");
iPhone.setPrice(65000.00);
Item iPad = new Item();
iPad.setName("iPad Pro");
iPad.setPrice(70000.00);
/* Add Items to List */
listItems.add(iPhone);
listItems.add(iPad);
/* Convert List to JRBeanCollectionDataSource */
JRBeanCollectionDataSource itemsJRBean = new JRBeanCollectionDataSource(listItems);
JRDataSource dataSource = new JRBeanCollectionDataSource(listItems);
/* Map to hold Jasper report Parameters */
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("ds1", itemsJRBean);
/* Using compiled version(.jasper) of Jasper report to generate PDF */
/* Using compiled version(.jasper) of Jasper report to generate PDF */
//JasperPrint jasperPrint = JasperFillManager.fillReport("resources/newReport.jasper", parameters, new JREmptyDataSource());
JasperPrint jasperPrint = JasperFillManager.fillReport("C:\\Users\\UPUANNGHARI\\Desktop\\report2.jasper", parameters, new JREmptyDataSource());
/* outputStream to create PDF */
OutputStream outputStream = new FileOutputStream(new File(outputFile));
/* Write content to PDF file */
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
System.out.println("File Generated");
} catch (JRException ex) {
ex.printStackTrace();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
Item.java: package javaapplication1;
public class Item {
private String name;
private Double price;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Double getPrice()
{
return price;
}
public void setPrice(Double price)
{
this.price = price;
}
}
JRXML:
<?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="report2" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="46d70a50-8fb3-4c45-afa0-3cc939ec618a">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="ds1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" isForPrompting="false"/>
<field name="name" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="102" y="32" width="100" height="20" uuid="0bfa36df-afdc-4991-aa40-bc9c411ba90c"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</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>
在我的iReport中,我只需添加一个参数,将其命名为“ds1”,并将其参数类更改为“net.sf.jasperreports.engine.data.JRBeanCollectionDataSource”。然后我放置一个textField并将其表达式更改为“$ F {name}”。在这里,当我运行它时,我得到一个空值。我认为我在配置iReport方面做得不够。显然我不知道怎么做一个完整的连接。请帮我解决一下我应该解决的问题。提前谢谢。