2012-11-06 97 views
3

我必須編寫用於生成包含圖像的Jasper報告的代碼。 我想發送碧玉報告給打印機。 我嘗試了代碼:如何以編程方式打印Jasper報告

String Report = "C:\\Template\\"+file_name+".jrxml";//my Jasper report file 
    JasperPrint print = JasperFillManager.fillReport(Report,null,con); 
    PrinterJob job = PrinterJob.getPrinterJob(); 
    /* Create an array of PrintServices */ 
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); 
    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().contains("Your printer's name")) 
     { 
      /*If the service is named as what we are querying we select it */ 
       selectedService = i; 
     } 
    } 
    job.setPrintService(services[selectedService]); 
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); 
    MediaSizeName mediaSizeName = MediaSize.findMedia(4,4,MediaPrintableArea.INCH); 
    printRequestAttributeSet.add(mediaSizeName); 
    printRequestAttributeSet.add(new Copies(1)); 
    JRPrintServiceExporter exporter; 
    exporter = new JRPrintServiceExporter(); 
    exporter.setParameter(JRExporterParameter.JASPER_PRINT, print); 
    /* We set the selected service and pass it as a paramenter */ 
    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.TRUE); 
    exporter.exportReport(); 

,但它給我的錯誤是:

net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Template\Alcon_Ele_Temp1.jrxml 
+0

是的,我得到了答案.. – Jayashri

+0

你可以簡單地報告模板本身,如果你使用的是合適的報表編輯器處理圖像。 我更喜歡iReport。這是一個非常好的編輯器,如果你學會了這一點,你就可以做很多複雜的事情。 請參考以下鏈接:http://www.novell.com/documentation/zenworks113/pdfdoc/ireport-ultimate-guide/ireport-ultimate-guide.pdf –

回答

0

您需要傳遞已編譯的文件而不是jrxml文件。但您可以編譯JRXML文件中這樣說:

JasperReport report = JasperCompileManager.compileReport("C:\\Template\\"+file_name+".jrxml"); 

然後填寫報告:

JasperPrint print = JasperFillManager.fillReport(report,null,con); 
-1

問題的if語句。你把/ *標記在你的if語句中。

還要檢查該文件是有C:/Templates/filename.jrxml