0

我正在使用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应用程序发送thisDatethatDate

4

1 回答 1

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){
于 2013-09-10T06:18:39.990 回答