0

我想打印一份JR报告。

我正在尝试这段代码

JasperPrint jp = new JasperFillManager().fillReport("billReport", billId, 
    "jdbc:mysql://localhost/kpc_hospital");  
JasperPrintManager.printReport(jp, true)

但这段代码不起作用。

4

1 回答 1

6

有不同的方法,您可以使用这些方法直接打印 Jasper 报告,而不是显示打印对话框。

最简单的方法是:

    Connection con;
    String url="jdbc:mysql://localhost/mydb";
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con=(Connection) DriverManager.getConnection(url,"root","");
    JasperReport jasperReport = JasperCompileManager.compileReport(fileName);
    JasperPrint print = JasperFillManager.fillReport(jasperReport, parameter, con);
    print.setPageHeight(100);
    print.setPageWidth(80);
    print.setOrientation(jasperReport.getOrientationValue().LANDSCAPE);
    JasperPrintManager.printReport(print,false);

你也可以使用这个:

    PrinterJob job = PrinterJob.getPrinterJob();
    int selectedService = 0;
    selectedService = 0;
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    printRequestAttributeSet.add(OrientationRequested.PORTRAIT);
    printRequestAttributeSet.add(MediaSizeName.ISO_A0); 
    MediaSizeName mediaSizeName = MediaSize.findMedia(64,25,MediaPrintableArea.MM);
    printRequestAttributeSet.add(mediaSizeName);
    printRequestAttributeSet.add(new Copies(1));
    JRPrintServiceExporter exporter;
    exporter = new JRPrintServiceExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasper_print);
    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);
    exporter.exportReport();
    job.print(printRequestAttributeSet);
于 2013-01-11T20:10:31.073 回答