java 如何以编程方式打印 Jasper 报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13245964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:00:49  来源:igfitidea点击:

How to print Jasper report programmatically

javaprintingjasper-reports

提问by Jayashri

I have to write a code for generating the Jasper report which containing the images. I want to send the jasper report to printer. I tried a Code:

我必须编写一个代码来生成包含图像的 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();

but It gives me error as:

但它给了我错误:

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

回答by Jimmy

You must load a jasper print no a jasper xml. There is a page that maybe can help you http://jasperreports.sourceforge.net/sample.reference/printservice/index.html

您必须加载 jasper 打印而不是 jasper xml。有一个页面可能可以帮助您http://jasperreports.sourceforge.net/sample.reference/printservice/index.html

回答by Sanket Choudhury

1st of all you create the one controller in your print button path url and create the one method.

首先,您在打印按钮路径 url 中创建一个控制器并创建一个方法。

@RequestMapping(value = "/Print_url_link", method = RequestMethod.GET) public ModelAndView methodName(Model m, @RequestParam(name = "para1") String para1) {

@RequestMapping(value = "/Print_url_link", method = RequestMethod.GET) public ModelAndView methodName(Model m, @RequestParam(name = "para1") String para1) {

    Map<String, Object>     inputData       =   new HashMap<String, Object>();
    Map<String, Object>     returnData      =   new HashMap<String, Object>();
    Map<String, Object>     jasperParameterMap  =   new HashMap<String, Object>();

    putData.put("productionID", productionID);

    returnData = serviceName.methodName(inputData);    

//create the serve and service Impl and Create the list of the what you want the list use it. and below list I add 0 index in list because in jasper report always list Start from the index 1.

//创建服务和服务Impl并创建您想要列表使用它的列表。在下面的列表中,我在列表中添加了 0 个索引,因为在 jasper 报告中总是从索引 1 开始列出。

    List<Record> list = (List<Record>) returnData.get("RecordList");
    list.add(0, new Record());

JRDataSource JRdataSource = new JRBeanCollectionDataSource(list);

JRDataSource JRdataSource = new JRBeanCollectionDataSource(list);

// JRDataSource interface represents the abstract representation of a JasperReports data source. All data source types must implement this interface. A data source implementation that wraps a collection of JavaBean objects.

// JRDataSource 接口表示 JasperReports 数据源的抽象表示。所有数据源类型都必须实现此接口。包装 JavaBean 对象集合的数据源实现。

It is common to access application data through object persistence layers like EJB, Hibernate, or JDO. Such applications may need to generate reports using data they already have available as arrays or collections of in-memory JavaBean objects.

通常通过 EJB、Hibernate 或 JDO 等对象持久层访问应用程序数据。此类应用程序可能需要使用它们已作为内存中 JavaBean 对象的数组或集合可用的数据生成报告。

    jasperParameterMap.put("ListObjectName", JRdataSource); 


    ModelAndView modelAndView = new ModelAndView("jrxmlviewName", jasperParameterMap);  


    return modelAndView;

}

}

Now the Jasper VIew XML Mapping

现在的 Jasper View XML 映射

<bean id="jrxmlviewName"
    class="org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView"
    p:url="classpath:com/report/jrxmlviewName.jrxml" />

And Now In the jrxml use the parameters to view the data in the jasper report. and sorry for the english

现在在 jrxml 中使用参数查看 jasper 报告中的数据。对不起英语

回答by John Alexander Betts

You need to pass the compiled file and not the jrxml file. You can compile your jrxml file in this way:

您需要传递编译后的文件而不是 jrxml 文件。你可以用这种方式编译你的 jrxml 文件:

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

And then fill the report:

然后填写报告:

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

回答by Aqeel Haider

Problem with the if statement. you put /* sign in your if statement.

if 语句有问题。你把 /* 登录到你的 if 语句中。

Also check that the file is there C:/Templates/filename.jrxml

还要检查文件是否存在 C:/Templates/filename.jrxml