Java 如何对jasper报告中的列记录进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19930602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:37:24  来源:igfitidea点击:

how to sort records of a column in jasper report

javaxmlsortingjasper-reports

提问by user2983424

I am working on Jasper ireport tool. I am getting records from table, but I wanted to sort the records. Whether I have to do sorting in java code or we can do it in tool itself I am not getting. so please help me how i can sort. Thank you in advance.

我正在研究 Jasper ireport 工具。我正在从表中获取记录,但我想对记录进行排序。无论我是必须在 Java 代码中进行排序,还是我们可以在工具本身中进行排序,我都没有得到。所以请帮助我如何排序。先感谢您。

<?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="Myreport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7a6aa9cf-834e-4433-bf00-09da542cedcf">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="Title" forecolor="#FFFFFF" fontName="Times New Roman" fontSize="50" isBold="false" pdfFontName="Times-Bold"/>
    <style name="SubTitle" forecolor="#CCCCCC" fontName="Times New Roman" fontSize="18" isBold="false" pdfFontName="Times-Roman"/>
    <style name="Column header" forecolor="#666666" fontName="Times New Roman" fontSize="14" isBold="true" pdfFontName="Times-Roman"/>
    <style name="Detail" mode="Transparent" fontName="Times New Roman" pdfFontName="Times-Roman"/>
    <style name="Row" mode="Transparent" fontName="Times New Roman" pdfFontName="Times-Roman">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style mode="Opaque" backcolor="#F0EFEF"/>
        </conditionalStyle>
    </style>
    <queryString language="SQL">
        <![CDATA[select * from ireport;]]>
    </queryString>
    <field name="Username" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="Empnumber" class="java.math.BigDecimal">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="dept" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="organisation" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="Designation" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="dateOfCreation" class="java.sql.Date">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <sortField name="Username"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="132" splitType="Stretch">
            <frame>
                <reportElement mode="Opaque" x="0" y="0" width="451" height="95" backcolor="#000000" uuid="2936018d-2b8a-4dc5-9cd1-f40670680e7f"/>
                <staticText>
                    <reportElement style="Title" x="0" y="0" width="370" height="66" uuid="f1a20926-1f5e-4b58-aca4-443c2693a2d9"/>
                    <textElement>
                        <font isBold="false"/>
                    </textElement>
                    <text><![CDATA[COGXAR]]></text>
                </staticText>
                <staticText>
                    <reportElement style="SubTitle" x="157" y="66" width="249" height="29" uuid="f3292300-bafd-40a9-bed3-e60684e263a1"/>
                    <textElement>
                        <font size="22" isBold="false"/>
                    </textElement>
                    <text><![CDATA[Cogxarian]]></text>
                </staticText>
            </frame>
            <frame>
                <reportElement mode="Opaque" x="0" y="100" width="451" height="32" forecolor="#000000" backcolor="#CC0000" uuid="3f5100f2-6886-440b-b495-428750dd9274"/>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement x="307" y="12" width="144" height="20" forecolor="#FFFFFF" uuid="1cafc436-89d2-4ca3-b1a4-41b6a5f1d684"/>
                    <textElement textAlignment="Right">
                        <font size="12"/>
                    </textElement>
                    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="26" splitType="Stretch">
            <staticText>
                <reportElement style="Column header" x="0" y="7" width="92" height="18" forecolor="#000000" uuid="c663f7b2-b7b7-493a-822d-72ad6b74d24a"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[Username]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="92" y="7" width="92" height="18" forecolor="#000000" uuid="f550db3a-3e20-4955-8197-fa0b71f95ac0"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[Empnumber]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="184" y="7" width="92" height="18" forecolor="#000000" uuid="0f22eeff-a2ba-4e2f-8c7e-087305844079"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[dept]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="276" y="7" width="92" height="18" forecolor="#000000" uuid="050a8043-0089-4496-8cf7-80aeca305866"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[organisation]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="368" y="7" width="92" height="18" forecolor="#000000" uuid="ca99e045-3117-425f-b918-57fac68acc13"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[Designation]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="460" y="7" width="92" height="18" forecolor="#000000" uuid="88f8eb45-00ae-476e-a4a7-d451fff93608"/>
                <textElement>
                    <font isBold="false"/>
                </textElement>
                <text><![CDATA[dateOfCreation]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="18" splitType="Stretch">
            <frame>
                <reportElement style="Row" mode="Opaque" x="0" y="0" width="555" height="18" uuid="0105b77c-fd98-4b2e-9b1a-229de1e56889"/>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" positionType="Float" x="0" y="0" width="92" height="18" uuid="5b41779b-5b12-4843-be95-c2092e86859b"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{Username}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" positionType="Float" x="92" y="0" width="92" height="18" uuid="0a87e452-d44b-463e-9e29-67934e9561db"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{Empnumber}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" positionType="Float" x="184" y="0" width="92" height="18" uuid="a669140e-e1b8-4dad-8821-9ddb2b1d4c7d"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{dept}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" positionType="Float" x="276" y="0" width="92" height="18" uuid="c4cb3a09-f100-4c53-898a-9513decc43be"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{organisation}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" positionType="Float" x="368" y="0" width="92" height="18" uuid="de6e6deb-6a26-4ee0-9dd4-e11f446e1d56"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{Designation}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" positionType="Float" x="460" y="0" width="92" height="18" uuid="a4dd4dd7-682b-43f2-80f6-d84eea5fd001"/>
                    <textElement>
                        <font size="14"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{dateOfCreation}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
    <columnFooter>
        <band height="142" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1" uuid="d472bee8-7d59-46f2-a9ca-1adad4b8f0f9"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="160" splitType="Stretch">
            <frame>
                <reportElement mode="Opaque" x="0" y="1" width="555" height="24" forecolor="#D0B48E" backcolor="#000000" uuid="53c76dd1-ef10-49a4-a80c-729f04a2b5aa"/>
                <textField evaluationTime="Report">
                    <reportElement style="Column header" x="513" y="0" width="40" height="20" forecolor="#FFFFFF" uuid="390b5016-5c20-479f-9aee-40b0ba966798"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement style="Column header" x="433" y="0" width="80" height="20" forecolor="#FFFFFF" uuid="1ee577c5-d775-4b82-a3c5-c28b48fe5b8b"/>
                    <textElement textAlignment="Right" verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                </textField>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement style="Column header" x="2" y="1" width="197" height="20" forecolor="#FFFFFF" uuid="4272ffa9-1561-453a-b52c-9f2415d9583a"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

回答by Lucas

I am not entirely sure, but if you have a look at IReportDesigner (the official tool for creating .jrxml files) there is an option in tutorial 'group by'. And if that's not working I think IReportDesigner is the tool to achieve what you need anyway.

我不完全确定,但如果您查看 IReportDesigner(用于创建 .jrxml 文件的官方工具),教程“group by”中有一个选项。如果这不起作用,我认为 IReportDesigner 是实现您所需要的工具。

回答by codeMan

you will find a "sort" element in your iReport Designer palette.

您将在 iReport Designer 调色板中找到“排序”元素。

You can drag this element into your report layout (for example, in a header row), and you will see a toggling arrow (which can sort ascending or descending).

您可以将此元素拖到您的报告布局中(例如,在标题行中),您将看到一个切换箭头(可以按升序或降序排序)。

After adding this sort option to your report, select it, and navigate to its "properties".

将此排序选项添加到您的报告后,选择它,然后导航到其“属性”。

In this view, you can assign a column to it, and other properties like styles, etc.

在此视图中,您可以为其分配一列,以及样式等其他属性。

When you run the report and click on the arrow, it will sort the report's data based on the column you selected, and the direction (ascending or descending).

当您运行报告并单击箭头时,它将根据您选择的列和方向(升序或降序)对报告的数据进行排序。

From : Jasper Wiki

来自:贾斯珀维基

回答by Syam Kumar S

You can use the

您可以使用

<sr:sort> 

element to sort based on a field.

根据字段排序的元素。

<reportelement height="20" width="30" >
    <sr:sort sortfieldname="field_to_sort"
        sortfieldtype="Field"
        xmlns:sr="http://jasperreports.sourceforge.net/jasperreports/components"
        xsi:schemalocation="http://jasperreports.sourceforge.net/jasperreports/components"/>  
</reportelement> 

You can use iReport Designer for doing this easily. IReport is a gui based designer for creating jasper reports.

您可以使用 iReport Designer 轻松完成此操作。IReport 是一个基于 gui 的设计器,用于创建 jasper 报告。

Jasper reports, Table component also provides many interactive features like sorting.

Jasper 报告,Table 组件还提供了许多交互功能,例如排序。