如何在PDF报告中显示ArrayList。报告模板在JasperStudio中编译。

huangapple go评论69阅读模式
英文:

How to display the ArrayList in a PDF report. The report template was compiled at JasperStudio

问题

这是你提供的Java代码和Jasper报表模板的相关信息。如果你需要关于这些内容的进一步帮助或解释,请随时提出你的问题。

英文:

Such a problem. Compiled a report template on JasperStudio. Everything is displayed well, except for the ArrayList - it is not displayed at all (blank). Help, can anyone come across such a situation. The report itself on JasperStudio is displayed well, but I need to generate it in Java, so I copied .jrxml to the development environment intellij idea Here is my code:

Controller:

@GetMapping("/report")
public ResponseEntity<byte[]> generateReport() throws IOException, DocumentException, JRException {
    byte[] result = reportService.exportReport();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_PDF);
    headers.setContentDispositionFormData("attachment", "report.pdf");
    return ResponseEntity.ok().headers(headers).body(result);
}

Service class:

public byte[] exportReport() throws IOException, JRException, DocumentException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    BoatCards boatCards = this.boatCardsDao.getByCardid(137);
    InfoAboutBoatsReport infoAboutBoatsReport = this.boatCardsToInfoAboutBoatsReportConverter.convert(boatCards);
    File file = ResourceUtils.getFile("classpath:boatCards.jrxml");
    JasperReport jasperReport = JasperCompileManager.compileReport(file.getAbsolutePath());
    JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(Collections.singletonList(infoAboutBoatsReport));
    Map<String, Object> map = new HashMap<>();
    map.put("created", "dataSource");
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, dataSource);
    JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
    return outputStream.toByteArray();
}

In service class I use converter (boatCardsToInfoAboutBoatsReportConverter.convert):

@Component
public class BoatCardsToInfoAboutBoatsReportConverter implements Converter<BoatCards, InfoAboutBoatsReport> {

    @Autowired
    private EnginesDao enginesDao;

    @Override
    public InfoAboutBoatsReport convert(BoatCards boatCards) {
        String cardid = boatCards.getCardid().toString();
        String regNum = boatCards.getRegNum();
        String tiketNum = boatCards.getTiketNum();
        String boatName = boatCards.getBoatName();
        String boatType = boatCards.getBoatType().getBtname();
        String boatYear = boatCards.getBoatYear();
        String boatVin = boatCards.getBoatVin();
        String parkingPlace = boatCards.getParkingPlace();
        String saCategory = boatCards.getSaCategory().getSacName();
        String boatLength = boatCards.getBoatLength().toString();
        String boatWidth = boatCards.getBoatWidth().toString();
        String boatHeight = boatCards.getBoatHeight().toString();
        String bodyMaterial = boatCards.getBodyMaterial().getMatname();
        String boatPayload = boatCards.getBoatPayload().toString();
        String passengersNum = boatCards.getPassengersNum().toString();
        String serviceLife = boatCards.getServiceLife();
        String engineNum = boatCards.getEngineNum()==null?null:boatCards.getEngineNum().toString();
        String agentDoverennost = boatCards.getAgentDoverennost();
        String ownerType = boatCards.getOwnerType().getPtName();
        String ownerSurname = boatCards.getOwnerSurname();
        String ownerName = boatCards.getOwnerName();
        String ownerMidname = boatCards.getOwnerMidname();
        String ownerBirthDate = boatCards.getOwnerBirthDate() == null ? null : boatCards.getOwnerBirthDate().toString();
        String ownerPersNum = boatCards.getOwnerPersNum();
        String ownerDocType = boatCards.getOwnerDocType() == null ? null : boatCards.getOwnerDocType().toString();
        String ownerDocNum = boatCards.getOwnerDocNum();
        String ownerDocDepartment = boatCards.getOwnerDocDepartment();
        String ownerDocDateIssue = boatCards.getOwnerDocDateIssue().toString();
        String leUnp = boatCards.getLeUnp();
        String leName = boatCards.getLeName();
        String leDocDepartment = boatCards.getLeDocDepartment();
        String leEgrNum = boatCards.getLeEgrNum();
        String ownerPhone = boatCards.getOwnerPhone();
        String ownerAddress = boatCards.getOwnerAddress();
        String cardDate = boatCards.getCardDate().toString();
        String cardDateEnd = boatCards.getCardDateEnd() == null ? null : boatCards.getCardDateEnd().toString();
        String note = boatCards.getNote();
        String engpwrmax = boatCards.getEngpwrmax();
        String boatVid = boatCards.getBoatVid().getName();
        String note2 = boatCards.getNote2();
        String section = boatCards.getSection().getSctName();
        String ticketDate = boatCards.getTicketDate() == null ? null : boatCards.getTicketDate().toString();

        List<Engines> engines = this.enginesDao.findAllByCardidCardid(boatCards.getCardid());

        return new InfoAboutBoatsReport(cardid, regNum, tiketNum, boatName, boatType, boatYear, boatVin, parkingPlace,
                saCategory, boatLength, boatWidth, boatHeight, bodyMaterial, boatPayload, passengersNum, serviceLife,
                engineNum, agentDoverennost, ownerType, ownerSurname, ownerName, ownerMidname, ownerBirthDate,
                ownerPersNum, ownerDocType, ownerDocNum, ownerDocDepartment, ownerDocDateIssue, leUnp, leName, leDocDepartment, leEgrNum,
                ownerPhone, ownerAddress, cardDate, cardDateEnd, note, engpwrmax, boatVid, note2, section, ticketDate, engines);
    }
}

and at the end I must get the class InfoAboutBoatsReport:

@JsonIgnoreProperties(ignoreUnknown = true)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class InfoAboutBoatsReport implements Serializable {
    String cardid;
    String regNum;
    String tiketNum;
    String boatName;
    String boatType;
    String boatYear;
    String boatVin;
    String parkingPlace;
    String saCategory;
    String boatLength;
    String boatWidth;
    String boatHeight;
    String bodyMaterial;
    String boatPayload;
    String passengersNum;
    String serviceLife;
    String engineNum;
    String agentDoverennost;
    String ownerType;
    String ownerSurname;
    String ownerName;
    String ownerMidname;
    String ownerBirthDate;
    String ownerPersNum;
    String ownerDocType;
    String ownerDocNum;
    String ownerDocDepartment;
    String ownerDocDateIssue;
    String leUnp;
    String leName;
    String leDocDepartment;
    String leEgrNum;
    String ownerPhone;
    String ownerAddress;
    String cardDate;
    String cardDateEnd;
    String note;
    String engpwrmax;
    String boatVid;
    String note2;
    String section;
    String ticketDate;

    List<Engines> enginesList;

}

Here is my .jrxml file (it is not all, but those fields that are not displayed are present here):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.20.1.final using JasperReports Library version 6.20.1-7584acb244139816654f64e2fd57a00d3e31921e  -->
<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="boatInfo" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="301a4691-f830-4257-8a66-d3b29fa5a257">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="GIMS"/>
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w1" value="479"/>
<property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w2" value="521"/>
<property name="com.jaspersoft.studio.property.dataset.dialog.DatasetDialog.sash.w1" value="544"/>
<property name="com.jaspersoft.studio.property.dataset.dialog.DatasetDialog.sash.w2" value="442"/>
<style name="Default" isDefault="true" pdfFontName="TIMES_NEW_ROMAN.TTF" pdfEncoding="Cp1251"/>
<subDataset name="enginesList" uuid="cb123b0b-4ea1-4234-bb7c-20ea886e122f">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter"/>
<queryString >
<![CDATA[SELECT engname, engvin,engpwr,engtype,date_reg,date_reg_end,
eng_prod_year
FROM gims.engines JOIN gims.boat_cards bc on bc.cardid = engines.cardid JOIN gims.boat_deals ba on bc.cardid = ba.cardid join gims.boat_arrests b on bc.cardid = b.cardid]]>
</queryString>
<field name="engname" class="java.lang.String">
<property name="com.jaspersoft.studio.field.name" value="engname"/>
<property name="com.jaspersoft.studio.field.label" value="engname"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[engname]]></fieldDescription>
</field>
<field name="engvin" class="java.lang.String">
<property name="com.jaspersoft.studio.field.name" value="engvin"/>
<property name="com.jaspersoft.studio.field.label" value="engvin"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[Заводской (идентификационный) номер двигателя]]></fieldDescription>
</field>
<field name="engpwr" class="java.lang.String">
<property name="com.jaspersoft.studio.field.name" value="engpwr"/>
<property name="com.jaspersoft.studio.field.label" value="engpwr"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[Предельная мощность двигателя]]></fieldDescription>
</field>
<field name="engtype" class="java.lang.Integer">
<property name="com.jaspersoft.studio.field.name" value="engtype"/>
<property name="com.jaspersoft.studio.field.label" value="engtype"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[Тип двигателя]]></fieldDescription>
</field>
<field name="dateReg" class="java.sql.Date">
<property name="com.jaspersoft.studio.field.name" value="date_reg"/>
<property name="com.jaspersoft.studio.field.label" value="date_reg"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[Дата постановки на учёт]]></fieldDescription>
</field>
<field name="dateRegEnd" class="java.sql.Date">
<property name="com.jaspersoft.studio.field.name" value="date_reg_end"/>
<property name="com.jaspersoft.studio.field.label" value="date_reg_end"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[Дата снятия с учёта]]></fieldDescription>
</field>
<field name="engProdYear" class="java.lang.String">
<property name="com.jaspersoft.studio.field.name" value="eng_prod_year"/>
<property name="com.jaspersoft.studio.field.label" value="eng_prod_year"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[Год производства двигателя]]></fieldDescription>
</field>
<field name="enginesList" class="java.util.List">
<property name="com.jaspersoft.studio.field.name" value="eng_prod_year"/>
<property name="com.jaspersoft.studio.field.label" value="eng_prod_year"/>
<property name="com.jaspersoft.studio.field.tree.path" value="engines"/>
<fieldDescription><![CDATA[Год производства двигателя]]></fieldDescription>
</field>
</subDataset>
<parameter name="cardid" class="java.lang.Integer"/>
<queryString>
<![CDATA[SELECT *
FROM gims.engines JOIN gims.boat_cards bc on bc.cardid = engines.cardid JOIN gims.boat_deals ba on bc.cardid = ba.cardid join gims.boat_arrests b on bc.cardid = b.cardid]]>
</queryString>
<componentElement>
<reportElement x="1" y="420" width="555" height="40" uuid="7ffc01ff-b13f-46de-9eca-a52dcef4d608"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="enginesList" uuid="17889024-a1dc-4356-be75-f56b4d88a3ef"/>
<jr:listContents height="40" width="555">
<textField>
<reportElement x="0" y="0" width="90" height="40" uuid="956c0c9b-5509-43d4-9119-2bbc1e6b6d50">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{enginesList}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="90" y="0" width="110" height="40" uuid="2227d4b1-d5d4-46b4-a870-fad4504135c9">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{engvin}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="50" height="40" uuid="8974f6a3-20e1-4a5a-94a2-84699bf6e9ff">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{engProdYear}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="250" y="0" width="60" height="40" uuid="effd126f-059b-41ca-85fe-3a1a4b5f248e">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{engpwr}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="310" y="0" width="70" height="40" uuid="df055a09-aaa7-43c4-905b-2c705757b85f">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{engtype}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="380" y="0" width="85" height="40" uuid="e9343045-0d18-4090-b698-f52861824058">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{dateReg}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="465" y="0" width="90" height="40" uuid="9ee1b015-6565-4b97-9829-d23d6a891005">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{dateRegEnd}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

答案1

得分: 1

enginesList字段需要成为主报表数据集的一部分(与cardidregNum等一起),而不是引擎子数据集的一部分。

然后,您需要将引擎列表作为数据源传递给子数据集:

<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="enginesList" uuid="17889024-a1dc-4356-be75-f56b4d88a3ef">
<dataSourceExpression>new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{enginesList})</dataSourceExpression>
</datasetRun>
</jr:list>
英文:

The enginesList field needs to be part of the main report dataset (along with cardid, regNum, etc) and not part of the engines subdataset.

And then you need to pass the engines list as a data source to the subdataset:

            &lt;jr:list xmlns:jr=&quot;http://jasperreports.sourceforge.net/jasperreports/components&quot; xsi:schemaLocation=&quot;http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd&quot;&gt;
&lt;datasetRun subDataset=&quot;enginesList&quot; uuid=&quot;17889024-a1dc-4356-be75-f56b4d88a3ef&quot;&gt;
&lt;dataSourceExpression&gt;new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{enginesList})&lt;/dataSourceExpression&gt;
&lt;/datasetRun&gt;

huangapple
  • 本文由 发表于 2023年6月1日 22:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定