Spring MVC+ Ireport 做统计报表,导出HTML、EXCEL等格式的文件,打印;

 最近在使用Spring MVC +Ireport 3.7 完成统计报表,在这里做一些记录和分享。

1、Method(导出HTML,方法过时)

@RequestMapping(value = "/exportHtml")
    public void exportHtml(HttpServletRequest request,
                                   HttpServletResponse response) throws IOException, JRException {
        String ctxpath = request.getSession().getServletContext()
                .getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
        //得到要显示的数据
        List reportList = reportService.listAc01ApplicationReport();
        File reFile = new File(ctxpath);
        //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
        Map parameters = new HashMap();
        parameters.put('info','哈哈哈');
        //封装reportList中数据作为数据源
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                reFile.getPath(), parameters, ds);
        //导出html(该方法)
        JRHtmlExporter exporter = new JRHtmlExporter();
        exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, response.getWriter());
        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
        exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "utf-8");
        exporter.exportReport();
    }

     2、Method(导出EXCEL,方法过时)

@RequestMapping(value = "/exportExcel")
    public void exportExcel(HttpServletRequest request,
                                   HttpServletResponse response) throws IOException, JRException {
        String ctxpath = request.getSession().getServletContext()
                .getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
        //得到要显示的数据
        List reportList = reportService.listAc01ApplicationReport();
        File reFile = new File(ctxpath);
        //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
        Map parameters = new HashMap();
        parameters.put('info','哈哈哈');
        //封装reportList中数据作为数据源,irepot中通过获取对象(report)的属性获取其值
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                reFile.getPath(), parameters, ds);
        //导出EXCEL
        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
        exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        response.setHeader("Content-Disposition", "attachment;filename=template.xls");//设置导出文件名
        response.setContentType("application/vnd_ms-excel;charset=utf-8");
        exporter.exportReport();
    }

3、Ireport模板

  
Spring MVC+ Ireport 做统计报表,导出HTML、EXCEL等格式的文件,打印;_第1张图片 

         

        虽然以上的方法可以使用,但是在编译的时候提示方法过时,考虑到系统以后的扩展性,还是尽量不去使用过时方法,可以用以下方法来替代。

4、Method(导出HTML)

@RequestMapping(value = "/exportHtml")
    public void exportHtml(HttpServletRequest request,
                                   HttpServletResponse response) throws IOException, JRException {
        String ctxpath = request.getSession().getServletContext()
                .getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
        //得到要显示的数据
        List reportList = reportService.listAc01ApplicationReport();
        File reFile = new File(ctxpath);
        //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
        Map parameters = new HashMap();
        parameters.put('info','哈哈哈');
        //封装reportList中数据作为数据源
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                reFile.getPath(), parameters, ds);
        //导出html,替换为这段
        HtmlExporter exporter=new HtmlExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getOutputStream()));
        SimpleHtmlExporterConfiguration configuration=new SimpleHtmlExporterConfiguration();
        exporter.setConfiguration(configuration);
        exporter.exportReport();
    }

5、Method(导出EXCEL)

@RequestMapping(value = "/exportHtml")
    public void exportHtml(HttpServletRequest request,
                                   HttpServletResponse response) throws IOException, JRException {
        String ctxpath = request.getSession().getServletContext()
                .getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
        //得到要显示的数据
        List reportList = reportService.listAc01ApplicationReport();
        File reFile = new File(ctxpath);
        //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
        Map parameters = new HashMap();
        parameters.put('info','哈哈哈');
        //封装reportList中数据作为数据源
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                reFile.getPath(), parameters, ds);
        //导出excel,替换为这段
        JRXlsExporter exporter = new JRXlsExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
        SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
        //设置导出文件名称
        response.setHeader("Content-Disposition", "attachment;filename=template.xls");
        //设置导出文件格式
        response.setContentType("application/vnd_ms-excel;charset=utf-8");
        exporter.setConfiguration(configuration);
        exporter.exportReport();
    }
        如果要导出其他格式的文件,只要改变导出适配器(Exporter)就可以了,例如要导出PDF,那就使用JRPdfExporter。很简单吧,但是纸上得来终觉浅,还是要亲自实践一下哦。

7、打印

要实现打印功能也是很简单的,只要将导出的那段代码替换为打印的就好了,如下。
@RequestMapping(value = "/print")
    public void print(HttpServletRequest request) throws IOException, JRException {
        String ctxpath = request.getSession().getServletContext()
                .getRealPath("/WEB-INF/jsp/cydk/report/jasper/districtReport.jasper");
        //得到要显示的数据
        List reportList = reportService.listAc01ApplicationReport();
        File reFile = new File(ctxpath);
        //这边也可以填充数据(键值),直接在ireport中通过$P{key_name}获取
        Map parameters = new HashMap();
        parameters.put('info','哈哈哈');
        //封装reportList中数据作为数据源
        JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(reportList);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                reFile.getPath(), parameters, ds);
        //替换掉导出的代码,true为弹出打印机设置,false为直接打印
        JasperPrintManager.printReport(jasperPrint,true);
    }

你可能感兴趣的:(springmvc,导出ireport报表,打印)