废话不多说,直接上代码
/**
* 用来标识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());
}
}
个人总结所得,大佬勿喷,如有好的解决方案,还请评论区留言讨论,谢谢
原创文章,转载请注明出处!!!!