JasperReport还可以导出为其它格式,如PDF,XLS,RTF,HTML,CSV 
下面是导出为PDF和XLS文件

Java代码  收藏代码
  1. Class.forName("com.mysql.jdbc.Driver");  
  2.         Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/jasperreportdb","root","root");  
  3.         try {  
  4.             //据据jasper文件生成JasperPrint对象  
  5.             JasperPrint jasperPrint = JasperFillManager.fillReport("WebRoot//JasperFile//report9.jasper"new HashMap(),connection);  
  6.             //生成我们的导出类JRPdfExporter 来自JRExporter  
  7.             JRPdfExporter jrpdfExporter = new JRPdfExporter();  
  8.             //设JasperPrint参数  
  9.             jrpdfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);  
  10.             //设置输入的PDF文件放在什么地方  
  11.             jrpdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "WebRoot//JasperFile//exporter.pdf");  
  12.             //输出  
  13.             jrpdfExporter.exportReport();  
  14.               
  15.             //Excel  JExcelApiExporter 来自JRExporter  
  16.             JExcelApiExporter xlsExporter = new JExcelApiExporter();  
  17.             xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);  
  18.             xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"WebRoot//JasperFile//exporter.xls");  
  19.             xlsExporter.exportReport();           
  20.         } catch (JRException e) {  
  21.             e.printStackTrace();  
  22.         }         


在WEB上面生成HTML的报表 
Java代码  收藏代码
  1. public class HtmlReportServlet extends HttpServlet {  
  2.   
  3.     private static final long serialVersionUID = 1L;  
  4.   
  5.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  6.             throws ServletException, IOException {  
  7.         try {  
  8.             //连结数据库  
  9.             Class.forName("com.mysql.jdbc.Driver");  
  10.             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/jasperreportdb","root","root");  
  11.             //获得Jasper输入流  
  12.             InputStream inputStream = getServletConfig().getServletContext().getResourceAsStream("JasperFile//report9.jasper");           
  13.             //获得JasperPrint对象  
  14.             JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, new HashMap(),connection);  
  15.             connection.close();  
  16.             //设置格式  
  17.             response.setContentType("text/html");  
  18.             //获得输出流 ,这里不能这样response.getOutputStream()  
  19.             PrintWriter printWriter = response.getWriter();  
  20.             //创建JRHtmlExporter对象  
  21.             JRHtmlExporter htmlExporter = new JRHtmlExporter();  
  22.             //把jasperPrint到Session里面(net.sf.jasperreports.j2ee.jasper_print)  
  23.             request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);  
  24.             //设值jasperPrint  
  25.             htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);  
  26.             //设置输出  
  27.             htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER,printWriter);  
  28.             //设置图片生成的Servlet(生成图片就用这个ImageServlet,并且要在XML文件里面配置 image?image=这个是Servlet的url-pattern)  
  29.             htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"image?image=");  
  30.             //导出  
  31.             htmlExporter.exportReport();      
  32.             printWriter.close();  
  33.         } catch (SQLException e) {  
  34.             e.printStackTrace();  
  35.         } catch (ClassNotFoundException e) {  
  36.             e.printStackTrace();  
  37.         } catch (JRException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.   
  42.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  43.             throws ServletException, IOException {  
  44.         this.doGet(request, response);  
  45.     }  
  46.   
  47. }  

注意:这个因为用到了JasperReport里面的一个Servlet要在web.xml文件里配置好
Xml代码  收藏代码
  1. <servlet>  
  2.     <servlet-name>HtmlReportServlet</servlet-name>  
  3.     <servlet-class>com.langhua.JasperReport.Servlet.HtmlReportServlet</servlet-class>  
  4.   </servlet>  
  5.   <servlet>  
  6.   <servlet-name>ImageServlet</servlet-name>  
  7.     <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>  
  8.   </servlet>  
  9. <!--url-pattern视你的情况而定,如果图片出不来,你就看他的连结是什么 -->  
  10. <servlet-mapping>  
  11.     <servlet-name>ImageServlet</servlet-name>  
  12.     <url-pattern>/servlet/image</url-pattern>  
  13.   </servlet-mapping>  


转载自:http://langhua9527.iteye.com/blog/403478

posted on 2012-07-17 17:04  星^_^風  阅读(749)  评论(0编辑  收藏  举报