栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在导出中合并多个jasper报告时,如何重新计算页码?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在导出中合并多个jasper报告时,如何重新计算页码?

jasper-report方法将不是在Java中合并报告,而是使用主报告,并将不同的报告作为子报告包含在此主报告中,从而将页码移至主报告中。

但是,如果我们希望像问题中那样结合使用Java,则也可以使用Java编辑和更正页码。

这个想法是在报表中设置“标记”,然后对进行后处理,然后

JasperPrint
用实际的页码替换标记。

jrxml (使用Freemaker样式时不要感到羞耻)

<textField>    <reportElement x="435" y="30" width="80" height="20" uuid="14bad2ac-3a56-4cf4-b4dd-310b8fcd2120"/>    <textElement textAlignment="Right"/>    <textFieldexpression><![CDATA["${CURRENT_PAGE_NUMBER}"]]></textFieldexpression></textField><textField evaluationTime="Report">    <reportElement x="515" y="30" width="40" height="20" uuid="06567ba6-6243-43e9-9813-f6593528524c"/>    <textFieldexpression><![CDATA["${TOTAL_PAGE_NUMBER}"]]></textFieldexpression></textField>

Java

定义我的标记

private static final String CURRENT_PAGE_NUMBER = "${CURRENT_PAGE_NUMBER}";private static final String TOTAL_PAGE_NUMBER = "${TOTAL_PAGE_NUMBER}";

fillReport
我的报告之后,用真实数字替换所有标记

//First loop on all reports to get totale page numberint totPageNumber=0;for (JasperPrint jp : jasperPrintList) {    totPageNumber += jp.getPages().size();}//Second loop all reports to replace our markers with current and total numberint currentPage = 1;for (JasperPrint jp : jasperPrintList) {    List<JRPrintPage> pages = jp.getPages();    //Loop all pages of report    for (JRPrintPage jpp : pages) {        List<JRPrintElement> elements = jpp.getElements();        //Loop all elements on page        for (JRPrintElement jpe : elements) { //Check if text element if (jpe instanceof JRPrintText){     JRPrintText jpt = (JRPrintText) jpe;     //Check if current page marker     if (CURRENT_PAGE_NUMBER.equals(jpt.getValue())){         jpt.setText("Page " + currentPage + " of"); //Replace marker         continue;     }     //Check if totale page marker     if (TOTAL_PAGE_NUMBER.equals(jpt.getValue())){         jpt.setText(" " + totPageNumber); //Replace marker     } }        }        currentPage++;    }}

注意:
如果这是我项目中的代码,则我不会嵌套大量的for和if语句,而是将代码提取到不同的方法中,但是为了清楚起见,我决定将所有内容都作为一个代码块发布

现在准备出口

JRPdfExporter exporter = new JRPdfExporter();exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));....


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609459.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号