热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

Java中jasperReport实现动态列打印的实现代码

这篇文章主要介绍了Java中jasperReport实现动态列打印的实现代码的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下

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 reportDataList = new ArrayList(); 
 
  for (int i = 0; i 
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(); 
 } 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


推荐阅读
author-avatar
光陆光陆光陆
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有