PRO*_*sta 10 java printing jasper-reports reporting-services
我想要的只是在没有用户选择打印机的情况下打印JasperReport.我搜索了它,但没有好的解决方案可行.这是我的代码的相关部分:
//compile to .jasper
String report = JasperCompileManager.compileReportToFile(sourceFileName);
//fill the report
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameter, dataSource);
//print the report
JasperPrintManager.printReport(jasperPrint, true);
Run Code Online (Sandbox Code Playgroud)
我想要选择一台打印机而不是简单的printReport.有没有办法做到这一点?
PRO*_*sta 10
这是它应该是这样的:
try {
String report = JasperCompileManager.compileReportToFile(sourceFileName);
JasperPrint jasperPrint = JasperFillManager.fillReport(report, para, ds);
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
printerJob.defaultPage(pageFormat);
int selectedService = 0;
AttributeSet attributeSet = new HashPrintServiceAttributeSet(new PrinterName(printerNameShort, null));
PrintService[] printService = PrintServiceLookup.lookupPrintServices(null, attributeSet);
try {
printerJob.setPrintService(printService[selectedService]);
} catch (Exception e) {
System.out.println(e);
}
JRPrintServiceExporter exporter;
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
printRequestAttributeSet.add(new Copies(1));
// these are deprecated
exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, printService[selectedService]);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printService[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.FALSE);
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这是在特定打印机上打印 jasper 报告的简单解决方案 创建选择打印机和打印报告的一种方法
private void PrintReportToPrinter(JasperPrint jp) throws JRException {
// TODO Auto-generated method stub
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
// printRequestAttributeSet.add(MediaSizeName.ISO_A4); //setting page size
printRequestAttributeSet.add(new Copies(1));
PrinterName printerName = new PrinterName("Microsoft XPS Document Writer", null); //gets printer
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(printerName);
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
exporter.exportReport();
}
Run Code Online (Sandbox Code Playgroud)
然后像这样调用这个方法
/* your code*/
Map parameters = new HashMap();
parameters.put("ckotid", kid);
try {
JasperDesign jsd = JRXmlLoader.load("report\\bill\\check_kot.jrxml");
JasperReport jr = JasperCompileManager.compileReport(jsd);
JasperPrint jp = JasperFillManager.fillReport(jr, parameters, con);
//JasperPrintManager.printPage(jp, 0, false);
//JasperPrint jp =reportEngine.fillReport() ;//it returns stream
PrintReportToPrinter(jp);//call method
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13872 次 |
| 最近记录: |