//: com/huaheng/pc/common/JasperPrint/print.java package com.huaheng.pc.common.JasperPrint; import com.huaheng.framework.web.domain.AjaxResult; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** A class comment */ @Configuration public class Print { @Resource private DataSource dataSource; @Resource private HttpServletResponse response; public void jasperPrint (Integer[] ids, String prefix) { ServletOutputStream output = null; Connection connection = null; try { /*加载模板-begin*/ // 获取文件流 硬编码稍后再改 // 获取前缀 的"/"分割的最后一个字符串为文件名(不包含后缀) String jasperName = prefix.split("/")[prefix.split("/").length - 1]; // this.getClass().getSimpleName().matches("(.+)Controller"); // 错误不能匹配当前文件名该段代码是封装 不用重复找轮子 防止代码冗余 final ClassPathResource resource = new ClassPathResource("jaspers/" + jasperName + ".jasper"); // 要打印的文件的ClassPathResource √ InputStream in = resource.getInputStream(); // 获取输入流 JasperReport jasperReport = (JasperReport) JRLoader.loadObject(in); // 利用输出和输入流以及JasperReport对象提供的方法来返回一个pdf文件 JasperReport对象传入文件 /*加载模板-end*/ /*反射获取实体类的打印机名称-begin*/ /* *//* 获取类 arg:类名 jasperName 类名的String 通过反射可以获取该类和该类的成员对象 *//* // 需要把/换成. // jasperName首字母大写 Class<?> clazz = Class.forName("com.huaheng.pc." + prefix.replaceAll("/", ".") + ".domain." + jasperName.substring(0, 1).toUpperCase() + jasperName.substring(1)); Field printName = clazz.getDeclaredField("printName"); // 需不需要注意安全性的问题 printName.setAccessible(true); // 要不要私有访问 String printStr = (String) printName.get(clazz.newInstance()); // SUCCESS PrintService[] pss = PrinterJob.lookupPrintServices(); // 获取所有打印机服务 PrintService ps = null; // 不能直接通过""选中该参数 for (PrintService printService : pss) { String sps = printService.toString(); // 判断打印机名称是否相同 if(sps.equalsIgnoreCase("Win32 Printer : " + printStr))// ps = printService; // 判断相同之后跳出循环 }*/ /*反射获取实体类的打印机名称-end*/ /*配置打印模板信息-begin*/ // map传入打印参数 Map<String, Object> param = new LinkedHashMap<>(); // 存值 插入值 // 进行打印的模板 List<JasperPrint> jasperPrints = new ArrayList<>(); // 查询 赋值 connection = dataSource.getConnection(); for (Integer id : ids) if(id != null) { param.put(jasperName + "Id", id); // // 只能一次一次调用参数覆盖!` JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, connection); // 填充jasperReport编译报表 报告参数paramters{(map) mysql连接 // 打印 内置对象打印方法调用打印机进行打印 // 调用是出错 打印单个调用打印窗口选项 /* 必须根据要答应的模板自动选者就需要选择打印机 */ // JasperPrintManager.printReport(jasperPrint, false); // jasperReport false为直接打印不要调用打印机选择 jasperPrints.add(jasperPrint); } /*配置打印机信息-end*/ // JRAbstractExporter je = new JRPrintServiceExporter(); // 设置打印配置 /* je.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints); je.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, ps); // arg2:null 导出为pdf je.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, false); je.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, false);*/ // 打印 // je.exportReport(); // pdf预览 /*jasper模板pdf预览-begin*/ // 打印PDF对象 JRAbstractExporter exporter = new JRPdfExporter(); // 下面是固定输出数据源 output = response.getOutputStream(); exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints); // exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, PrinterJob.lookupPrintServices()[6]); // arg2:null 导出为pdf void exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, output); // 序列化jasper对象 exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); // exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, false);void // exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, false); void exporter.exportReport(); // new JRPrintServiceExporter().exportReport(); } catch (IOException e) { AjaxResult.error(e.toString()); } catch (JRException e) { AjaxResult.error(e.toString()); } catch (SQLException e) { AjaxResult.error(e.toString()); } /*catch (ClassNotFoundException e) { AjaxResult.error(e.toString()); }*/ /*catch (NoSuchFieldException e) { AjaxResult.error(e.toString()); }*/ /*catch (IllegalAccessException e) { AjaxResult.error(e.toString()); }*/ /*catch (InstantiationException e) { e.printStackTrace(); } //*/ finally { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } try { output.flush(); } catch (IOException e) { e.printStackTrace(); } try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } ///:~