本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  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");
 }
 

 

  %>

======================================
servlet 输出

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        Connection conn
=null;
        
try{
            Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
            conn
=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1481;DatabaseName=pdw","sa","123456");
            ServletContext servletContext
=this.getServletContext();
            Map parameters
=new HashMap();
            parameters.put(
"vname","裴德万");
            File reportfile
=new File(servletContext.getRealPath("/classes.jasper"));
            System.out.println(reportfile.getPath());
            
byte[] bytes=JasperRunManager.runReportToPdf(reportfile.getPath(),parameters,conn);
            response.setContentType(
"application/pdf");
            response.setContentLength(bytes.length);
            ServletOutputStream outputStream
=response.getOutputStream();
            outputStream.write(bytes,
0,bytes.length);
            outputStream.flush();
            outputStream.close();
        }
catch(Exception e){
            e.printStackTrace();
        }
    }
posted on 2007-11-01 18:19 有猫相伴的日子 阅读(4788) 评论(4)  编辑  收藏 所属分类: 报表

评论:
# re: jasperreport使用list输出[未登录] 2008-07-25 14:06 | winnie
你好,但是我有个问题请教一下你,就是生成Excel的时候用list作为数据源,如果我list.size=0为什么会生成一个空的Excel呢,连静态的东西,还有定义的列都没有!我觉得应该只是没有统计的数据而已啊!  回复  更多评论
  
# re: jasperreport使用list输出 2008-09-08 10:19 | zb
report.jasper是什么样的  回复  更多评论
  
# re: jasperreport使用list输出[未登录] 2011-09-28 17:17 |
List list=this.customerDao.getAllCustomer(); //得到所有客户
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);
JasperPrint jasperPrint = JasperFillManager.fillReport(
reportFilePath, parameters, ds);
你确定这些代码就够了,能跑通,我反正没通,报ClassNotFound的Exception  回复  更多评论
  
# re: jasperreport使用list输出 2014-07-08 11:28 | villain
@winnie
我也有这个问题啊  回复  更多评论
  

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


网站导航:
 
本站不再更新,欢迎光临 java开发技术网