Use jasper reports library
<pre name="code" class="java">
DataBean.java
public class DataBean {
private String name;
private String occupation;
private String place;
private String country;
//setters and getters
}
DataBeanMaker.java
package com.report.javabean;
import java.util.ArrayList;
public class DataBeanMaker {
public ArrayList<DataBean> getDataBeanList() {
ArrayList<DataBean> dataBeanList = new ArrayList<DataBean>();
dataBeanList.add(produce("umar farooq", "Manager", "karnool", "india"));
dataBeanList.add(produce("mohan", "SSE", "vizag", "india"));
dataBeanList.add(produce("Srikanth tirumala", "CEO", "hyderabad", "India"));
dataBeanList.add(produce("ruby", "Co-Founder", "karnool", "India"));
dataBeanList.add(produce("subbu", "SSE" , "nelure", "india"));
dataBeanList.add(produce("mustak", "SE" , "karnool", "india"));
dataBeanList.add(produce("satya", "SE", "nalgonda", "india"));
dataBeanList.add(produce("kiran", "SSE" , "east", "india"));
dataBeanList.add(produce("sujatha", "HR" , "hyderabad", "india"));
return dataBeanList;
}
private DataBean produce(String name, String occupation, String place, String country) {
DataBean dataBean = new DataBean();
dataBean.setName(name);
dataBean.setOccupation(occupation);
dataBean.setPlace(place);
dataBean.setCountry(country);
return dataBean;
}
}
Repoter.java
package com.report.javabean;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
public class Reporter {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream ("reports/test_jasper.xml");
DataBeanMaker dataBeanMaker = new DataBeanMaker();
ArrayList<DataBean> dataBeanList = dataBeanMaker.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataBeanList);
Map parameters = new HashMap();
JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "D:/demo.pdf");
}
}
test_jasper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.1.0.final using JasperReports Library version 6.1.0 -->
<!-- 2015-06-17T10:42:17 -->
<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="scriptlet" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="30" rightMargin="30" topMargin="30" bottomMargin="30" whenResourceMissingType="Empty" uuid="4a933c9b-78d7-426f-8fd8-e27159b11caa">
<property name="com.jasperassistant.designer.Grid" value="false"/>
<property name="com.jasperassistant.designer.SnapToGrid" value="false"/>
<property name="com.jasperassistant.designer.GridWidth" value="12"/>
<property name="com.jasperassistant.designer.GridHeight" value="12"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="country" class="java.lang.String">
<fieldDescription><![CDATA[country]]></fieldDescription>
</field>
<field name="occupation" class="java.lang.String">
<fieldDescription><![CDATA[occupation]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="place" class="java.lang.String">
<fieldDescription><![CDATA[place]]></fieldDescription>
</field>
<group name="dummy">
<groupExpression><![CDATA["dummy"]]></groupExpression>
<groupHeader>
<band height="23">
<staticText>
<reportElement mode="Opaque" x="0" y="5" width="535" height="15" backcolor="#70A9A9" uuid="d6b37903-6581-4be2-acd8-a34e03d4a65f"/>
<box>
<bottomPen lineWidth="5.0" lineColor="#CCCCCC"/>
</box>
<text><![CDATA[]]></text>
</staticText>
<staticText>
<reportElement x="136" y="5" width="150" height="15" uuid="bc77d454-a4ad-4acd-a45c-7781a1cc9db3"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Occupation]]></text>
</staticText>
<staticText>
<reportElement x="286" y="5" width="128" height="15" uuid="9a0b1647-7c70-46d2-9904-b6c8ec2516bc"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<text><![CDATA[Place]]></text>
</staticText>
<staticText>
<reportElement x="414" y="5" width="121" height="15" uuid="1d3dc13e-6844-4551-87c4-a84942396de6"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Country]]></text>
</staticText>
<staticText>
<reportElement x="0" y="5" width="136" height="15" uuid="35ea54b3-46ba-4a00-b209-0f0caae22199"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</groupHeader>
</group>
<detail>
<band height="16">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="535" height="14" backcolor="#E5ECF9" uuid="2b45dbc9-215c-453e-b263-22419173a2a6"/>
<box>
<bottomPen lineWidth="0.25" lineColor="#CCCCCC"/>
</box>
<text><![CDATA[]]></text>
</staticText>
<textField>
<reportElement x="136" y="0" width="150" height="15" uuid="2321034b-fef7-4c07-9605-0a40e48a0080"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{occupation}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="286" y="0" width="128" height="15" uuid="472e0b06-bb32-458f-8073-a637449a060f"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{place}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="414" y="0" width="121" height="15" uuid="6de7ba67-0d5c-48bb-9817-6390fc7a9e13"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="9"/>
</textElement>
<textFieldExpression><![CDATA[$F{country}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="136" height="15" uuid="d592ad48-a319-4939-860f-733d28e312a2"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
</pre>
great
ReplyDelete