欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ireport+JasperReport制作报表导出PDF,Word,Html,Excel

程序员文章站 2024-02-24 13:07:40
...

Ireport Designer下载与基础教程

Ireport介绍

Jasperreport是报表打印工具,支持pdf、csv多种格式的报表打印。

JasperReport是一个开源的工具包,将JasperReport包加入到项目的classpath中即可开始使用JasperReport包了,从java程序中通过调用包中的类对jrxml文件进行操作,可以完成报表的编译、显示、输出等工作;

1)利用ireport图形编辑工具,iReport是报表编辑图形工具,编辑完成后输出jrxml文件(XML格式), 生成jasper文件(以.jasper结尾);

2)直接利用java代码编译生成jasper文件(以.jasper结尾)。
最终要使用的是编译后的*.jasper文件。
Jasperreport支持多种数据源,包括xml、数据库、JavaBean等多种数据源。

JAVA代码

Controller

@RequestMapping("/printReport.do")
    public void printReport(@RequestParam("dataList")String dataList,HttpServletResponse response,HttpServletRequest request) throws IOException {
        //TODO:解析json字符串,导出pdf
        //解析字符串
        JSONArray json = JSONArray.fromObject(dataList);
        JsonConfig jsonConfig = new JsonConfig();
        //设置过滤信息
        jsonConfig.setIgnoreDefaultExcludes(true);
        jsonConfig.setExcludes(new String[] {"end_time","start_time"});
        jsonConfig.setRootClass(WyfeeCostDetailPO.class);

        //展示数据
        List<WyfeeCostDetailPO> wyfeeCostDetailPOs=  new ArrayList<WyfeeCostDetailPO>();

        for (Object object : json) {

            WyfeeCostDetailPO wyfeeCostDetailPO = 
                    (WyfeeCostDetailPO) JSONObject.toBean(JSONObject.fromObject(object,jsonConfig),WyfeeCostDetailPO.class);
            wyfeeCostDetailPOs.add(wyfeeCostDetailPO);
        }

        //找到jasper文件
        File file = new File(
                request.getSession().getServletContext()
                .getRealPath("/WEB-INF/view/crm/wyfee/arrearsReminders.jrxml"));
        //数据参数
        Map<String, Object> paramsMap = new HashMap<>();

        paramsMap.put("community",);
        paramsMap.put("residence_code",wyfeeCostDetailPOs.get(0).getResidence_code());

        //将展示数据转化为数据源
        JRDataSource dataSource = new JRBeanCollectionDataSource(wyfeeCostDetailPOs);  
        try {
            JasperPrint jasperPrint = ReportUtils.getJasperPrint(
                    request.getSession().getServletContext().getRealPath("/WEB-INF/view/crm/wyfee/arrearsReminders.jrxml"), 
                    paramsMap, 
                    dataSource);
            // 设置相应参数,以附件形式保存PDF
            response.setContentType("application/pdf");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + FileUtils.encodeDownloadFilename("缴费单.pdf",
            request.getHeader("user-agent")));
            //导出Excel文件或者Pdf文件
            ReportUtils.exportPdf(request, response, jasperPrint);



        } catch (Exception e) {
             PrintWriter out = null;  
                try {  
                    response.setCharacterEncoding("UTF-8");  
                    out = response.getWriter();  
                    out.write("<h1 style='position: absolute;left: 50%;top: 50%;margin-left: -180px;margin-top: -10px;'>报表出错,请重试!</h1>");  
                } catch (IOException ex) {  
                    ex.printStackTrace();  
                }finally{  
                    out.close();  
                }  
                e.printStackTrace();  
        }

    }

Utils

public class ReportUtils {

    /** 
     * 获取 JasperPrint 对象 
     * ckz 
     *  
     * @return 
     * @throws Exception  
     */  
    public static JasperPrint getJasperPrint(String reportFileName,Map<String, Object> params,JRDataSource dataSource) throws Exception{  
        File file = new File(reportFileName);  
        if(!file.exists())  
            throw new Exception("系统找不文件  " + reportFileName); 
        JasperReport report = JasperCompileManager.compileReport(file.getPath());
        /*JasperReport report = (JasperReport) JRLoader.loadObjectFromFile(file.getPath());  */
        JasperPrint print = JasperFillManager.fillReport(report, params, dataSource);  
        return print;  
    }  

    /** 
     * 打印pdf文件 
     * ckz 
     *  
     * @param req 
     * @param resp 
     * @param jasperPrint 
     * @throws IOException  
     * @throws JRException  
     */  
    public static void exportPdf(HttpServletRequest req,HttpServletResponse resp,JasperPrint jasperPrint) throws Exception{  
        //获取JasperPrint流 对象  
        JasperPrint print = jasperPrint;  
        //使用pdf导出器  
        JRPdfExporter exporter = new JRPdfExporter(); 
      //使用html导出器  
        JRHtmlExporter exporter = new JRHtmlExporter();
        JRXlsExporter.....等等
        //设置exporter的参数  
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);  
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resp.getOutputStream());  
        exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); 
        //执行exporter  
        exporter.exportReport();  
    }  
}

导出完毕