java 使用JasperRunManager生成pdf报告

7rfyedvj  于 2022-12-21  发布在  Java
关注(0)|答案(2)|浏览(241)

我想用ejbql生成一个pdf格式的报告,但是我不知道怎么做。我试着做了以下的事情,但是发生了一个错误:

parameterMap.put(JRJpaQueryExecuterFactory.PARAMETER_JPA_ENTITY_MANAGER,    entityManager);
parameterMap.put("priority", "3");
try
{
JasperFillManager.fillReportToFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.jasper", parameterMap);
JasperRunManager.runReportToPdfFile("C:/Documents and Settings/MyDocuments/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf", parameterMap);
}
catch (JRException ex)
{
ex.printStackTrace();
}

Tehb错误为:

net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Documents and Settings\My Documents\NetBeansProjects\JiraMap\src\java\Reports\Test.pdf
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:101)
at net.sf.jasperreports.engine.JasperRunManager.runReportToPdfFile(JasperRunManager.java:93)
at Reporting.ReportBean.main(ReportBean.java:38)
Caused by: java.io.EOFException

更新

我使用过这段代码(我没有使用任何参数),但报告总是空的:

JasperReport report = JasperCompileManager.compileReport("C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.jrxml");
JasperPrint print = JasperFillManager.fillReport(report,null);
JasperExportManager.exportReportToPdfFile(print,"C:/Documents and Settings/My Documents/NetBeansProjects/JiraMap/src/java/Reports/Test.pdf");

包含聚合函数计数的报表(我曾尝试使用别名作为数值br,但没有成功):

<?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="Test" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20"  topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="priority" class="java.lang.String">
<defaultValueExpression><![CDATA[]]></defaultValueExpression>
</parameter>

<queryString language="ejbql">
    <![CDATA[select count(j)  from Jiraissue j where j.priority='3']]>
</queryString>
    <field name="count(j)" class="java.lang.Long"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
        <band height="79" splitType="Stretch">

    </band>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="84" splitType="Stretch">
        <staticText>
            <reportElement x="28" y="0" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[count(j)]]></text>
        </staticText>

    </band>
</columnHeader>
<detail>
    <band height="125" splitType="Stretch">
        <textField>
            <reportElement x="50" y="11" width="100" height="20"/>
            <textElement/>
            <textFieldExpression class="java.lang.Long">  <![CDATA[$F{count(j)}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<columnFooter>
    <band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

错误为:

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : count(j)
at    net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDat    aSource.java:123)
at   net.sf.jasperreports.engine.data.JRJpaDataSource$PropertyReader.getValue(JRJpaDataSource.java:206)
at  net.sf.jasperreports.engine.data.JRJpaDataSource.getFieldValue(JRJpaDataSource.java:131)
at  net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:821)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:785)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1482)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:126)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:946)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:118)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:435)
at Reporting.ReportBean.main(ReportBean.java:41)
Caused by: java.lang.NoSuchMethodException: Unknown property 'count'+ on bean class 'class java.lang.Long'
[EL Info]: 2011-06-29 11:05:12.606--ServerSession(27055962)--file:/C:/Documents and     Settings/a497165/My Documents/NetBeansProjects/JiraMap/build/web/WEB-INF/classes/_JiraMapPU logout successful
at   org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:624)
at org.apache.commons.beanutils.PropertyUtilsBean.getMappedProperty(PropertyUtilsBean.java:570)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:758)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:837)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:426)
at  net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 10 more
v2g6jxz6

v2g6jxz61#

这是我用JasperPrint生成的代码的一部分;并将文件发送回浏览器响应。虽然这使用了JDBC,而不是EJBQL。但代码的JasperReports部分是相同的。

FileResolver fileResolver = new FileResolver() {
                                            @Override
                                            public File resolveFile(String fileName) {
                                               return new File(getServletContext().getRealPath("/") + fileName);
                                            }
                                         };

             Map<String, Object> parameters = new HashMap<String, Object>();
             parameters.put("REPORT_FILE_RESOLVER", fileResolver);

             ...... // blah blah

             JasperPrint jrprint = JasperFillManager.fillReport(input, parameters,  conn);

             byte[] reportBytes;

             jrprint.setName("MyFile." + fileFormat);

             response.setContentType("APPLICATION/OCTET-STREAM");
             String disHeader = "Attachment;Filename=\"MyFile.";

             ServletOutputStream os = response.getOutputStream();

                response.setHeader("Content-Disposition", disHeader);

                reportBytes = JasperExportManager.exportReportToPdf(jrprint);
                os.write(reportBytes);
e3bfsja2

e3bfsja22#

您需要传递所需的所有参数,如下所示:

JasperPrint jrprint = JasperFillManager.fillReport(input, parameters,  connections);

相关问题