转自:https://wenku.baidu.com/view/104156f9770bf78a65295462.html
1.
2.导入jar包
导入G:\iReport-5.6.0\ireport\modules\ext下的jar包
3.在webContent下创建report包,并导入报表工具设计好的sample.jrxml和sample.jasper
4.创建HTML格式的报表jsp文件 testhtml.jsp文件访问WebContent下
1 <%@page import="net.sf.jasperreports.engine.JasperRunManager"%> 2 <%@page import="java.sql.DriverManager"%> 3 <%@page import="java.sql.Connection"%> 4 <%@page import="java.util.Map"%> 5 <%@page import="java.io.File"%> 6 <%@page import="java.util.HashMap"%> 7 <%@ page language="java" contentType="text/html; charset=UTF-8" 8 pageEncoding="UTF-8"%> 9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 10 <html> 11 <head> 12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 13 <title>Insert title here</title> 14 </head> 15 <body> 16 <% 17 String path = this.getServletConfig().getServletContext() 18 .getRealPath("/report/sample.jasper"); 19 File reportFile = new File(path); 20 String url = "jdbc:oracle:thin:@192.168.100.9:1521:Alpha"; //数据库连接URL 21 Class.forName("oracle.jdbc.driver.OracleDriver");//数据库连接驱动 22 Map parameters = new HashMap(); //"SQLSTR"是报表中定义的参数名称,其类型为String 型 //设置SQLSTR参数的内容,根据需要赋值sql语句 23 parameters.put("SQLSTR", "select * from employee");//填充数据 24 Connection conn = DriverManager.getConnection(url, "MLP_BONDED", 25 "password"); 26 JasperRunManager.runReportToHtmlFile(reportFile.getPath(), 27 parameters, conn); 28 response.sendRedirect("report/sample.html"); 29 %> 30 </body> 31 </html>
6.
1. 定位jasper文件
2. 给报表模板中使用到的参数SQLSTR赋值,这里指定一个sql语句
3. 采用JDBC方式连结数据库
4. 生成HTML文件,参数分别为报表文件模板物理位置,报表参数,数据库连结
5. 转向到此页面,这个页面是固定的,需要和报表模板的位置和路径相同
发布项目后预览,输入http://localhost:8080/iReportTest/testhtml.jsp
(本例采用tomcat作为web服务器,发布过程参考其他文档,数 据库采用的mysql)
4.1
开发工具
开发工具使用
eclispe3.2.2
和
MyEclipse5.0
搭配开发。环境配置参考另外的手册。
4.2
创建项目
打开
Eclipse
,在工具栏中,
“
F
ile”
-
>“
N
ew”
-
>“
O
ther”