JasperReport - 包装文本以在文本字段中显示长文本 [英] JasperReport - wrap text to show long text in textfield

查看:140
本文介绍了JasperReport - 包装文本以在文本字段中显示长文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用jasper报告作为报告工具。我想知道如何通过将 reportElement 动态地扩展到垂直方向来包装长文本(扩展行大小,而不是列宽)。或者有没有办法实现这一目标?我的方法是截断长文本一些很长的名字。你能给我一些建议吗?

I am using jasper report as reporting tool in my application. And I am wondering how can i wrap the long text by expanding the reportElement dynamically into vertical direction ( expanding the row size, not column width). Or Is there any way to achieve this? My approach below is truncating the long text "Some very long name". Can you please give me some suggestions?

输出:

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class JrUtils {
    public static void showJrReport(List objectList, String fileName, String title, Map parameters) {
        try {

            File f = new File(fileName);
            JasperDesign jasperDesign = JRXmlLoader.load(f.getAbsolutePath());
            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(objectList);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, ds);
            JasperViewer jv = new JasperViewer(jasperPrint, false);
            jv.setTitle(title);
            jv.setVisible(true);

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public static void main(String[] args) {
        List<Person> pList = new ArrayList<Person>();

        Person p1 = new Person();
        p1.setPersonName("Some Name ");
        p1.setAddress("Nepal - Address Fits Here");

        Person p2 = new Person();
        p2.setPersonName("Some very long name");
        p2.setAddress("Nepal - Address Fits Here");

        pList.add(p1);
        pList.add(p2);

        showJrReport(pList, "testReport.jrxml", "Test Report", new HashMap<Object, Object>());
    }

}

Jasper Report JrXML文件 - testReport。 jrxml:

Jasper Report JrXML file - testReport.jrxml :

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="250" pageHeight="400" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="2.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <field name="personName" class="java.lang.String"/>
    <field name="address" class="java.lang.String"/>
    <columnHeader>
        <band height="23" splitType="Stretch">
            <rectangle>
                <reportElement x="0" y="0" width="88" height="23"/>
            </rectangle>
            <rectangle>
                <reportElement x="88" y="0" width="122" height="23"/>
            </rectangle>
            <staticText>
                <reportElement x="0" y="0" width="88" height="23"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="88" y="0" width="122" height="23"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Address]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="21" splitType="Stretch">
            <rectangle>
                <reportElement x="0" y="0" width="88" height="21"/>
            </rectangle>
            <textField>
                <reportElement x="0" y="0" width="88" height="21"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{personName}]]></textFieldExpression>
            </textField>
            <rectangle>
                <reportElement x="88" y="0" width="122" height="21"/>
            </rectangle>
            <textField>
                <reportElement x="88" y="0" width="122" height="21"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>


推荐答案

我自己找到了答案:

我对 textField 矩形的属性做了一些额外的研究组件。并且发现我需要设置以下属性。

I did some extra research about the properties of textField and rectangle components. And found that I need to set the following properties.

对于矩形:

        <rectangle>
            <reportElement stretchType="RelativeToBandHeight" ... />
        </rectangle>

对于textField:

        <textField isStretchWithOverflow="true">
            ...
        </textField>

按预期输出:

<细节> ...< / detail> 更正后的部分:

<detail>
        <band height="21" splitType="Stretch">
            <rectangle>
                <reportElement stretchType="RelativeToBandHeight" x="0" y="0" width="88" height="21"/>
            </rectangle>
            <textField isStretchWithOverflow="true">
                <reportElement x="2" y="0" width="84" height="21"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{personName}]]></textFieldExpression>
            </textField>
            <rectangle>
                <reportElement stretchType="RelativeToBandHeight" x="88" y="0" width="122" height="21"/>
            </rectangle>
            <textField isStretchWithOverflow="true">
                <reportElement x="90" y="0" width="118" height="21"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
            </textField>
        </band>
    </detail>

更新

您还可以将属性 net.sf.jasperreports.print.keep.full.text 设置为 true 来实现在你的所有报告中。

You can also set property net.sf.jasperreports.print.keep.full.text to true to achieve that across your all reports.

这篇关于JasperReport - 包装文本以在文本字段中显示长文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆