2013-01-11 180 views
0

我想打印JR报告。如何打印报告

我想这个代码

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

但是这个代码不工作。

+0

以何种方式是不工作? –

+0

我想直接打印jesper报告而不显示打印对话框,但此代码不起作用 –

+0

@MubashirHassan如果您将使用printReport(print,false),则不会显示打印对话框。 –

回答

6

有许多不同的方法可以用来直接打印Jasper Report,而不是显示Print Dialog。

简单的方法是:

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

上面的代码在本地主机中工作正常,但是当我托管应用程序时,打印对话框从不显示,并且不会将报告添加到打印机队列 –

+0

@Senthil:check this --- String url = “JDBC:MySQL的://本地主机/ MYDB”; –

+0

嗨伊姆兰。谢谢,但我没有连接数据库使用连接。我正在通过List并显示Jasper Report。我的意思是上述工作的第二部分在本地很好,但是当我将应用程序托管在服务器中并在浏览器中启动应用程序并尝试使用相同的应用程序时,它从未显示打印机对话框 –