我必須編寫用於生成包含圖像的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
是的,我得到了答案.. – Jayashri
你可以簡單地報告模板本身,如果你使用的是合適的報表編輯器處理圖像。 我更喜歡iReport。這是一個非常好的編輯器,如果你學會了這一點,你就可以做很多複雜的事情。 請參考以下鏈接:http://www.novell.com/documentation/zenworks113/pdfdoc/ireport-ultimate-guide/ireport-ultimate-guide.pdf –