1

这是我开发的 JasperReport 的一个片段,它使用嵌入在报告中的静态查询。

<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" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true">
     <property name="ireport.zoom" value="1.0"/>
     <property name="ireport.x" value="129"/>
     <property name="ireport.y" value="0"/>
     <parameter name="lp" class="java.lang.Integer"/>
     <parameter name="all_price" class="java.lang.Integer"/>
     <parameter name="all_amount" class="java.lang.Integer"/>
     <parameter name="all_total_price" class="java.lang.Integer"/>
     <queryString>
          <![CDATA[SELECT sanaf_number,sanaf,sales_id,sales.sanaf_id,customer_name,  price, total_price, addad, wahda, required_amount FROM sanaf,sales  where (sales.export='0')and(sanaf.sanaf_id=sales.sanaf_id) limit $P{lp}]]>
     </queryString>
     <field name="sales_id" class="java.lang.Integer"/>
     <field name="sanaf_id" class="java.lang.Integer"/>

现在我需要使用 aResultSet而不是静态查询。我用来执行报告的代码如下/

void showResports(){
  try{
    // OriginalExportOriginalExport 
    String reportName ="";

    if(export.equalsIgnoreCase("1")){
      reportName = "OriginalExport.jasper";
    } else if(export.equalsIgnoreCase("0")){
      reportName = "ExportReport4.jasper";
    }

    InputStream in =getClass().getResourceAsStream(reportName);

    if(in==null|| connection==null) return ;

    HashMap parameterMap = new HashMap();
    parameterMap.put("lp", new Integer(selectedNumber));

    if(export.equalsIgnoreCase("0")){
      int all_price=DatabaseManager.countTotalPriceForImport(selectedNumber);
      int all_amount=DatabaseManager.countAmountForImport(selectedNumber);
      int all_total_price=DatabaseManager.countTotalPrice(selectedNumber);

      parameterMap.put("all_price",new Integer(all_price));
      parameterMap.put("all_amount",new Integer(all_amount));
      parameterMap.put("all_total_price",new Integer(all_total_price));
    } else if(export.equalsIgnoreCase("1")){
      int all_requiredAmount=DatabaseManager.countAllRequiredAmount(selectedNumber);
      int all_given_amount=DatabaseManager.countAllGivenAmount(selectedNumber);
      parameterMap.put("all_requiredAmount",new Integer(all_requiredAmount));
      parameterMap.put("all_given_amount",new Integer(all_given_amount));
    }

    JasperPrint jp = JasperFillManager.fillReport(in, parameterMap, connection);
    System.out.println("Object of Jasper Print created");

    JRViewer jv = new JRViewer(jp);
    //Insert viewer to a JFrame to make it showable
    JFrame jf = new JFrame();
    jf.getContentPane().add(jv);
    jf.validate();
    jf.setVisible(true);
    jf.setSize(new Dimension(800,600));
    jf.setLocation(300,100);
    jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
  }catch(Exception e){
    e.printStackTrace();
  }
}// showReports
4

1 回答 1

3

如果结果集包含与您当前嵌入报告中的查询相同的字段,那么您需要queryString从报告中删除,然后更改:

JasperFillManager.fillReport(in, parameterMap, connection);

JasperFillManager.fillReport(in, parameterMap, new JRResultSetDataSource(resultSet));

resultSet数据的结果集在哪里。

唯一的复杂情况是结果集是否具有不同的字段名称,或者完全不同的字段。在这种情况下,您还需要调整报告中的字段名称。

于 2012-06-05T17:01:36.043 回答