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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_38845611/article/details/79930402

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();  
    }  
}

导出完毕

展开阅读全文

急···请教ireport导出word报表

07-27

我在jsp页面中调用这个action,但是却什么也导不出,想问问下面的代码哪里有问题,在jrxml这个文件夹有shipReceiveStyreneReport.jasper和shipReceiveStyreneReport.jrxml两个文件, 谢谢!!rnrnrn[code=Java]rnrnpublic String printlnShipReceiveStyrene() rn String jrxmlFile=new String("E:\\apache-tomcat-6.0.26"+"\\webapps\\PMI\\reports\\materialManagement\\shipReceiveStyrene\\jrxml\\shipReceiveStyreneReport.jrxml"); rn String jasperFile=new String("E:\\apache-tomcat-6.0.26"+"\\webapps\\PMI\\reports\\materialManagement\\shipReceiveStyrene\\jasper\\shipReceiveStyreneReport.jasper"); rn String jrprintFile=new String("E:\\apache-tomcat-6.0.26"+"\\webapps\\PMI\\reports\\materialManagement\\shipReceiveStyrene\\jrprint\\shipReceiveStyreneReport.jrprint"); rn String docFile=new String("E:\\apache-tomcat-6.0.26"+"\\webapps\\PMI\\reports\\materialManagement\\shipReceiveStyrene\\doc\\shipReceiveStyreneReport.doc"); rn String[] params= (String[]) parameters.get("printlnDate"); rn String printlnDate=""; rn if(params!=null) rn printlnDate=params[0].substring(0,4)+"年"+params[0].substring(5,7)+"月"; rn rn //以下值为传输到报表的变量值 rn HashMap parameter=new HashMap(); rn parameter.put("dateTime1",printlnDate); rn try rn Connection conn= (Connection) DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=pmi", "root", "root"); rn /* rn * 编译报表:将文件从指定位置编译到目标位置 rn */ rn JasperCompileManager.compileReportToFile(jrxmlFile, jasperFile); rn rn //填充报表:转化为print rn JasperFillManager.fillReportToFile(jasperFile, jrprintFile, parameter, conn); rn //导出为word,成功,还需直接打开显示. rn JasperPrint jp=(JasperPrint) JRLoader.loadObject(jrprintFile); rn JRExporter exporter = new JRRtfExporter(); rn exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp); rn exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, docFile); rn exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR,Boolean.TRUE); rn exporter.exportReport(); rn try rn System.out.println("文件名:"+docFile); rn java.lang.Runtime.getRuntime().exec("\"C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.EXE\"\""+docFile+"\""); rn catch (IOException e) rn e.printStackTrace(); rn rn return processSuccess(); rn catch(JRException jre) rn return processException(jre); rn catch(MappingException me) rn return processException(me); rn catch(HibernateException he) rn return processException(he); rn catch (SQLException e) rn return processException(e); rn rn rnrn[/code]rn 论坛

没有更多推荐了,返回首页