Posted on 2007-08-03 17:08
kooyee 阅读(1419)
评论(0) 编辑 收藏 所属分类:
Jasper Report
eclipse,new 一個project,把所有ireport 的 libraries 都加進去。copy simple.jrxml (ireport生成的报表文件)到project 的folder裏。
在project裏建立一個Simple.java, 在main裏鍵入:
JasperDesign jasperDesign = JasperManager.loadXmlDesign("Simple.jrxml");
JasperReport jasperReport = JasperManager.compileReport(jasperDesign);
Connection conn = Database.getConnection(); //隨便起一個空的database connection
Map parameters = new HashMap();
parameters.put("NAME", "CSJDN");
JasperPrint jasperPrint = JasperManager.fillReport(jasperReport,parameters, conn);
JasperManager.printReportToPdfFile(jasperPrint, "BasicReport.pdf");
JasperViewer.viewReport(jasperPrint);
如何把做好的报表(jasper文件)引入到应用程序中?
此时我们就要引入一个jsp页面,假设是pdf.jsp,那么下面就是pdf.jsp的代码:
<%@ page import="dori.jasper.engine.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%
File reportFile = new File(application.getRealPath("/reports/test.jasper"));
Map parameters = new HashMap();
parameters.put("name", "p");
Connection conn=null
//以下忽略得到数据库连接过程
//……
//……
byte[] bytes =
JasperRunManager.runReportToPdf(
reportFile.getPath(),
parameters,
conn
);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
%>
还有一个必要的步骤就是把你下载的Jasperreport.jar放在你应用程序的lib下,