JasperReport

package test.report;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
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.util.JRLoader;

public class GenerateReport {
public static void main(String[] args) {
try {
sql4Report();
} catch (JRException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
System.out.print( "finish" );
}
}

public static void parameter4Report ()
throws SQLException, JRException {
//report parameter
HashMap<String, Object> parameter =
new HashMap<String, Object>();
parameter.put( "master_location_id", new BigDecimal(22) );

Connection con = getConnection();
JasperReport jasperReport =
JasperCompileManager.compileReport("reports/Location.jrxml");
JasperPrint jasperPrint =
JasperFillManager.fillReport( jasperReport, parameter, con );

JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/location_parameter4Report.pdf");
}

public static void sql4Report () throws SQLException, JRException {
//create the ResultSet
Connection con = getConnection();
PreparedStatement statement =
con.prepareStatement(
"select * from loc_location "
+ "where location_type_cd = 'ROOM' "
+ "order by location_type_cd"
);
ResultSet resultSet = statement.executeQuery();

JRResultSetDataSource result =
new JRResultSetDataSource( resultSet );
JasperReport jasperReport =
JasperCompileManager.compileReport("reports/Location.jrxml");
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport, new HashMap(), result
);

JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/location_sql4Report.pdf"
);
}

public static void jasper4Report ()
throws SQLException, JRException {
//create the ResultSet
Connection con = getConnection();
PreparedStatement statement =
con.prepareStatement(
"select * from loc_location "
+ "where location_type_cd = 'ROOM' "
+ "order by location_type_cd"
);
ResultSet resultSet = statement.executeQuery();

JRResultSetDataSource result =
new JRResultSetDataSource( resultSet );
//modify
JasperReport jasperReport =
(JasperReport) JRLoader.loadObject(
"reports/Location.jasper"
);
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport, new HashMap(), result
);

JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/location_jasper4Report.pdf"
);
}

public static void xls4Report () throws SQLException, JRException {
// create the ResultSet
Connection con = getConnection();
PreparedStatement statement =
con.prepareStatement(
"select * from loc_location "
+ "where location_type_cd = 'ROOM' "
+ "order by location_type_cd"
);
ResultSet resultSet = statement.executeQuery();


JRResultSetDataSource result =
new JRResultSetDataSource( resultSet );
// create JasperReport from .jasper
JasperReport jasperReport =
(JasperReport) JRLoader.loadObject("reports/Location.jasper");
JasperPrint jasperPrint =
JasperFillManager.fillReport( jasperReport, new HashMap(), result );

// JasperExportManager.exportReportToPdfFile( jasperPrint, "reports/location_xls4Report.xls");

JRXlsExporter xlsExporter = new JRXlsExporter();
xlsExporter.setParameter( JRExporterParameter.JASPER_PRINT, jasperPrint );
xlsExporter.setParameter( JRExporterParameter.OUTPUT_FILE, new File("reports/location_xls4Report.xls") );
xlsExporter.exportReport();
}

private static Connection getConnection () throws SQLException {
DriverManager.registerDriver(
new oracle.jdbc.driver.OracleDriver()
);
return DriverManager.getConnection( url, user, pw );
}

private static String url = "jdbc:oracle:thin:@localhost:1521:db";
private static String user = "report";
private static String pw = "report";
}


需要的包:
jasperreports-3.1.2.jar
jasperreports-3.1.2-applet.jar
jasperreports-3.1.2-javaflow.jar
commons-collections.jar
commons-digester.jar
commons-logging-1.0.4.jar
commons-logging-api.jar
commons-beanutils.jar
itext-1.3.1.jar
classes12.jar
poi-3.0.1.jar
poi-contrib-3.0.1-FINAL-20070705.jar
poi-scratchpad-3.0.1-FINAL-20070705.jar

文件结构:
+test
++report
+++GenerateReport.java

+reports
++Location.jrxml
++Location.jasper

数据表要和Location.jrmal或Location.jasper(即report的设计一致就行了)

另外可以参考一下这里:
http://blog.csdn.net/kabini/archive/2007/05/08/1600098.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值