浏览 4517 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-07
最后修改:2009-06-07
JasperReport还可以导出为其它格式,如PDF,XLS,RTF,HTML,CSV
下面是导出为PDF和XLS文件 Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/jasperreportdb","root","root"); try { //据据jasper文件生成JasperPrint对象 JasperPrint jasperPrint = JasperFillManager.fillReport("WebRoot//JasperFile//report9.jasper", new HashMap(),connection); //生成我们的导出类JRPdfExporter 来自JRExporter JRPdfExporter jrpdfExporter = new JRPdfExporter(); //设JasperPrint参数 jrpdfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint); //设置输入的PDF文件放在什么地方 jrpdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "WebRoot//JasperFile//exporter.pdf"); //输出 jrpdfExporter.exportReport(); //Excel JExcelApiExporter 来自JRExporter JExcelApiExporter xlsExporter = new JExcelApiExporter(); xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"WebRoot//JasperFile//exporter.xls"); xlsExporter.exportReport(); } catch (JRException e) { e.printStackTrace(); } 在WEB上面生成HTML的报表 public class HtmlReportServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //连结数据库 Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/jasperreportdb","root","root"); //获得Jasper输入流 InputStream inputStream = getServletConfig().getServletContext().getResourceAsStream("JasperFile//report9.jasper"); //获得JasperPrint对象 JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, new HashMap(),connection); connection.close(); //设置格式 response.setContentType("text/html"); //获得输出流 ,这里不能这样response.getOutputStream() PrintWriter printWriter = response.getWriter(); //创建JRHtmlExporter对象 JRHtmlExporter htmlExporter = new JRHtmlExporter(); //把jasperPrint到Session里面(net.sf.jasperreports.j2ee.jasper_print) request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint); //设值jasperPrint htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint); //设置输出 htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER,printWriter); //设置图片生成的Servlet(生成图片就用这个ImageServlet,并且要在XML文件里面配置 image?image=这个是Servlet的url-pattern) htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"image?image="); //导出 htmlExporter.exportReport(); printWriter.close(); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (JRException e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } } 注意:这个因为用到了JasperReport里面的一个Servlet要在web.xml文件里配置好 <servlet> <servlet-name>HtmlReportServlet</servlet-name> <servlet-class>com.langhua.JasperReport.Servlet.HtmlReportServlet</servlet-class> </servlet> <servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class> </servlet> <!--url-pattern视你的情况而定,如果图片出不来,你就看他的连结是什么 --> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlet/image</url-pattern> </servlet-mapping> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |