我如何在 jasper Reports 中按组将数据表打印到特定打印机?

我有表是名称 Mr_items 我需要当我选择任何我需要打印的项目时,在碧玉报告中按组打印到 it_print 列


item_sn  |  item_name  | it_print

---------------------------------

1        |  pepsi      | drinks


2        |  sandwiches | foods


3        |  water      | drinks


4        |  burger     | foods

我尝试通过此代码打印


///print invoice cashier

    public void PrintInvoiceSection() throws JRException, IOException {


        try {


            /////////print///////////////////

            HashMap parameters = new HashMap();


            parameters.put("nameilll", cashier_name.getText().trim());

            parameters.put("datevist", sysdate_invc.getText().trim());

            parameters.put("timevist", systime_invc.getText().trim());

            parameters.put("invoice_type", InvoiceType.getText());

            parameters.put("invoiceno", inv_id);

            parameters.put("cus_name", cusName.getText().trim());

            parameters.put("cus_mobile", cusMobile.getText().trim());

            parameters.put("cus_address", cusAddrss.getText());

            parameters.put("inv_remark", customerBehestRemark.getText());

            parameters.put("inv_remark", invoiceRemark.getText());

            InputStream ul = getClass().getResourceAsStream("/printer/Printsection.jasper");

            java.util.Vector collection = new java.util.Vector();

            collection = new Vector(itemslist);

            JasperPrint jasperPrint = JasperFillManager.fillReport(ul, parameters, new JRBeanCollectionDataSource(collection));

            JasperViewer jv = new JasperViewer(jasperPrint, false);

            jv.setVisible(false);

            PrintReportToPrinterSection(jasperPrint);

        } catch (JRException ex) {

            Logger.getLogger(SalesinterfaceController.class.getName()).log(Level.SEVERE, null, ex);

        }

    }

我不能将它包含在 jasper 报告中,因为我不知道 jasper 的领域我是新手:)


qq_花开花谢_0
浏览 199回答 1
1回答

HUWWW

最后我找到了解决方案////此类用于将表单打印到打印机中public void PrintReportToPrinterSection(JasperPrint jp, String pp) throws PrinterException {try{        // 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(pp, null); //gets printer         PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();        printServiceAttributeSet.add(printerName);            PrinterJob printerJob = PrinterJob.getPrinterJob();            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);            PrintService selectedService = null;            if(services.length != 0 || services != null)            {                for(PrintService service : services){                    String existingPrinter = service.getName().toLowerCase();            Object selectedPrinter = pp;                    if(existingPrinter.equals(selectedPrinter))                    {                        selectedService = service;                        break;                    }                }                if(selectedService != null)                {                    printerJob.setPrintService(selectedService);                    boolean printSucceed = JasperPrintManager.printReport(pp, false);                }}        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();} catch (JRException e) {    System.out.println(e);}    }///打印部分打印机表单/// 然后我将类调用到 printSection 表单类中 /// 然后我执行 while 循环以按打印机名称逐行获取每一行public void PrintInvoiceSection() throws JRException, SQLException, PrinterException {        try {            String sql2 = "select pr_id as printId, pr_printer as printName from invoices_items where tr_inv_no='" + inv_id + "' Group BY pr_id";            rs = con.createStatement().executeQuery(sql2);            while (rs.next()) {                int prind = rs.getInt("printId");                String prnam = rs.getString("printName");                JasperDesign jasperdesign = JRXmlLoader.load("C:\\Users\\Wael\\Documents\\NetBeansProjects\\resrr\\resturnopos\\src\\printer\\Printsection.jrxml");                String sql = "SELECT resturno.mr_printsection.pr_section,\n" +"   resturno.invoices_items.tr_it_name,\n" +"       resturno.invoices_items.tr_date,\n" +"       resturno.invoices_items.tr_inv_issued,\n" +"   resturno.invoices_items.tr_it_price,\n" +"   resturno.invoices_items.tr_vat,\n" +"   resturno.invoices_items.tr_it_qty,\n" +"   resturno.invoices_items.tr_it_tot,\n" +"   resturno.invoices_items.tr_inv_no,\n" +"   resturno.mr_invoicetyp.inv_name1,\n" +"   resturno.invoices_items.inv_typ_id,\n" +"   resturno.invoices_items.tr_it_name,\n" +"   resturno.invoices_items.tr_it_price,\n" +"   resturno.invoices_items.tr_vat,\n" +"   resturno.invoices_items.tr_it_tot,\n" +"   resturno.invoices_items.tr_it_qty,\n" +"   resturno.mr_invoicetyp.inv_name1,\n" +"   resturno.mr_invoicetyp.inv_name2,\n" +"   resturno.mr_printsection.pr_section,\n" +"   resturno.invoices_items.pr_id,\n" +"   resturno.invoices_items.pr_printer\n" +"FROM resturno.invoices_items\n" +"   INNER JOIN resturno.mr_invoicetyp ON \n" +"    resturno.mr_invoicetyp.inv_typ_id = resturno.invoices_items.inv_typ_id \n" +"   INNER JOIN resturno.mr_printsection ON \n" +"    resturno.mr_printsection.pr_id = resturno.invoices_items.pr_id \n" +"WHERE  \n"                    + "  resturno.invoices_items.tr_inv_no = '" + inv_id + "' \n"                    + "  AND resturno.invoices_items.pr_id = '" + prind + "'"                    + "  AND resturno.invoices_items.pr_printer = '" + prnam + "'";                JRDesignQuery jrq = new JRDesignQuery();                jrq.setText(sql);                jasperdesign.setQuery(jrq);                JasperReport jsp = JasperCompileManager.compileReport(jasperdesign);                JasperPrint jasperPrint = JasperFillManager.fillReport(jsp, new HashMap(), con);//              List jpList = new ArrayList();//              jpList.add(j.loadObjectFromFile("build/reports/Report1.jrprint"));                 JasperViewer jv = new JasperViewer(jasperPrint, false);                jv.setVisible(true);                PrintReportToPrinterSection(jasperPrint, prnam);            }        } catch (JRException ex) {            System.out.println(ex);        }    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java