2014-12-30 90 views
3

我使用JasperReports生成報告,並假設將其導出爲多種格式。但目前我只使用excel報告。Jasper報告缺少第一行

這是我的控制器代碼。

InputStream in = reportTemplate.getTemplate(reportInquery.getTemplateFile()); 
    JasperPrint print = JasperFillManager.fillReport(in, null, 
       new JRResultSetDataSource(reportDao.getReportData(reportInquery))); 
    resp = HttpConfig.setHeaders(reportInquery, resp); 
    Exporter exporter = reportOption.getRenderOptions(reportInquery.getFormat(), resp.getOutputStream(), print); 
    exporter.exportReport(); 

在我的配置工廠,Excel報表配置如下,調用getRenderOptions方法之後。

public Exporter exporterOptions(OutputStream outputStream, JasperPrint print) { 
    JRXlsExporter exporter = new JRXlsExporter(); 
    exporter.setExporterInput(new SimpleExporterInput(print)); 
    OutputStreamExporterOutput outputStreamExporterOutput = new SimpleOutputStreamExporterOutput(outputStream); 
    exporter.setExporterOutput(outputStreamExporterOutput); 
    SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration(); 
    configuration.setOnePagePerSheet(true); 
    configuration.setDetectCellType(true); 
    configuration.setMaxRowsPerSheet(100); 
    configuration.setRemoveEmptySpaceBetweenColumns(true); 
    configuration.setRemoveEmptySpaceBetweenRows(true); 
    exporter.setConfiguration(configuration); 
    return exporter; 
} 

要創建JRResultSetDataSource我已經在上面getReportData方法中使用的Oracle OracleCachedRowSet

public RowSet getReportData(ReportInqueryDTO reportInquery) { 
    try { 
     String query = getQueryById(reportInquery.getTemplateId()); 
     Map<String, String> params = new HashMap<>(); 
     params.put("fromDate", reportInquery.getFromDate()); 
     params.put("toDate", reportInquery.getToDate()); 
     params.put("appName", this.applicationName); 

     RowSet rowSet = namedParameterJdbcTemplate.query(query, params, new ResultSetExtractor<RowSet>() { 
      @Override 
      public RowSet extractData(ResultSet resultSet) throws SQLException, DataAccessException { 
       OracleCachedRowSet rs = new OracleCachedRowSet(); 
       rs.populate(resultSet); 
       return rs; 
      } 
     }); 


     rowSet.beforeFirst(); 
     return rowSet; 
    } catch (Exception exception) { 
     log.error("getReportData failed."); 
    } 
    return null; 
} 

我的問題是,EXCEL總是與第一條記錄,到哪指出的resultSet的光標丟失。 (例如:這裏rowSet.beforeFirst()意味着在第一個索引之前,我錯過了這個場景的第一條記錄。)

我被這個問題困住了幾天。我想知道這是不是一個碧玉問題,我該如何解決它。如果無法解析,我想知道如何在第一條記錄之前動態添加緩存的行集?

我有幾個jrxml文件,其中之一添加below.Every文件遵循相同的格式。

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library  version 6.0.0 --> 
<!-- 2014-12-30T15:20:10 --> 
<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="TTT_Call_Transfers_Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b61676f8-0366-4125-996c-7564d0f77eb4"> 
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="NewDataAdapter"/> 
<style name="Table_TH" mode="Opaque" backcolor="#F0F8FF"> 
    <box> 
     <pen lineWidth="0.5" lineColor="#000000"/> 
     <topPen lineWidth="0.5" lineColor="#000000"/> 
     <leftPen lineWidth="0.5" lineColor="#000000"/> 
     <bottomPen lineWidth="0.5" lineColor="#000000"/> 
     <rightPen lineWidth="0.5" lineColor="#000000"/> 
    </box> 
</style> 
<style name="Table_CH" mode="Opaque" backcolor="#BFE1FF"> 
    <box> 
     <pen lineWidth="0.5" lineColor="#000000"/> 
     <topPen lineWidth="0.5" lineColor="#000000"/> 
     <leftPen lineWidth="0.5" lineColor="#000000"/> 
     <bottomPen lineWidth="0.5" lineColor="#000000"/> 
     <rightPen lineWidth="0.5" lineColor="#000000"/> 
    </box> 
</style> 
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF"> 
    <box> 
     <pen lineWidth="0.5" lineColor="#000000"/> 
     <topPen lineWidth="0.5" lineColor="#000000"/> 
     <leftPen lineWidth="0.5" lineColor="#000000"/> 
     <bottomPen lineWidth="0.5" lineColor="#000000"/> 
     <rightPen lineWidth="0.5" lineColor="#000000"/> 
    </box> 
</style> 
<subDataset name="Dataset1" uuid="54db10cf-a696-4f7d-b642-96871feb617d"> 
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="NewDataAdapter"/> 
    <queryString language="SQL"> 
     <![CDATA[]]> 
    </queryString> 
    <field name="DATE_OF_TRANSFER" class="java.sql.Timestamp"/> 
    <field name="AGENT_ID" class="java.lang.String"/> 
    <field name="AGENT_NAME" class="java.lang.String"/> 
    <field name="CTALK_CONTACT_ID" class="java.lang.String"/> 
    <field name="ASSOCIATED_QUEUE_ID" class="java.lang.String"/> 
</subDataset> 
<queryString language="SQL"> 
    <![CDATA[]]> 
</queryString> 
<background> 
    <band splitType="Stretch"/> 
</background> 
<title> 
    <band height="63" splitType="Stretch"> 
     <staticText> 
      <reportElement x="160" y="16" width="222" height="30" uuid="d89e2553-d3fb-4360-b797-4f69b60938a5"/> 
      <text><![CDATA[TTT Call Transfers Report]]></text> 
     </staticText> 
    </band> 
</title> 
<detail> 
    <band height="253" splitType="Stretch"> 
     <componentElement> 
      <reportElement x="0" y="0" width="555" height="253" uuid="c229d6c3-18b7-4de2-a669-01e11d25c642"/> 
      <jr:table 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="Dataset1" uuid="e0262612-dc81-4d84-abf8-9b52609ba792"> 
        <datasetParameter name="REPORT_DATA_SOURCE"> 
         <datasetParameterExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></datasetParameterExpression> 
        </datasetParameter> 
       </datasetRun> 
       <jr:column width="40" uuid="a91061c3-7760-43d2-bdc1-091f0e712aaa"> 
        <jr:columnHeader style="Table_CH" height="30"> 
         <staticText> 
          <reportElement x="0" y="0" width="40" height="30" uuid="6afcdf24-958c-49f4-9ef3-b757ffd1e8de"/> 
          <text><![CDATA[DATE_OF_TRANSFER]]></text> 
         </staticText> 
        </jr:columnHeader> 
        <jr:columnFooter style="Table_CH" height="30"/> 
        <jr:detailCell style="Table_TD" height="30"> 
         <textField> 
          <reportElement x="0" y="0" width="40" height="30" uuid="84a5e4b7-30e0-409c-b2ab-e13c1dd02950"/> 
          <textFieldExpression><![CDATA[$F{DATE_OF_TRANSFER}]]></textFieldExpression> 
         </textField> 
        </jr:detailCell> 
       </jr:column> 
       <jr:column width="40" uuid="c7b5a8eb-04b6-4cd6-9328-e1e8ce11a2fc"> 
        <jr:columnHeader style="Table_CH" height="30"> 
         <staticText> 
          <reportElement x="0" y="0" width="40" height="30" uuid="d7cc290a-6918-4d47-8f55-b43761bee59b"/> 
          <text><![CDATA[AGENT_ID]]></text> 
         </staticText> 
        </jr:columnHeader> 
        <jr:columnFooter style="Table_CH" height="30"/> 
        <jr:detailCell style="Table_TD" height="30"> 
         <textField> 
          <reportElement x="0" y="0" width="40" height="30" uuid="b5f2b8bf-9132-4447-9209-f1fcb3b873c4"/> 
          <textFieldExpression><![CDATA[$F{AGENT_ID}]]></textFieldExpression> 
         </textField> 
        </jr:detailCell> 
       </jr:column> 
       <jr:column width="40" uuid="d2427379-2c72-4e33-9b46-ade8727dbed5"> 
        <jr:columnHeader style="Table_CH" height="30"> 
         <staticText> 
          <reportElement x="0" y="0" width="40" height="30" uuid="bdcb01c5-5073-4a10-b498-e2d1a2cf7c78"/> 
          <text><![CDATA[AGENT_NAME]]></text> 
         </staticText> 
        </jr:columnHeader> 
        <jr:columnFooter style="Table_CH" height="30"/> 
        <jr:detailCell style="Table_TD" height="30"> 
         <textField> 
          <reportElement x="0" y="0" width="40" height="30" uuid="e259ef09-f446-491c-88dd-f7694197e060"/> 
          <textFieldExpression><![CDATA[$F{AGENT_NAME}]]></textFieldExpression> 
         </textField> 
        </jr:detailCell> 
       </jr:column> 
       <jr:column width="40" uuid="81fe9b33-57aa-48b2-914b-e3e9b45cf6a1"> 
        <jr:columnHeader style="Table_CH" height="30"> 
         <staticText> 
          <reportElement x="0" y="0" width="40" height="30" uuid="ef5e379b-65de-4ea0-a5cb-13368efa16f0"/> 
          <text><![CDATA[CTALK_CONTACT_ID]]></text> 
         </staticText> 
        </jr:columnHeader> 
        <jr:columnFooter style="Table_CH" height="30"/> 
        <jr:detailCell style="Table_TD" height="30"> 
         <textField> 
          <reportElement x="0" y="0" width="40" height="30" uuid="5f49f1a9-379a-4d02-9e55-fc6a865aa828"/> 
          <textFieldExpression><![CDATA[$F{CTALK_CONTACT_ID}]]></textFieldExpression> 
         </textField> 
        </jr:detailCell> 
       </jr:column> 
       <jr:column width="40" uuid="f6e8b2a6-925b-41b5-9043-1ffa215871f1"> 
        <jr:columnHeader style="Table_CH" height="30"> 
         <staticText> 
          <reportElement x="0" y="0" width="40" height="30" uuid="b55c7599-a237-43f1-aff5-464717e3c917"/> 
          <text><![CDATA[ASSOCIATED_QUEUE_ID]]></text> 
         </staticText> 
        </jr:columnHeader> 
        <jr:columnFooter style="Table_CH" height="30"/> 
        <jr:detailCell style="Table_TD" height="30"> 
         <textField> 
          <reportElement x="0" y="0" width="40" height="30" uuid="0d2d53d0-1cb2-4d64-a09d-b0eeacca694d"/> 
          <textFieldExpression><![CDATA[$F{ASSOCIATED_QUEUE_ID}]]></textFieldExpression> 
         </textField> 
        </jr:detailCell> 
       </jr:column> 
      </jr:table> 
     </componentElement> 
    </band> 
</detail> 

在此先感謝。

+0

你能檢查RowSet返回是否包含所有檢索到的行嗎?實際上,問題在於你丟失RowSet填充中的行或Jasper DataSource中反映的RowSet? – StanislavL

+0

@StanislavL在註釋循環中它包含所有記錄。所以添加到Jasper Datasource後數據丟失了。 –

回答

0

正如勞拉在answer中提到的那樣,數據源中的記錄指針由接收它的每個元素遞增。由於我傳遞了結果集,我認爲沒有必要再將結果集作爲參數傳遞。最後,我決定在結果集的開頭添加一條空記錄。

getReportData方法在上面的問題,你可以通過下面的方法來做到這一點。

RowSet rowSet = namedParameterJdbcTemplate.query(query, params, new ResultSetExtractor<RowSet>() { 
      @Override 
      public RowSet extractData(ResultSet resultSet) throws SQLException, DataAccessException { 

       OracleCachedRowSet rs = new OracleCachedRowSet(); 
       rs.populate(resultSet); 

       // Have to add a empty row, because jasper is not displaying 
       // the first row of report. 
       rs.setReadOnly(false); 
       rs.beforeFirst(); 
       rs.moveToInsertRow(); 

       int numCol = rs.getMetaData().getColumnCount(); 
       for (int i = 1; i < numCol + 1; i++) { 
        // Add null inserted row to each column 
        rs.updateNull(i); 
       } 

       rs.insertRow(); 
       rs.beforeFirst(); 
       return rs; 
      } 
     }); 
6

請張貼您的jrxml文件。

問題可能不在您的代碼中。

在Jasper中,數據源中的記錄指針由接收它的每個元素遞增(例如,如果您在報表中有一個表並且將表的數據源設置爲數據源的數據源報告將跳過第一條記錄,如果是這種情況,則必須將報告中的數據源作爲參數傳遞給表)。

UPDATE:

1.Send您的數據源從服務器作爲一個參數,並填寫了不同的一個報告(可以爲空)。

2.在與您的bean集合相同類型的報表中新建一個參數,我們將其命名爲'DS1'。

3.將TableDatasource設置爲使用$ P {DS1}參數。

查看我對 How to show JRBeanCollectionDataSource data with help of Table component?的迴應。