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 閱讀(6390)
評論(6) 編輯 收藏