- 浏览: 185395 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
masuweng:
好好
与ant结合使用yguard -
lenomon:
你也可以看看这个,Linux同步时间命令ntpdate
Linux与远程时间服务器同步 -
木叶之舟:
我这里有个疑问,就是我想通过拦截外键的属性,该怎样实现呢?
比 ...
springside的filter查询 -
dongbiying:
好!
与ant结合使用yguard -
javaray:
遨豪(大连)科技有限公司自2007年与Liferay,Inc合 ...
Liferay的CAS SSO实现思路
这几天多在用jasperreports写下一点入门的东西。这里就不讲在ireport里连接jdbc了,不切实际。单说在应用里用javabean提供数据实现报表展现。
不讲别的就贴代码了。具体的操作在代理注释。下面有附用ireport图形界面连接jdbc生成报表。要添加的jar也附上。我开始练手的时候找的要死,因为多是以前的版本。配用jar很难找。还有介绍个根据包名查找具体jar的网站,应该很多人多指导了www.findjar.com,但是好像也有好些超找不到。
以下是文本报表
javabean:
package lld.test.ireport.zhj; public class Student { private String name; private String address; private int age; private String sex; public Student(){} public Student(String name, String address, int age, String sex) { super(); this.name = name; this.address = address; this.age = age; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
makeData:
package lld.test.ireport.zhj; import java.util.ArrayList; /** * 数据制造类 * @author Administrator * @param <T> */ public class MakeData<T> { private ArrayList<T> listData = new ArrayList<T>(); public ArrayList<T> getData() { return listData; } public void addData(T t) { listData.add(t); } }
servlet:
package lld.test.ireport.zhj; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRExporter; import net.sf.jasperreports.engine.JRExporterParameter; 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.export.JRHtmlExporter; import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.export.JRRtfExporter; import net.sf.jasperreports.engine.export.JRXlsAbstractExporter; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.j2ee.servlets.ImageServlet; //javabean数据源 单纯报表 public class ZhjBeanRepServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int reportType = Integer.parseInt(req.getParameter("reportType")); MakeData makeData = new MakeData(); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("王冠峰", "河南", 22, "男")); makeData.addData(new Student("小钱", "安徽", 22, "男")); makeData.addData(new Student("小胡", "江西", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); makeData.addData(new Student("赵汉江", "湖南", 22, "男")); ArrayList<Student> listStudent = makeData.getData(); String reportFilePath = this.getServletContext().getRealPath("/").replace('\\', '/') + "WEB-INF/classes/lld/test/ireport/zhj/zhjtest.jasper"; File file = new File(reportFilePath); if (!file.exists()) { try { throw new Exception("jasper文件不存在:" + reportFilePath); } catch (Exception e) { e.printStackTrace(); } } JRDataSource dataSource = new JRBeanCollectionDataSource(listStudent); switch (reportType) { case ReportDefine.REPORT_PDF: try { exportPdf(req, resp, reportFilePath, dataSource); } catch (Exception e1) { e1.printStackTrace(); } break; case ReportDefine.REPORT_EXCEL: try { exportExcel(req, resp, reportFilePath, dataSource); } catch (Exception e1) { e1.printStackTrace(); } break; case ReportDefine.REPORT_HTML: try { exportHtml(req, resp, reportFilePath, dataSource); } catch (Exception e1) { e1.printStackTrace(); } break; case ReportDefine.REPORT_DOC: try { exportDoc(req, resp, reportFilePath, dataSource); } catch (Exception e1) { e1.printStackTrace(); } break; default: try { throw new Exception("报表输出类别错误:" + reportType); } catch (Exception e) { e.printStackTrace(); } } } private JasperPrint getJasperPrint(String reportFilePath, JRDataSource dataSource) throws Exception { JasperReport report = (JasperReport) JRLoader.loadObject(reportFilePath); JasperPrint print = JasperFillManager.fillReport(report, null, dataSource); return print; } protected void exportPdf(HttpServletRequest req, HttpServletResponse resp, String reportFilePath, JRDataSource dataSource) throws Exception { System.out.println("export PDF report by JasperPrint..."); JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource); // OutputStream ouputStream = resp.getOutputStream(); resp.setContentType("application/pdf"); resp.setCharacterEncoding("UTF-8"); resp.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("PDF报表", "UTF-8") + ".pdf\""); // //使用JRPdfExproter导出器导出pdf // JRPdfExporter exporter = new JRPdfExporter(); // exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); // exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); // exporter.exportReport(); // ouputStream.close(); // System.out.println("export PDF report by JasperRunManager..."); // OutputStream outputStream = resp.getOutputStream(); // resp.setContentType("application/pdf"); // resp.setCharacterEncoding("UTF-8"); // resp.setHeader("Content-Disposition", "attachment; filename=\"" // + URLEncoder.encode("PDF报表", "UTF-8") + ".pdf\""); // byte[] bytes = JasperRunManager.runReportToPdf(reportFilePath, null, // dataSource); // outputStream.write(bytes, 0, bytes.length); // outputStream.close(); // 生成我们的导出类JRPdfExporter 来自JRExporter JRPdfExporter jrpdfExporter = new JRPdfExporter(); jrpdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); jrpdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM,resp.getOutputStream()); jrpdfExporter.exportReport(); } protected void exportExcel(HttpServletRequest req, HttpServletResponse resp, String reportFilePath, JRDataSource dataSource) throws Exception { System.out.println("export Excel report..."); JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource); JRXlsAbstractExporter exporter = new JRXlsExporter(); resp.setContentType("application/xls;charset=utf-8"); resp.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("Excel报表", "UTF-8") + ".xls\""); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resp.getOutputStream()); exporter.exportReport(); } protected void exportHtml(HttpServletRequest req, HttpServletResponse resp, String reportFilePath, JRDataSource dataSource) throws Exception { System.out.println("export HTML report..."); resp.setCharacterEncoding("UTF-8"); PrintWriter out = resp.getWriter(); resp.setContentType("text/html;charset=UTF-8"); JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource); JRHtmlExporter exporter = new JRHtmlExporter(); req.getSession().setAttribute( ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); exporter.exportReport(); } protected void exportDoc(HttpServletRequest req, HttpServletResponse resp, String reportFilePath, JRDataSource dataSource) throws Exception { JasperPrint jasperPrint = this.getJasperPrint(reportFilePath, dataSource); JRExporter exporter = new JRRtfExporter(); resp.setContentType("application/msword;charset=utf-8"); resp.setHeader("Content-Disposition", "attachment; filename=" + "doc报表.doc"); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resp.getOutputStream()); exporter.exportReport(); } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>ireport_test</display-name> <servlet> <servlet-name>ZhjReport</servlet-name> <servlet-class>lld.test.ireport.zhj.ZhjBeanRepServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ZhjReport</servlet-name> <url-pattern>/zhj_rep.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
jsp:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <ul> <li>JavaBean数据源报表 <ul> <li><a href="zhj_rep.do?reportType=1">Pdf格式</a></li> <li><a href="zhj_rep.do?reportType=2">Excel格式</a></li> <li><a href="zhj_rep.do?reportType=3">HTML格式</a></li> <li><a href="zhj_rep.do?reportType=4">DOC格式</a></li> </ul> </li> </ul> </body> </html>
以下是饼状图
servet:
package lld.test.ireport.zhj; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRExporterParameter; 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.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; public class ZhjChartRepServlet extends HttpServlet { private static final long serialVersionUID = -7332836030597016040L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { String root_path = this.getServletContext().getRealPath("/"); root_path = root_path.replace('\\', '/'); String reportFilePath = root_path + "WEB-INF/classes/lld/test/ireport/zhj/zhjchart.jasper"; MakeData makeData = new MakeData(); makeData.addData(new Language("java", 22));发表评论
-
用exp/imp远程操作数据库
2012-03-27 10:55 1179在http://icyheart.iteye.com/bl ... -
Eclipse for JEE 3.5.2 maven插件安装
2012-03-27 10:56 2592先安装gef插件,然后安装subclipse插件,最后安装m2 ... -
onkeypress,onkeydown,onkeyup区别 和 javascript event.keycode值大全
2010-08-05 17:32 4905onkeypress是在用户按下并放开任何字母数字键时发生 ... -
国王和100个囚犯
2010-01-15 11:27 921看到一个关于算法的帖子,总结了下跟帖的算法发表一下: 原贴地 ... -
搞懂java中的synchronized关键字
2009-12-12 17:55 18441、synchronized关键字的作用域有二种: 1 ... -
JAVA 30个基本知识
2009-12-12 17:53 8561.OOP中唯一关系的是对 ... -
精妙的SQL語句
2009-12-12 17:51 962说明:复制表(只复制结构,源表名:a 新表名:b) SQL: ... -
JPA & Hibernate 注解
2009-11-25 18:05 1185原文地址:http://blog.csdn.net/eastl ... -
Servlet监听器详解
2009-11-11 16:56 1250原文地址:http://www.21jn.net/html/8 ... -
Servlet过滤器介绍之原理分析
2009-11-11 16:31 1360原文地址:http://zhangjunhd.blog.51c ... -
web.xml 中的listener、 filter、servlet 加载顺序及其详解
2009-11-11 14:46 1079原文地址:http://sunxboy.iteye.com/b ... -
jsp常见问题
2009-11-11 14:14 1423原文地址:http://liujinpan75.iteye.c ... -
OGNL表达式语言介绍
2009-11-09 18:08 1990原文地址:http://darkranger.iteye.co ... -
OGNL的用法
2009-11-09 17:46 1477原文地址:http://www.blogjava.net/ma ... -
jmail收发邮件
2009-10-28 17:09 1714jmail收发邮件,发送邮件基于smtp协议,收邮件基于pop ... -
程序员每天该做的8件事
2009-09-23 15:10 904不重视细节,如何谈得上成功? 1)程序员每天总结自己一天 ... -
ant使用
2009-08-07 15:16 1177参考地址: http://www.blogjava. ... -
Alpha、Beta、RC、GA版本的区别
2009-08-05 11:08 1400Alpha、Beta、RC、GA版本的区别 ... -
基本排序
2009-07-15 12:20 899package zyio; public class Tes ... -
在TOMCAT下配置工程的默认访问
2009-07-10 14:09 3281对工程的部署一般是将工程的压缩文件放在tomcat安装目录的w ...
相关推荐
开发者可以使用iReport或Jaspersoft Studio等设计工具来创建JasperDesign文件。 2. **JasperReport**: 报表编译后生成的文件,包含了运行时需要的所有信息,如样式、字段、图像、表格等。它是JasperDesign编译后的...
JasperReports 3.6.1 Java报表
总的来说,"PDF模版设计工具iReport1.3.3+iReport5.6.0+Jaspersoft Studio-6.18.1"的组合为Java开发者提供了一个强大且灵活的报表设计环境,能够满足各种复杂报表的需求。无论是简单的数据展示还是复杂的多页报告,...
将JDK 1.4与iReport 1.3.3结合使用,开发者可以充分利用这两个工具的优势来构建企业级的报表系统。在那个时候,JDK 1.4的稳定性和性能能够支持iReport的运行,而iReport 1.3.3则提供了丰富的报表设计功能,使得非...
三、iReport的版本与使用: 你提到的文件“iReport-1.3.3”是iReport的一个较早版本。在早期版本中,用户界面可能相对简单,但基本功能已经相当完善。随着版本的更新,iReport逐渐增加了更多高级特性,如更复杂的...
- jasperreports-1.3.3.jar - jdt-compiler-3.1.1.jar - jxl-2.6.jar - png-encoder-1.5.jar - poi-2.0-final-20040126.jar 这些库文件提供了JasperReport运行所需的各种功能支持。 #### 四、总结 本文详细介绍了...
在`SpringMVC`中,我们可以使用`JasperReports`库来处理`ireport`设计的报表模板。首先,需要在项目中引入`jasperreports`依赖,并配置相应的数据源。`SpringMVC`的控制器负责接收请求,根据参数生成报表,最后将其...