JasperReports API:如何传递多个参数进行报告

时间:2013-09-10 05:18:35

标签: java jasper-reports

我正在使用 JasperReports API 从我的 Java 应用程序进行打印。我正在打印这样的报告:

public void prints(String billNo, String fileName, String outFileName, String perameterName) {
    HashMap hm = new HashMap();
    hm.put(perameterName, billNo);
    //Here parameterName is the parameter in JasperReport.
    // billNo is the value from my java application.

    try {
        conn = new connection().db();
        PrinterJob job = PrinterJob.getPrinterJob();
        /* Create an array of PrintServices */
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService default_service = PrintServiceLookup.lookupDefaultPrintService();
        int selectedService = 0;
        /* Scan found services to see if anyone suits our needs */
        for (int i = 0; i < services.length; i++) {
            if (services[i].getName().toUpperCase().equals(default_service.getName().toUpperCase())) {
                selectedService = i;
            }
        }
        job.setPrintService(services[selectedService]);
        PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
        printRequestAttributeSet.add(MediaSizeName.ISO_A4);
        printRequestAttributeSet.add(new Copies(1));
        JRPrintServiceExporter exporter;
        exporter = new JRPrintServiceExporter();

        JasperPrint print = JasperFillManager.fillReport(fileName, hm, conn);
        JRExporter exporterr = new JRPdfExporter();
        exporterr.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
        exporterr.setParameter(JRExporterParameter.JASPER_PRINT, print);

        exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes());
        exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
        exporter.exportReport();
        exporterr.exportReport();
    } catch (JRException e) {
        JOptionPane.showMessageDialog(null, e);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Printer not connected. Check power cable.");
    }
} 

我用来在其他类中调用此方法 但是如何传递多个参数和多个值来报告? 例如查询:

"select * from table1 where date>"+thisDate+" and date<"+thatDate+" "

如何从 Java 应用程序发送 thisDate thatDate

1 个答案:

答案 0 :(得分:0)

由于您使用的是HashMap,因此您可以存储许多值。你可以这样做:

HashMap hm = new HashMap();  
hm.put(perameterName, billNo);
hm.put("thisDate", thisDate);
hm.put("theDate", theDate);
hm.put("anotherParam", paramValue);

如果您不希望您的方法传递如此多的键和值作为参数,您只需将“hm”对象传递给您的方法:

public void prints(  String fileName, String outFileName, HashMap hm){