如何打印报告

时间:2013-01-11 19:54:37

标签: java jasper-reports

我想打印 JR 报告。

我正在尝试此代码

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

但此代码无效。

1 个答案:

答案 0 :(得分:6)

您可以使用不同的方法直接打印Jasper Report,而不是显示“打印对话框”。

最简单的方法是:

    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);