作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的表格名称为 Mr_items,当我选择任何一个项目时,我需要在 jasper 报告中按组打印到 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);
}
}
此代码也用于选择打印机
private void PrintReportToPrinterSection(JasperPrint jp) throws JRException {
// getSectionPrint();
// 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(cashPrint.getText(), 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.getPrintService();
}
我尝试这个语法
SELECT item_sn, item_name, it_print,
IF($P{consolidate_loc}, concat(itemname, ', ', itemprice, ' ', itemprint ), Mr_items) AS groupField
FROM
GROUP BY groupField;
我无法将其纳入 jasper 报告中,因为我不了解 jasper 中的该领域,我是新人:)
最佳答案
终于找到解决办法了
////这个类用于将表单打印到打印机中
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);
}
}
关于java - 我如何在 jasper Reports 中按组将数据表打印到特定打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57205678/
我是一名优秀的程序员,十分优秀!