一。安装ireport,到iReport的官方网站:http://ireport.sourceforge.net/去下载。
二。安装完后制作report模板文件:
1.新建一个文件:档案-->开启新档
2.配置数据源:data-->连接资源来源-->new-->DataBase JDBC connection,输入名称,选择驱动,填写url.
3.插入数据,选择data-->报表查询,在这里写要查询的sql,写好后会在field里自动生成查后的列;
4.拖动field到detail里。这样就制作完成了。
5.建立--->编译会在ireport的安装目录下生成.jasper结尾的文件。这就是我们需要的模板文件。
很简单的一个报表例子。
三。写jsp文件: (偶用的是IREPORT300,包请在IREPORT300中lib里找吧)
1。导入需要的降包:itext-1.3.1.jar,iTextAsian.jar,jasperreports-1.0.1.jar,poi-2.0-final-20040126.jar,classes12.jar。注意:不要是classes12.zip,我导入classes12.zip就是报找不到OracleDriver错误。
2.写jsp文件:
- <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
- <%@ page import="java.sql.*"%>
- <%@ page import="java.io.*"%>
- <%@ page import="net.sf.jasperreports.engine.JasperFillManager"%>
- <%@ page import="net.sf.jasperreports.engine.JasperPrint"%>
- <%@ page import="net.sf.jasperreports.engine.JRException"%>
- <%@ page import="net.sf.jasperreports.engine.JRExporterParameter"%>
- <%@ page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%>
- <%@ page import="net.sf.jasperreports.engine.export.JRXlsExporter"%>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <body>
-
- <%
- String Name = "aa";
- Class.forName("oracle.jdbc.driver.OracleDriver");
- Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","lshh","lshh");
-
- File exe_rpt = new File(application.getRealPath("/test.jasper"));
-
- Map parameters = new HashMap();
- parameters.put("Name", Name);
- try {
-
- JasperPrint jasperPrint = JasperFillManager.fillReport(exe_rpt
- .getPath(), parameters, conn);
-
- ByteArrayOutputStream oStream = new ByteArrayOutputStream();
- JRXlsExporter exporter = new JRXlsExporter();
- exporter.setParameter(JRExporterParameter.JASPER_PRINT,
- jasperPrint);
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
- oStream);
- exporter.setParameter(
- JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
- Boolean.TRUE);
- exporter.setParameter(
- JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
- Boolean.FALSE);
- exporter.setParameter(
- JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
- Boolean.FALSE);
- exporter.exportReport();
-
- byte[] bytes = oStream.toByteArray();
-
- if (bytes != null && bytes.length > 0) {
- response.reset();
- response.setContentType("application/vnd.ms-excel");
- response.setContentLength(bytes.length);
- ServletOutputStream ouputStream = response
- .getOutputStream();
- ouputStream.write(bytes, 0, bytes.length);
- ouputStream.flush();
- ouputStream.close();
-
- } else {
- out.print("bytes were null!");
- }
- conn.close();
- } catch (JRException ex) {
- out.print("Jasper Output Error:" + ex.getMessage());
- }
- %>
-
-
- </body>
- </html>
强烈强调一句,如果你要在线显示PDF,excel直接把response.setHeader("Content-disposition", "attachment; filename=" + fileName);
改为:response.setHeader("Content-disposition","inline; filename=MyExcel.pdf"); 就行了,
再强调一点,首先确定一定要安装adobe reader阅读器啊(别说,我就因为没装这个,折腾了二天,呵呵。。。)
升级处理:
需要包:iText-2.1.7.jar、iTextAsian.jar、jasperreports-5.5.0.jar、poi-3.9-20121203.jar