最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 中jasperReport實(shí)現(xiàn)動(dòng)態(tài)列打印的實(shí)現(xiàn)代碼

 更新時(shí)間:2017年09月17日 10:32:33   投稿:lqh  
這篇文章主要介紹了Java 中jasperReport實(shí)現(xiàn)動(dòng)態(tài)列打印的實(shí)現(xiàn)代碼的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下

Java 中jasperReport實(shí)現(xiàn)動(dòng)態(tài)列打印的實(shí)現(xiàn)代碼

        以下代碼中注釋說明很清楚,希望能幫助到大家,大家參考下。

示例代碼:

public ActionResult projectPrint() { 
  String[] printValue = null; 
  // 從頁面中獲得要查詢的字段 
  String reqPrintValue = getRequest().getParameter("printValue"); 
  // 沒有選擇則默認(rèn)全打印 
  if (null == reqPrintValue || StringUtils.isEmpty(reqPrintValue)) { 
   printValue = new String[] { "pnumber", "pname", "pdepart", "pdecision", "pthrow", "plastmonth", "pfund", "ploan" }; 
  } else { 
   printValue = reqPrintValue.split(","); 
  } 
  // 查詢統(tǒng)計(jì)數(shù)據(jù) 
  List<Object[]> projectList = getEntityManager().queryPrintProjectInfo(printValue); 
 
  // 將數(shù)據(jù)轉(zhuǎn)換為Map對(duì)象,換化成Map對(duì)象 
  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;// 數(shù)據(jù)列 
  int fieldCount = 0;// 字段列數(shù)(因?yàn)閜name比較長所以想讓pname比其它的列長些,故設(shè)計(jì)這個(gè)變量) 
  int pnameCount = -1;// 記錄下pname的序號(hào) 
  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的序號(hào) 
    columCount = columCount + 1; 
    fieldCount = fieldCount + 2; 
   } else { 
    // 其它的列都占一個(gè)單位 
    columCount = columCount + 1; 
    fieldCount = fieldCount + 1; 
   } 
  } 
 
  InputStream is = null; 
  try { 
   // 從資源文件中讀取報(bào)表 
   is = this.getClass().getResourceAsStream("/reports/project.jrxml"); 
   JasperDesign jasperDesign = (JasperDesign) JRXmlLoader.load(is); 
 
   Map styleMap = jasperDesign.getStylesMap(); 
   // column header 對(duì)應(yīng)的樣式 
   JRDesignStyle theaderStyle = (JRDesignStyle) styleMap.get("theader"); 
   // column detail 對(duì)應(yīng)的樣式 
   JRDesignStyle tbodyStyle = (JRDesignStyle) styleMap.get("tboby"); 
   // pagefoot 對(duì)應(yīng)的樣式 
   JRDesignStyle tfootStyle = (JRDesignStyle) styleMap.get("tfoot"); 
 
   int _START_X_ = 20;// x軸的起始位置 
   int startX = _START_X_; // x軸的起始位置 
   // 單列的寬度 
   // 535是jasepreReport報(bào)表column最大的寬度 
   int columnWidth = 535 / fieldCount; 
   // 20,24,15是報(bào)表中已設(shè)置的,一定與之相同 
   final int columnHeadBandHeight = 20; 
   final int detailHeight = 24; 
   final int pagefootHeight = 15; 
 
   // 設(shè)置報(bào)表字段 
   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("序號(hào)"); 
     columnHeadBand.addElement(staticText); 
     startX += columnWidth; 
    } else if ("pname".equals(printValue[idx])) { 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     // 項(xiàng)目名稱的寬度是其它的寬度的2倍 
     staticText.setWidth(columnWidth * 2); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(2 * columnHeadBandHeight); 
     staticText.setText("項(xiàng)目名稱"); 
     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])) { 
     // 投審會(huì)下面有兩列 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth * 2); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("投審會(huì)"); 
     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])) { 
     // 投決會(huì)下面有一列 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("投決會(huì)"); 
     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])) { 
     // 投貸協(xié)同額下面有兩列 
     JRDesignStaticText staticText = new JRDesignStaticText(); 
     staticText.setStyle(theaderStyle); 
     staticText.setWidth(columnWidth * 2); 
     staticText.setY(0); 
     staticText.setX(startX); 
     staticText.setHeight(columnHeadBandHeight); 
     staticText.setText("投貸協(xié)同額"); 
     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(); 
   // 合計(jì)數(shù)據(jù),本應(yīng)統(tǒng)計(jì)的 
   List<Object[]> pageCountList = new ArrayList<Object[]>(); 
   Object[] obj = new String[] { "合計(jì)", "15299", "", "", "67121", "92420", "155877", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "XXX小計(jì)", "", "24473", "16470", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "WWW小計(jì)", "", "7289", "1674", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "ZZZ小計(jì)", "", "32700", "13000", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "YYY小計(jì)", "", "12733", "120733", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "AAA小計(jì)", "", "2225", "120733", }; 
   pageCountList.add(obj); 
   obj = new String[] { "", "", "", "BBB小計(jì)", "", "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); 
    } 
   } 
 
   // 編譯報(bào)表 
   JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); 
   String type = this.getRequest().getParameter("type");//pdf格式 
   JasperUtils.prepareReport(jasperReport, type); 
   // 報(bào)表數(shù)據(jù)源 
   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(); 
 } 

如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 使用Spring Data Jpa的CriteriaQuery一個(gè)陷阱

    使用Spring Data Jpa的CriteriaQuery一個(gè)陷阱

    使用Spring Data Jpa的CriteriaQuery進(jìn)行動(dòng)態(tài)條件查詢時(shí),可能會(huì)遇到一個(gè)陷阱,當(dāng)條件為空時(shí),查詢不到任何結(jié)果,并不是期望的返回所有結(jié)果。這是為什么呢?
    2020-11-11
  • 詳解如何使用IntelliJ IDEA新建一個(gè)Servlet項(xiàng)目

    詳解如何使用IntelliJ IDEA新建一個(gè)Servlet項(xiàng)目

    這篇文章主要介紹了詳解如何使用IntelliJ IDEA新建一個(gè)Servlet項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • Java鎖機(jī)制Lock用法示例

    Java鎖機(jī)制Lock用法示例

    這篇文章主要介紹了Java鎖機(jī)制Lock用法,結(jié)合具體實(shí)例形式分析了Java鎖機(jī)制的相關(guān)上鎖、釋放鎖、隱式鎖、顯式鎖等概念與使用技巧,需要的朋友可以參考下
    2018-08-08
  • nodejs與JAVA應(yīng)對(duì)高并發(fā)的對(duì)比方式

    nodejs與JAVA應(yīng)對(duì)高并發(fā)的對(duì)比方式

    這篇文章主要介紹了nodejs與JAVA應(yīng)對(duì)高并發(fā)的對(duì)比方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • MyBatis實(shí)現(xiàn)插入大量數(shù)據(jù)方法詳解

    MyBatis實(shí)現(xiàn)插入大量數(shù)據(jù)方法詳解

    最近在公司項(xiàng)目開發(fā)中遇到批量數(shù)據(jù)插入或者更新,下面這篇文章主要給大家介紹了關(guān)于MyBatis實(shí)現(xiàn)批量插入的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • Java實(shí)現(xiàn)帶緩沖的輸入輸出流

    Java實(shí)現(xiàn)帶緩沖的輸入輸出流

    本文詳細(xì)講解了Java實(shí)現(xiàn)帶緩沖的輸入輸出流,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 使用springboot對(duì)linux進(jìn)行操控的方法示例

    使用springboot對(duì)linux進(jìn)行操控的方法示例

    這篇文章主要介紹了使用springboot對(duì)linux進(jìn)行操控的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • SpringBoot中的ApplicationRunner與CommandLineRunner問題

    SpringBoot中的ApplicationRunner與CommandLineRunner問題

    這篇文章主要介紹了SpringBoot中的ApplicationRunner與CommandLineRunner問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Feign Client超時(shí)時(shí)間設(shè)置不生效的解決方法

    Feign Client超時(shí)時(shí)間設(shè)置不生效的解決方法

    這篇文章主要為大家詳細(xì)介紹了Feign Client 超時(shí)時(shí)間設(shè)置不生效的原因與解決方法,具有一定的的參考價(jià)值,希望對(duì)大家有一定的幫助
    2025-04-04
  • Java中多個(gè)線程交替循環(huán)執(zhí)行的實(shí)現(xiàn)

    Java中多個(gè)線程交替循環(huán)執(zhí)行的實(shí)現(xiàn)

    有些時(shí)候面試官經(jīng)常會(huì)問,兩個(gè)線程怎么交替執(zhí)行呀,本文就來詳細(xì)的介紹一下Java中多個(gè)線程交替循環(huán)執(zhí)行的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01

最新評(píng)論

托克逊县| 安泽县| 无为县| 宁远县| 灵石县| 浮梁县| 疏附县| 星子县| 长治县| 太仓市| 囊谦县| 闽清县| 河池市| 柳河县| 高邮市| 阳朔县| 深州市| 阳西县| 江津市| 阿拉善右旗| 眉山市| 荔浦县| 安岳县| 页游| 玉门市| 灵宝市| 南溪县| 厦门市| 吉林市| 沾化县| 大冶市| 黑水县| 靖江市| 介休市| 荔浦县| 长春市| 沧源| 巴林左旗| 巴马| 宁强县| 隆安县|