External Styles in JasperReports

2019-01-21 13:54发布

I'm working on a system that includes a large number of reports, generated using JasperReports. One of the newer features is that you can define styles for reports.

From the available docs I believe there is some way to have an external file defining styles to use, and you can reference that in your jasper reports. This allows a single style to be used by multiple reports.

I can't find any concrete information on whether this is an actual feature, and if it is, how to use it. Does anyone know if it is possible to have external styles for jasper reports, and if so, how to do it?

3条回答
一夜七次
2楼-- · 2019-01-21 14:35

You can also avoid specifying the actual file name in the <template> element by using a parameter passed into your report at runtime

<parameter name="TEMPLATE_FILE" isForPrompting="false" class="java.lang.String"/>

<template><![CDATA[$P{TEMPLATE_FILE}]]></template>

where $P{TEMPLATE_FILE} is the full path to the style resource

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-21 14:39

I like to share my learning of using styles in Jasper reports, which I think quite useful for report designers like me, from a book named JasperReport Development cookbook by Bilal Siddiqui. I like this book and found demonstrating styles in a variety of manner like:

  • Creating a reusable style
    Simply select “Style” while creating a new report and define style for text, line and rectangles. The style file will be stored as .jrtx file.

  • Import reusable style it in your report
    There are three chunk of information when importing styles in your report. Step1. Name and location of style template

<template><![CDATA["C:\\ BigBoldRedTemplate.jrtx"]]></template>

Step2. Each time you apply style to your report elements using the style template, a <reportElement> tag is created as shown below:

//style applied to a rectangle
<rectangle radius="10">
    <reportElement style="BigBoldRed" mode="Transparent" x="0" y="0" width="555" height="44"/>
</rectangle>
//style applied to a the text field
<staticText>
        <reportElement style="BigBoldRed" x="0" y="0" width="555" height="66"/>
        <textElement textAlignment="Center" verticalAlignment="Middle"/>
        <text><![CDATA[Monthly Customer Invoices]]></text>
</staticText>
  • Mixing the internal and reusable styles in report
  • Using the power of HTML to style your report
    For example, your text field has following expression which includes HTML tags (i.e. <li>) and you want the HTML tags to work in your report design:
"<li>"+"Invoice # "+$F{InvoiceID}+", "+

$F{CustomerName}+" purchased "+$F{ProductName}+" in "+$F{InvoicePeriod}+" (Invoice value: \$ "+$F{InvoiceValue}+")"+"

Solution is simple, just set “Markup” property of the text field to “Styled” and that it.

I have taken permission from the author to copy code chunk from his JasperReports cookbook in this post.

查看更多
女痞
4楼-- · 2019-01-21 14:46

Use JasperReport templates. A JasperReports template is one that ends in .jrtx, and may look similar to this (styles.jrtx):

<?xml version="1.0"?>
<!DOCTYPE jasperTemplate
  PUBLIC "-//JasperReports//DTD Template//EN"
  "http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd">

<jasperTemplate>
    <style name="Report Title" isDefault="false" hAlign="Center" fontSize="24" isBold="true"/>
    <style name="Heading 1" isDefault="false" fontSize="18" isBold="true"/>
    <style name="Heading 2" isDefault="false" fontSize="14" isBold="true"/>
</jasperTemplate>

and then in your .jrxml file, include it as a template:

...
<template>"styles.jrtx"</template>
...

iReport also understands this, so your styles are imported and shown in iReport correctly (though I did notice sometimes it wouldn't pick them up an a reload or recompile was necessary).

查看更多
登录 后发表回答