Jasperreports6.3.1+Jaspersoft studio6.3.1进行报表开发实战教程(六)-模板元素详解

来源:互联网 发布:单片机反编译 编辑:程序博客网 时间:2024/06/03 14:28

前言

上节已经介绍了如何用实体类集合来填充报表,下载demo传送门 
本节主要讲解模板设计中如何放置模板元素达到我们想要的效果,涉及到模板元素的使用,如detail,column header等,下面将一一讲解outline中各个自带元素的使用,其实第二节中有粗略讲到它们的含义。 
这里写图片描述

Title &Detail

title的作用我们用上一节的学生成绩模板可以明显地看到。 
这里写图片描述 
打开预览,生成了三页,是不是很奇怪上一节才生成了两页,为何这一次生成了三页,因为我将所有元素都加回来了,上一节的模板只保留了title和detail. 
这里写图片描述 
明显看到page header和column header都占了一定的位置,从中也可以看到Title的作用。 
Title在第一页中显示,而第二页和第三页都不会再显示,所以Title只会显示一次。

Detail则是要放置填充数据的位置,因为它会加载多次,有多少条数据就会加载多少次,保证了数据可以全部显示出来。 
这里写图片描述

顾名思义,页的头部,应该就是我们所说的页眉,我们的word文档一般都会有页眉,写点励志的话,现在就来试验一下吧。 
这里写图片描述 
来看看预览的效果如何。 
这里写图片描述 
这里写图片描述 
这里写图片描述 
每一页都出现了这句话,即Page Header会在每一页都加载一次,与Detail作用一致。

首先看名字,表头与表尾,作用与Page Header应该相差无几,都是提示文字,表头可以放表格里列的名字,表尾可以放一些数据的总结概括等等。 
在Column Header 和 Column Footer各自放置两段Static Text,然后进行预览。 
这里写图片描述 
预览如下图。 
这里写图片描述 
这里写图片描述 
可以看到,Column Header与 Column Footer确实在每一页都有出现,Column Header出现在与模板相同的位置上,但是Column Footer却出现在了页底的位置,与模板设计界面相差甚远,这是为何? 
我的猜测是,模板上我们看的并不是真正的页面大小的预览,只是显示了从上到下排放元素的实际长度,如何没有放置元素软件就会把界面缩小到只有内容的长度。 
如我们的模板是A4,21cm*29cm,实际上只显示了的长度才为26cm左右。实际上我们是可以拖动这些元素的大小的,拖得越短,显示的越少。 
这里写图片描述 
仔细观察我们的预览图,Colomn Footer实际显示的位置应该是在Page Footer的上方,而Detail元素则会填充剩余的位置,因为我们的表格是固定大小的,所以多余的大小都显示为空白了。

有了页眉,自然有对应的页脚。一般页脚的作用都是用于显示页数的。现在我们就来放置一个页数来看看效果,Jaspersoft studio就自带这个显示页数的组件,使用非常方便。 
这里写图片描述 
其实这个组件就是引用了studio自带的一个变量PAGE_NUMBER,现在来看看预览效果。

第一页

这里写图片描述

第二页

这里写图片描述

第三页

这里写图片描述 
奇怪的是第三页没有正常显示,这是为何?是不是不够位置了,我们把Page Footer拉长一些试试。 
这里写图片描述 
结果还是没有显示,只有Column Footer显示。看来最后一页是特殊的,用Page Footer是没法显示最后一页的。幸好还有一个元素,Last Page Footer,看来这个元素就是用以最后一页页脚的。

上面已经说明这个元素的作用,现在就来试试它是不是真的用于最后一页的。 
添加一个页数显示,与Page Footer一样。 
这里写图片描述 
打开预览

第二页

这里写图片描述

第三页

这里写图片描述 
第三页显示了正常的页数,所以我们的猜测是正确的,Last Page Footer只用于最后一页,只有最后一页才显示Last Page Footer,而Page Footer在最后一页是不显示的,故Last Page Footer在有页数显示时是一定要添加的。 
由此也可以看出其实Page Footer 与Last Page Footer虽然都添加在模板中,实际上始终只显示一个。

Summary

中文意思是摘要,一般而言书籍的摘要都是放在第一页的,翻书的第一眼看到不是摘要就是序言。那么模板里的Summary的摘要是显示在哪里的?有何作用呢?只好实践一番了。 
在Summary里添加一段话,时间与总页数,看看效果如何。 
这里写图片描述 
查看预览 
第一页与第二页都没看到有何文字显示,直到打开第三页才发现了摘要的位置原来是在这里,感觉像发现新世界了一样惊奇。 
这里写图片描述 
是不是很惊讶,没想到Summary竟然出现了最后一页的空白位置,而且是紧跟在Detail元素的下方,在Column Footer与Last Page Footer的上方。看来Summary是数据显示完之后用以总结的,并且会出现在Detail表格下方,在页面填充的空白无数据的位置出现。 
但是如果我把Detail调到最大,不让出现空白的位置,这里Summary会不会正常显示呢。 
我把Detail拉到最大,只能向下拉,向上拉只会拉短Detail上方的Column Header的长度,但是当拉到27cm时会无法再向下拉了,应该是有长度限制的Detail。 
这里写图片描述 
预览,发现生成了五个页面

第一页

这里写图片描述

第四页

这里写图片描述

第五页

这里写图片描述 
第一页并没有显示表格,看来有了title,detail就不够位置显示了,表格就放到了下一页进行显示。 
Summary出现在最后一个单独的页面,这时Page Header, Column Header/Footer,Page Footer没有显示,Last Page Footer也没有显示,结论是如果Summary单独出现在一个单独的界面时,只会显示Summary,不会再显示其它元素。

No Data

不放置数据,没什么特殊含义,可以略去。

Background

Background应该是报表的背景,可以放一张图片试试效果如何,Background可以调到跟界面一样大。

拉取一个Image组件到背景元素中,选择第一项以项目中的图片作为资源来源。

这里写图片描述

将图片放在Background 中,可以看到图片的预览,显示的大小与图片本身的像素相关联,当然我们也可以拉大它。 
这里写图片描述 
现在来看看报表模板生成的预览。 
这里写图片描述

背景的图片在每一页都会显示,并且开始位置是与页面的实际大小的开始位置一致的(设置了页边距)。 
我们将图片放大一些,看看效果。

生成预览 
这里写图片描述

将图片拉得多大,在页面中就显示为多大,最大为整个页面的高度。 
至此,Jasperreport的教程就算是完成第一个阶段了,算是入门了。下面是1-6章的案例demo,不包括所依赖的jar包,有报表模板文件jrxml,还有bean的jar包,生成模板文件的Java源代码。

官方文档说明

TITLE

This is the first section of the report. It is generated only once during the report-filling 
process and represents the beginning of the resulting document. 
The title section precedes even the page header section. To print the page header before 
the title section, put the elements on the page header at the beginning of the title section 
as well. You can suppress the actual page header on the first page using the 
《printWhenExpression》, based on the PAGE_NUMBER report variable. 
As described in “Report Template Properties” on page 42, the title section can be 
followed by a page break if the isTitleNewPage attribute is set to true.

PAGE HEADER

This section appears at the top of each page in the generated document.

COLUMN HEADER

This section appears at the top of each column in the generated document.

DETAIL

For each record in the data source, the engine tries to generate this section. The detail 
section can be made of multiple bands.

This section appears at the bottom of each column in the generated document. It never 
stretches downward to acquire the content of its containing text fields. Its rendering 
position is controlled by the isFloatColumnFooter flag declared at report template 
level.

This section appears at the bottom of each page in the generated document. Just like the 
column footer section, the page footer never stretches downwards to acquire the content 
of its containing text fields and always retains the declared fixed height.

SUMMARY

This section is generated only once per report and appears at the end of the generated 
document, but is not necessarily the last section generated. This is because in some cases 
the column footer and/or page footer of the last page follows it. 
As mentioned in “Report Template Properties” on page 42, you can have the summary 
section start a new page of its own by setting the isSummaryNewPage attribute to true. 
Even if this attribute remains false, the summary section always starts a new page if it 
does not fit on the remaining space of the last page, or if the report has more than one 
column and it has already started a second column on the last page.

If present, this section replaces the normal page footer section, but only on the last 
occurrence of the page footer, which might not be the last page if the summary is present 
and it overflows on multiple pages or it is rendered alone on its own last page. So it 
behaves more like the last page footer than the footer of the last page.

BACKGROUND

This is a special section that is rendered on all pages and its content placed underneath 
all other report sections. Normal report sections are rendered one after the other, but the 
background section does not interfere with the other report sections and can be used to 
achieve watermark effects or to create the same background for all pages. 
If the main report sections described here don’t meet your needs, you might consider 
introducing supplementary sections like group headers and group footers. 
The following section shows how to group data on the report.

NO DATA

This is another special section that is generated only once per report and, under certain 
conditions, its content will replace all the ordinary report’s content. 
Sometimes when the report data source is empty, is very useful to generate an equivalent 
content, a notice, maybe, or an image, in order to replace all the empty zone. Especially 
in the case of subreports, more useful is to replace all the subreport’s content with an 
equivalent one. 
If the ‘noData’ section is defined in the report template, and if the data source is empty, 
then the ’noData‘ section will be the only one taken into account at fill time, and its 
content will produce the report output.

阅读全文
0 0
原创粉丝点击