为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

qq_花开花谢_0 2023-04-19 16:09:13
我有表是名称 Mr_items 我需要当我选择任何我需要打印的项目时,在碧玉报告中按组打印到 it_print 列item_sn  |  item_name  | it_print---------------------------------1        |  pepsi      | drinks2        |  sandwiches | foods3        |  water      | drinks4        |  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 的领域我是新手:)
查看完整描述

1 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

最后我找到了解决方案


////此类用于将表单打印到打印机中


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);

        }

    }



查看完整回答
反对 回复 2023-04-19
  • 1 回答
  • 0 关注
  • 191 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信