Java 中jasperReport实现动态列打印的实现代码
以下代码中注释说明很清楚,希望能帮助到大家,大家参考下。
示例代码:
public ActionResult projectPrint() { String[] printValue = null; // 从页面中获得要查询的字段 String reqPrintValue = getRequest().getParameter("printValue"); // 没有选择则默认全打印 if (null == reqPrintValue || StringUtils.isEmpty(reqPrintValue)) { printValue = new String[] { "pnumber", "pname", "pdepart", "pdecision", "pthrow", "plastmonth", "pfund", "ploan" }; } else { printValue = reqPrintValue.split(","); } // 查询统计数据 List projectList = getEntityManager().queryPrintProjectInfo(printValue); // 将数据转换为Map对象,换化成Map对象 List
public static void export(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request, String type) throws IOException { if (EXCEL_TYPE.equals(type)) { exportExcel(jasperPrint, response, request); } else if (PDF_TYPE.equals(type)) { exportPDF(jasperPrint, response, request); } else if (HTML_TYPE.equals(type)) { exportHTML(jasperPrint, response, request); } else { exportPrint(jasperPrint, response, request); } }
public static void exportExcel(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request) throws IOException { response.setContentType("application/vnd.ms-excel"); String filename = DownloadHelper.encodeFilename("未命名.xls", request); response.setHeader("Content-disposition", "attachment;filename=" + filename); ServletOutputStream ouputStream = response.getOutputStream(); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); try { exporter.exportReport(); } catch (JRException e) { e.printStackTrace(); } ouputStream.flush(); ouputStream.close(); } public static void exportPDF(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request) throws IOException { response.setContentType("application/pdf"); String filename = DownloadHelper.encodeFilename("未命名.pdf", request); response.setHeader("Content-disposition", "attachment;filename=" + filename); ServletOutputStream ouputStream = response.getOutputStream(); try { JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream); } catch (JRException e) { e.printStackTrace(); } ouputStream.flush(); ouputStream.close(); }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!