我正在使用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?