allen
专注于java ee技术,包括struts,jsf,webwork,spring,hibernate,ibatis
posts - 7,  comments - 9,  trackbacks - 0

jasperreport中可以使用List作为数据源,使用格式如下.
List list=this.customerDao.getAllCustomer();  //得到所有客户
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);
   JasperPrint jasperPrint = JasperFillManager.fillReport(
     reportFilePath, parameters, ds);
得填充数据后,即可输出显示到PDF,Excel,Html
到PDF:
public byte[] generatePDF(String begCustNo, String endCustNo,
   String reportTitle, String reportFilePath) throws DemoException {
  // TODO Auto-generated method stub
 //begCustNo,endCustNo分别为查询传入的开始编号,结束编号.
  jdbcCustomerDao = new JdbcCustomerDao();
  Map parameters = new HashMap();
  parameters.put("ReportTitle", reportTitle);//报表标题
  List list = jdbcCustomerDao.getAllCustomer(begCustNo, endCustNo);
   try {
   JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);
   JasperPrint jasperPrint = JasperFillManager.fillReport(
     reportFilePath, parameters, ds);   return JasperExportManager.exportReportToPdf(jasperPrint);
  } catch (JRException e) {
   throw new DemoException("Report Export Failed.");
  }
 }
到Html:
public byte[] generateHtml(String begCustNo, String endCustNo,
   String reportTitle, String reportFilePath) throws DemoException {
  jdbcCustomerDao = new JdbcCustomerDao();
  Map parameters = new HashMap();
  parameters.put("ReportTitle", reportTitle);
  List list = jdbcCustomerDao.getAllCustomer(begCustNo, endCustNo);
  System.out.println("list size is :" + list.size());
  JRHtmlExporter exporter = new JRHtmlExporter();
  ByteArrayOutputStream oStream = new ByteArrayOutputStream();
  try {
   JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);
   JasperPrint jasperPrint = JasperFillManager.fillReport(
     reportFilePath, parameters, ds);
   exporter.setParameter(
     JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
     Boolean.FALSE);
   exporter
     .setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
   exporter
     .setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
   exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
   exporter.exportReport();
   byte[] bytes = oStream.toByteArray();
   return bytes;
  } catch (JRException e) {
   throw new DemoException("Report Export Failed.");
  }
 }
到Excel:
public byte[] generateExcel(String begCustNo, String endCustNo,
   String reportTitle, String reportFilePath) throws DemoException {
  jdbcCustomerDao = new JdbcCustomerDao();
  Map parameters = new HashMap();
  parameters.put("ReportTitle", reportTitle);
  List list = jdbcCustomerDao.getAllCustomer(begCustNo, endCustNo);
  System.out.println("list size is :" + list.size());
  JRXlsExporter exporter = new JRXlsExporter(); // Excel
  ByteArrayOutputStream oStream = new ByteArrayOutputStream();
  try {
   JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);
   JasperPrint jasperPrint = JasperFillManager.fillReport(
     reportFilePath, parameters, ds);
   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();
   return bytes;

  } catch (JRException e) {
   throw new DemoException("Report Export Failed.");
  }
 }

jsp调用方法:
 <%
    
   
 String filePath=getServletContext().getRealPath("/")+"report.jasper"; 

 CustomerServiceImpl  custs=new CustomerServiceImpl();
    byte[] bytes=null;
  String begNo=request.getParameter("beginCustNo");
  String endNo=request.getParameter("endCustNo");
  String type=request.getParameter("type");

  if(type.equals("Pdf")){ 
      bytes= custs.generatePDF(begNo,endNo,"客户资料明细表",filePath);
  }else if(type.equals("Excel")){
   bytes=custs.generateExcel(begNo,endNo,"客户资料明细表",filePath);
  }else
     bytes=custs.generateHtml(begNo,endNo,"客户资料明细表",filePath);


 if(bytes!=null){
  if(type.equals("Pdf")){
         response.setContentType("application/pdf");
  }else if(type.equals("Excel")){
     response.setContentType("application/vnd.ms-excel");
  }else
   response.setContentType("text/html");
    response.setContentLength(bytes.length);
    ServletOutputStream ouputStream = response.getOutputStream();
    ouputStream.write(bytes,0,bytes.length);
    ouputStream.flush();
    ouputStream.close();
 }else
 {
  out.println("error");
 }
 

 

  %>

posted on 2006-11-12 22:21 robbin163 阅读(6363) 评论(6)  编辑  收藏

FeedBack:
# re: jasperreport生成html,Excel,PDF报表,数据源使用List
2007-09-06 10:52 | sss
你引的包都有那些  回复  更多评论
  
# re: jasperreport生成html,Excel,PDF报表,数据源使用List
2007-09-06 10:53 | sss
能不能给完整的代码?我也在做公司报表。需要一个完整的代码  回复  更多评论
  
# re: jasperreport生成html,Excel,PDF报表,数据源使用List
2007-09-06 11:19 | sss
还有你的“jdbcCustomerDao = new JdbcCustomerDao();”
里面的jdbcCustomerDao 这个包引不出来呀!  回复  更多评论
  
# re: jasperreport生成html,Excel,PDF报表,数据源使用List
2008-05-20 09:36 | sss
就是个垃圾!!  回复  更多评论
  
# re: jasperreport生成html,Excel,PDF报表,数据源使用List
2009-08-21 09:41 | 你傻啊
你傻啊, 这是你自己定义的连接的连接数据库的@sss
  回复  更多评论
  
# re: jasperreport生成html,Excel,PDF报表,数据源使用List
2009-10-28 17:23 | weijiadi
jdbcCustomerDao 里面有些什么内容  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2009年8月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜