ireport明细模版补充空白行

废话不多说,直接上代码


 /**
     * 用来标识HTML中的空行,由于ireport排版问题,detail和page lastpage中间有空白行,导致的现有版本只能手动去掉空行
     */
    public final static String HTML_BLANK_IDENTIFICATION = "________";



/**
     * 用于处理明细报表中补全最后一页的空白行功能
     *
     * @param jasperReport ireport对象
     * @param detailList   报表明细内容集合
     * @throws FileNotFoundException
     * @throws JRException
     */
    public static void getLastPageDetailHeight(JasperReport jasperReport, List<Map<String, String>> detailList) throws JRException {

        try {
            int pageHeight = jasperReport.getPageHeight();

            int topMargin = jasperReport.getTopMargin();
            int bottomMargin = jasperReport.getBottomMargin();
            int titleHeight = 0;
            int pageHeaderHeight = 0;
            int columnHeaderHeight = 0;
            int detailHeight = 0;
            int columnFooterHeight = 0;
            int pageFooterHeight = 0;
            int lastPageFooterHeight = 0;
            int summaryHeight = 0;

            if (jasperReport.getTitle() != null)
                titleHeight = jasperReport.getTitle().getHeight();
            if (jasperReport.getPageHeader() != null)
                pageHeaderHeight = jasperReport.getPageHeader().getHeight();
            if (jasperReport.getColumnHeader() != null)
                columnHeaderHeight = jasperReport.getColumnHeader().getHeight();

            if (jasperReport.getDetailSection() != null) {
                for (JRBand detailBand : jasperReport.getDetailSection().getBands()) {
                    detailHeight += detailBand.getHeight();
                }
            }
            if (jasperReport.getColumnFooter() != null)
                columnHeaderHeight = jasperReport.getColumnHeader().getHeight();
            if (jasperReport.getPageFooter() != null){
                JRBaseStaticText blankChild = (JRBaseStaticText)jasperReport.getPageFooter().getChildren().get(0).clone();
                blankChild.setText(HTML_BLANK_IDENTIFICATION);
                blankChild.setForecolor(new Color(255,255,255));

                jasperReport.getPageFooter().getChildren().add(blankChild);
                pageFooterHeight = jasperReport.getPageFooter().getHeight();
            }

            if (jasperReport.getLastPageFooter() != null){
                JRBaseStaticText blankChild = (JRBaseStaticText)jasperReport.getLastPageFooter().getChildren().get(0).clone();
                blankChild.setText(HTML_BLANK_IDENTIFICATION);
                blankChild.setForecolor(new Color(255,255,255));
                jasperReport.getLastPageFooter().getChildren().add(blankChild);
                lastPageFooterHeight = jasperReport.getLastPageFooter().getHeight();
            }

            if (jasperReport.getSummary() != null)
                summaryHeight = jasperReport.getSummary().getHeight();

            int exceptDetailAndPageAndLastPageHeight = topMargin + bottomMargin + titleHeight + pageHeaderHeight + columnHeaderHeight + columnFooterHeight + summaryHeight;

            // 最后一页有last page footer 它和 page footer 不会同时出现,故不减 page footer 的高度
            int lastPageExceptDetailHeight = pageHeight - exceptDetailAndPageAndLastPageHeight - lastPageFooterHeight;
            // 非最后一页可存放detail的高度
            int notLastPageDetailHeight = pageHeight - exceptDetailAndPageAndLastPageHeight - pageFooterHeight;

            // 每页可以存放detail的条数
            int everyDetailNum = notLastPageDetailHeight / detailHeight;
            // 最后一页detail的条数
            int lastPageDetailNum = detailList.size() % everyDetailNum;

            System.out.println("每页可以存放detail的条数  >> "+ everyDetailNum);
            System.out.println(" 最后一页detail的条数  >> "+ lastPageDetailNum);

            int addBlankNum;
            int lastPageMaxDetailNum = (pageHeight - exceptDetailAndPageAndLastPageHeight - lastPageFooterHeight) / detailHeight;
            if ((lastPageDetailNum * detailHeight + lastPageFooterHeight) > (pageHeight - exceptDetailAndPageAndLastPageHeight)) {
                addBlankNum = everyDetailNum - lastPageDetailNum + lastPageMaxDetailNum;
            } else {
                addBlankNum = lastPageMaxDetailNum - lastPageDetailNum;
                System.out.println("最后一页最大条数  >> "+ lastPageMaxDetailNum);
            }

            JRField[] jrFields = jasperReport.getMainDataset().getFields();

            List<Map<String, String>> bankDetailList = new ArrayList<Map<String, String>>(addBlankNum);
            for (int detailNum = 0; detailNum < addBlankNum; detailNum++) {
                Map<String, String> map = new HashMap<String, String>();
                for (JRField jrField : jrFields) {
                    map.put(jrField.getName(), "");
                }
                bankDetailList.add(map);
            }
            detailList.addAll(bankDetailList);
        }catch (Exception e){
            throw new Exception (e.getMessage());
        }

    }

 

个人总结所得,大佬勿喷,如有好的解决方案,还请评论区留言讨论,谢谢

 

原创文章,转载请注明出处!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值