ireport使用中遇到的问题

ireport 通过java导出xls,pdf格式
代码如下:

Path reportFile = Paths.get(System.getProperty("user.dir"),
                    "/webapps/mdap-report/WEB-INF/jaspers/" + report + ".jasper");

            String path = reportFile.toString();

            Map params = new HashMap();
            MysqlConnection mysqlConnection = new MysqlConnection();
            connection = mysqlConnection.getConnection();
            if (reportType.equals("xls")) {
                JasperPrint print = JasperFillManager.fillReport(path, params, connection);

                response.setContentType("application/vnd.ms-excel");
                String defaultname = "report.xls";
                String reportName = new String(defaultname.getBytes("gbk"), "utf-8");
                response.setHeader("Content-disposition", "attachment; filename=" + reportName);
                ServletOutputStream ouputStream = response.getOutputStream();
                JRXlsExporter exporter = new JRXlsExporter();
                exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
                exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, ouputStream);
                exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
                exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
                String sheetName = "入口明细表";
                String[] sheetNames = { sheetName };
                exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES, sheetNames);
                String decoderReportName = "入口统计报表";
                if (name != null) {
                    decoderReportName = name;
                }
                String decoderFileName = UrlUtil.getURLDecoderString(decoderReportName);
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + getFilename(request, decoderFileName) + ".xls");
                response.setContentType("application/vnd.ms-excel");
                exporter.exportReport();
                ouputStream.flush();
                ouputStream.close();
            }
            if (reportType.equals("pdf")) {
                // 导出PDF
                HashMap<String, Object> parameters = new HashMap<String, Object>();// 给报表模板文件传参

                // 得到枚举类型的参数名称,参数名称若有重复的只能得到第一个--获取页面传来的参数,和模板中文件的sql参数名称一一对应
//              Enumeration<?> temp = request.getParameterNames();
//              while (temp.hasMoreElements()) {
//                  String paramName = (String) temp.nextElement();
//                  String paramValue = request.getParameter(paramName);
//                  parameters.put(paramName, paramValue);
//              }

                // System.out.println("--------path------------" + path);
                MysqlConnection mysqlConnection1 = new MysqlConnection();
                connection = mysqlConnection1.getConnection();

                byte[] bytes = JasperRunManager.runReportToPdf(path, parameters, connection);
                response.setContentType("application/pdf");
                response.setContentLength(bytes.length);
                ServletOutputStream out;

                out = response.getOutputStream();
                out.write(bytes, 0, bytes.length);
                out.flush();
                out.close();
            }
        }
        // } catch (IOException e) {

        // e.printStackTrace();
        // }
        catch (JRException e) {
            e.printStackTrace();
        }
    }

导出excel中数据居中问题
解决方式:可修改纸张大小Page format

报错:类找不到问题
解决方式:一般情况为jar包版本问题

将多个jasper导入到一个excel中,并且是放置到不同的sheet页

JRXlsExporter xls = new JRXlsExporter();
xls.setParameter(JRExporterParameter.JASPER_PRINT_LIST,list);//此处的list为JasperPrint的list集合
xls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
                    Boolean.TRUE);//设置为true,即可在一个excel中,每个单独的jasper对象放入到一个sheet页中

String[] sheetNames = {"自定义1","自定义2","自定义3"};
xls.setParameter(JRXlsExporterParameter.SHEET_NAMES, sheetNames);//此处这样写即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值