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

Java代码  JasperReport学习笔记7-导出成其它格式(pdf,xls,html)_JasperReport
  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. //Excel JExcelApiExporter 来自JRExporter

  15. JExcelApiExporter xlsExporter = new JExcelApiExporter();

  16. xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

  17. xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"WebRoot//JasperFile//exporter.xls");

  18. xlsExporter.exportReport();

  19. } catch (JRException e) {

  20. e.printStackTrace();

  21. }



在WEB上面生成HTML的报表
Java代码  JasperReport学习笔记7-导出成其它格式(pdf,xls,html)_JasperReport
  1. publicclass HtmlReportServlet extends HttpServlet {

  2. privatestaticfinallong serialVersionUID = 1L;

  3. publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

  4. throws ServletException, IOException {

  5. try {

  6. //连结数据库

  7. Class.forName("com.mysql.jdbc.Driver");

  8. Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/jasperreportdb","root","root");

  9. //获得Jasper输入流

  10. InputStream inputStream = getServletConfig().getServletContext().getResourceAsStream("JasperFile//report9.jasper");

  11. //获得JasperPrint对象

  12. JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, new HashMap(),connection);

  13. connection.close();

  14. //设置格式

  15. response.setContentType("text/html");

  16. //获得输出流 ,这里不能这样response.getOutputStream()

  17. PrintWriter printWriter = response.getWriter();

  18. //创建JRHtmlExporter对象

  19. JRHtmlExporter htmlExporter = new JRHtmlExporter();

  20. //把jasperPrint到Session里面(net.sf.jasperreports.j2ee.jasper_print)

  21. request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

  22. //设值jasperPrint

  23. htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

  24. //设置输出

  25. htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER,printWriter);

  26. //设置图片生成的Servlet(生成图片就用这个ImageServlet,并且要在XML文件里面配置 p_w_picpath?p_w_picpath=这个是Servlet的url-pattern)

  27. htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"p_w_picpath?p_w_picpath=");

  28. //导出

  29. htmlExporter.exportReport();

  30. printWriter.close();

  31. } catch (SQLException e) {

  32. e.printStackTrace();

  33. } catch (ClassNotFoundException e) {

  34. e.printStackTrace();

  35. } catch (JRException e) {

  36. e.printStackTrace();

  37. }

  38. }

  39. publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

  40. throws ServletException, IOException {

  41. this.doGet(request, response);

  42. }

  43. }


注意:这个因为用到了JasperReport里面的一个Servlet要在web.xml文件里配置好
Xml代码  JasperReport学习笔记7-导出成其它格式(pdf,xls,html)_JasperReport
  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/p_w_picpath</url-pattern>

  13. </servlet-mapping>