转自:http://hi.baidu.com/nieweiguo/blog/item/b92e369bf0c882b6c8eaf476.html
步骤如下:
首先在ireprot里做好所需要报表,保存 好
ireport里面的字段就是你要传入到jasper里的list所封装的model的字段,一一对应即可,
在ireport里面要设置报表字段(如果要传参数到报表里,则要在ireport设置报表参数)
,做好报表后,就在后台ACTION(自己可以在JSP,SERVLET 封装等...)封装数据了,我做了个测试片段如下:
List list=new ArrayList();
for(int i=0;i<50;i++)
{
Account accout =new Account();
accout.setId(new Integer(i));
accout.setEmail("邮箱youxiang"+i);
accout.setLogin("登录名denglu"+i);
accout.setName("姓名xingm"+i);
accout.setPassword("密码mima"+i);
accout.setType("类型leixing"+i);
list.add(accout);
}
request.setAttribute("list",list);
request.setAttribute("test", "anie测试test");
return mapping.findForward("baobiao");
ACCOUNT MODEL就是你要显示在报表中的类型
接着就转发到baobiao.jsp页面了,此页面的代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.util.List"%>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.List"%>
<%
//获取过滤信息
String outputType = (String)request.getAttribute("outputType");
List list=(List)request.getAttribute("list");
String test = (String)request.getAttribute("test");
%>
<%
//下面两句 意思是ireport的报表XML文件在程序中动态编译成.jasper文件。注释掉就是指定了.jasper文件
//String path=application.getRealPath("/file");
//JasperCompileManager.compileReportToFile(path+"/anietest.jrxml",path+"/anietest.jasper");
File reportFile;
reportFile= new File(application.getRealPath("/file/anietest.jasper"));
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
Map parameters = new HashMap();
parameters.put("test",test);
//为数据源设置参数
//outputType="EXCEL";
if("HTML".equals(outputType))
{
System.out.println(outputType);
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
new JRBeanCollectionDataSource(list)
);
JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE) ;
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE) ;
exporter.exportReport();
}
else if("PDF".equals(outputType))
{
System.out.println(outputType);
byte[] bytes =
JasperRunManager.runReportToPdf(
reportFile.getPath(),
parameters,
new JRBeanCollectionDataSource(list)
);
String fileName = "阿聂TEST.pdf";
fileName=java.net.URLEncoder.encode(fileName,"utf-8");
response.setContentType("application/download");
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
//PDF 跟EXCEL需要此句,要不然会报错 HTML不需要
out.clear();
out=pageContext.pushBody();
}
else if("EXCEL".equals(outputType)){
System.out.println(outputType);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reportFile.getPath(),
parameters,
new JRBeanCollectionDataSource(list)
);
response.setContentType("application/vnd.ms-excel");
JRXlsExporter exporter = new JRXlsExporter();
byte[] bytes;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport();
bytes = baos.toByteArray();
if (bytes != null && bytes.length > 0) {
response.reset();
String fileName = "阿聂TEST.xls";
fileName=java.net.URLEncoder.encode(fileName,"utf-8");
response.setContentType("application/download");
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
//PDF 跟EXCEL需要此句,要不然会报错 HTML不需要
out.clear();
out=pageContext.pushBody();
}
}
%>
里面有写些需要改写地方,请慢慢参考吧,很简单,其中
Map parameters = new HashMap();
parameters.put("test",test); 这句是要设置在报表里的参数,通过放入MAP中然后
传给jasper报表输出 代码如下:JasperPrint jasperPrint = JasperFillManager.fillReport(
reportFile.getPath(),
parameters,
new JRBeanCollectionDataSource(list)
);
基本主要代码就是以上了,然后说一下需要的包,基本就是只需要这几个包就行了。测试成功,饼形报表跟柱形
报表慢慢研究中