首页 Java java教程 Java中jasperReport实现动态列打印的实例

Java中jasperReport实现动态列打印的实例

Sep 18, 2017 am 09:41 AM
java 打印

这篇文章主要介绍了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<Object[]> projectList = getEntityManager().queryPrintProjectInfo(printValue); 
 
  // 将数据转换为Map对象,换化成Map对象 
  List<Map> reportDataList = new ArrayList<Map>(); 
 
  for (int i = 0; i < projectList.size(); i++) { 
   Object[] personStr = projectList.get(i); 
   Map reportData = new HashMap(); 
   for (int j = 0; j < personStr.length; j++) { 
    reportData.put("field_" + j, String.valueOf(personStr[j])); 
   } 
   reportDataList.add(reportData); 
  } 
 
  int columCount = 0;// 数据列 
  int fieldCount = 0;// 字段列数(因为pname比较长所以想让pname比其它的列长些,故设计这个变量) 
  int pnameCount = -1;// 记录下pname的序号 
  for (int i = 0; i < printValue.length; i++) { 
   // pthrow下面有两列 
   if ("pthrow".equals(printValue[i])) { 
    columCount = columCount + 2; 
    fieldCount = fieldCount + 2; 
    // ploan下面也有两列 
   } else if ("ploan".equals(printValue[i])) { 
    columCount = columCount + 2; 
    fieldCount = fieldCount + 2; 
    // 故意让pname也占两列 
   } else if ("pname".equals(printValue[i])) { 
    pnameCount = i;// 记录下pname的序号 
    columCount = columCount + 1; 
    fieldCount = fieldCount + 2; 
   } else { 
    // 其它的列都占一个单位 
    columCount = columCount + 1; 
    fieldCount = fieldCount + 1; 
   } 
  } 
 
  InputStream is = null; 
  try { 
   // 从资源文件中读取报表 
   is = this.getClass().getResourceAsStream("/reports/project.jrxml"); 
   JasperDesign jasperDesign = (JasperDesign) JRXmlLoader.load(is); 
 
   Map styleMap = jasperDesign.getStylesMap(); 
   // column header 对应的样式 
   JRDesignStyle theaderStyle = (JRDesignStyle) styleMap.get("theader"); 
   // column detail 对应的样式 
   JRDesignStyle tbodyStyle = (JRDesignStyle) styleMap.get("tboby"); 
   // pagefoot 对应的样式 
   JRDesignStyle tfootStyle = (JRDesignStyle) styleMap.get("tfoot"); 
 
   int _START_X_ = 20;// x轴的起始位置 
   int startX = _START_X_; // x轴的起始位置 
   // 单列的宽度 
   // 535是jasepreReport报表column最大的宽度 
   int columnWidth = 535 / fieldCount; 
   // 20,24,15是报表中已设置的,一定与之相同 
   final int columnHeadBandHeight = 20; 
   final int detailHeight = 24; 
   final int pagefootHeight = 15; 
 
   // 设置报表字段 
   for (int idx = 0; idx < columCount; idx++) { 
    JRDesignField field = new JRDesignField(); 
    field.setName("field_" + idx); 
    field.setValueClass(java.lang.String.class); 
    jasperDesign.addField(field); 
   } 
 
   JRDesignBand columnHeadBand = (JRDesignBand) jasperDesign.getColumnHeader(); 
   // 绘制表头 
   for (int idx = 0; idx < printValue.length; idx++) { 
    if ("pnumber".equals(printValue[idx])) { 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(2 * columnHeadBandHeight); 
     staticText.setText("序号"); 
     columnHeadBand.addElement(staticText); 
     startX += columnWidth; 
    } else if ("pname".equals(printValue[idx])) { 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     // 项目名称的宽度是其它的宽度的2倍 
     staticText.setWidth(columnWidth * 2); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(2 * columnHeadBandHeight); 
     staticText.setText("项目名称"); 
     columnHeadBand.addElement(staticText); 
     startX += columnWidth * 2; 
    } else if ("pdepart".equals(printValue[idx])) { 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(2 * columnHeadBandHeight); 
     staticText.setText("部门"); 
     columnHeadBand.addElement(staticText); 
     startX += columnWidth; 
    } else if ("pdecision".equals(printValue[idx])) { 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(2 * columnHeadBandHeight); 
     staticText.setText("已决策"); 
     columnHeadBand.addElement(staticText); 
     startX += columnWidth; 
    } else if ("pthrow".equals(printValue[idx])) { 
     // 投审会下面有两列 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth * 2); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("投审会"); 
     columnHeadBand.addElement(staticText); 
 
     staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     columnHeadBand.addElement(staticText); 
     staticText.setWidth(columnWidth); 
     staticText.setY(columnHeadBandHeight); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("12月初"); 
 
     staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     columnHeadBand.addElement(staticText); 
     staticText.setWidth(columnWidth); 
     staticText.setY(columnHeadBandHeight); 
     staticText.setX(startX + columnWidth); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("12月中"); 
     columnHeadBand.addElement(staticText); 
     startX += 2 * columnWidth; 
    } else if ("plastmonth".equals(printValue[idx])) { 
     // 投决会下面有一列 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("投决会"); 
     columnHeadBand.addElement(staticText); 
 
     staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     columnHeadBand.addElement(staticText); 
     staticText.setWidth(columnWidth); 
     staticText.setY(columnHeadBandHeight); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("12月下"); 
     columnHeadBand.addElement(staticText); 
     startX += columnWidth; 
    } else if ("pfund".equals(printValue[idx])) { 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(2 * columnHeadBandHeight); 
     staticText.setText("基金投资额"); 
     columnHeadBand.addElement(staticText); 
     startX += columnWidth; 
    } else if ("ploan".equals(printValue[idx])) { 
     // 投贷协同额下面有两列 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth * 2); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("投贷协同额"); 
     columnHeadBand.addElement(staticText); 
 
     staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     columnHeadBand.addElement(staticText); 
     staticText.setWidth(columnWidth); 
     staticText.setY(columnHeadBandHeight); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("金额"); 
 
     staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     columnHeadBand.addElement(staticText); 
     staticText.setWidth(columnWidth); 
     staticText.setY(columnHeadBandHeight); 
     staticText.setX(startX + columnWidth); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("入库情况"); 
     columnHeadBand.addElement(staticText); 
     startX += 2 * columnWidth; 
    } 
   } 
 
   // 绘制Detail部门 
   startX = _START_X_; 
   JRDesignBand columnDetailBand = (JRDesignBand) jasperDesign.getDetail(); 
   for (int idx = 0; idx < columCount; idx++) { 
    JRDesignTextField textField = new JRDesignTextField(); 
    textField.setStretchWithOverflow(true); 
    textField.setX(startX); 
    textField.setY(0); 
    if (pnameCount == idx) { 
     textField.setWidth(2 * columnWidth); 
     startX += 2 * columnWidth; 
    } else { 
     textField.setWidth(columnWidth); 
     startX += columnWidth; 
    } 
    textField.setHeight(detailHeight); 
    textField.setPositionType(JRElement.POSITION_TYPE_FLOAT); 
    textField.setStyle(tbodyStyle); 
    textField.setBlankWhenNull(true); 
    JRDesignExpression expression = new JRDesignExpression(); 
    expression.setValueClass(java.lang.String.class); 
    expression.setText("$F{field_" + idx + "}"); 
    textField.setExpression(expression); 
    columnDetailBand.addElement(textField); 
   } 
 
   JRDesignBand pageFootBand = (JRDesignBand) jasperDesign.getPageFooter(); 
   // 合计数据,本应统计的 
   List<Object[]> pageCountList = new ArrayList<Object[]>(); 
   Object[] obj = new String[] { "合计", "15299", "", "", "67121", "92420", "155877", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "XXX小计", "", "24473", "16470", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "WWW小计", "", "7289", "1674", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "ZZZ小计", "", "32700", "13000", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "YYY小计", "", "12733", "120733", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "AAA小计", "", "2225", "120733", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "BBB小计", "", "3000", "0", }; 
   pageCountList.add(obj); 
   int footWidth = 535 / 7; 
   for (int p = 0; p < pageCountList.size(); p++) { 
    for (int k = 0; k < 7; k++) { 
     Object[] ob = pageCountList.get(p); 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(tfootStyle); 
     staticText.setWidth(footWidth); 
     staticText.setY(pagefootHeight * p); 
     staticText.setX(k * footWidth + _START_X_); 
     staticText.setHeight(pagefootHeight); 
     staticText.setText(String.valueOf(ob[k])); 
     pageFootBand.addElement(staticText); 
    } 
   } 
 
   // 编译报表 
   JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); 
   String type = this.getRequest().getParameter("type");//pdf格式 
   JasperUtils.prepareReport(jasperReport, type); 
   // 报表数据源 
   JRDataSource dataSource = new JRBeanCollectionDataSource(reportDataList); 
   JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, dataSource); 
   HttpServletResponse response = this.getResponse(); 
   JasperUtils.export(jasperPrint, response, getRequest(), type); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 
  return null; 
 }
登录后复制


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(); 
 }
登录后复制

以上是Java中jasperReport实现动态列打印的实例的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Java 中的完美数 Java 中的完美数 Aug 30, 2024 pm 04:28 PM

Java 完美数指南。这里我们讨论定义,如何在 Java 中检查完美数?,示例和代码实现。

Java 中的随机数生成器 Java 中的随机数生成器 Aug 30, 2024 pm 04:27 PM

Java 随机数生成器指南。在这里,我们通过示例讨论 Java 中的函数,并通过示例讨论两个不同的生成器。

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java 版 Weka 指南。这里我们通过示例讨论简介、如何使用weka java、平台类型和优点。

Java 中的史密斯数 Java 中的史密斯数 Aug 30, 2024 pm 04:28 PM

Java 史密斯数指南。这里我们讨论定义,如何在Java中检查史密斯号?带有代码实现的示例。

Java Spring 面试题 Java Spring 面试题 Aug 30, 2024 pm 04:29 PM

在本文中,我们保留了最常被问到的 Java Spring 面试问题及其详细答案。这样你就可以顺利通过面试。

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

Java 中的时间戳至今 Java 中的时间戳至今 Aug 30, 2024 pm 04:28 PM

Java 中的时间戳到日期指南。这里我们还结合示例讨论了介绍以及如何在java中将时间戳转换为日期。

Java程序查找胶囊的体积 Java程序查找胶囊的体积 Feb 07, 2025 am 11:37 AM

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

See all articles