Java學習

          java,spring,structs,hibernate,jsf,ireport,jfreechart,jasperreport,tomcat,jboss -----本博客已經搬家了,新的地址是 http://www.javaly.cn 如果有對文章有任何疑問或者有任何不懂的地方,歡迎到www.javaly.cn (Java樂園)指出,我會盡力幫助解決。一起進步

           

          關于Ireport,JasperReport生成Excel報表,chart 圖 不顯示的問題

          問題描述:  用Ireport 和 JasperReports生成報表,在PDF中能顯示2D chart 和 3D chart圖,但是在Excel和html報表中,chart圖不能顯示。

          代碼:--------------------
           

              public String generateReport() throws Exception{

          /*
          *
          *
          *去掉不相關的部分代碼
          *
          *
          */
                  HashMap parameters=new HashMap();
                  parameters.put("user", userDetails.getLoginName());
                  parameters.put("fromDate", this.dateFrom);
                  parameters.put("toDate", this.dateTo);
                  parameters.put("ownerName", this.ownerName);
                  parameters.put("regNo", this.vehicleNo);       
                  parameters.put("SUBREPORT_DIR", reportGenerator.getReportPath()+"/");
                 
                  if(this.repost_type.equals("html")){//html
                     
                      //show piechart
                      if("".equalsIgnoreCase(this.pieValue) && this.showTable1 == false && this.showTable2 == false && this.showTable3 == false){
                          initErrorBean(errorManager.getMessage("valid.no.record"));
                          return null;
                      }
                  }
                  else{    //excel || pdf
                      this.setShowTable1(false);
                      this.setShowTable2(false);
                      this.setShowTable3(false);
                      this.setPieFlag(false);
                      if(this.pieValue == null || this.pieValue.equals("")){
                           //TODO: add err msg
                           return "";
                      }
                      if("1".equalsIgnoreCase(this.getFilter_asset_by())){
                          if(null == utilizationList1 || utilizationList1.size() == 0
                                  || null == utilizationList2 || utilizationList2.size() == 0){
                                 //TODO: add err msg
                                 return "";
                          }
                          parameters.put("sub1_title", this.getTopchart());
                          parameters.put("regNo", "ALL");   
                          parameters.put("MyDatasource_sub1", new JRBeanCollectionDataSource(utilizationList1));
                          parameters.put("MyDatasource_sub2", new JRBeanCollectionDataSource(utilizationList2));
                      }
                      else{
                          if(null == indutilizationList3 || indutilizationList3.size() == 0){
                                 //TODO: add err msg
                                 return "";
                          }
                          parameters.put("MyDatasource", new JRBeanCollectionDataSource(indutilizationList3));
                      }
                 
                     List chatList = new ArrayList();
                     String []type = this.pieValue.split(":");     //mannual immedate advance vacant 
                     if(type.length != 4){
                         //TODO: add err msg
                         return "";
                     }
                     String [] manualDetail = type[0].split(",");
                     String [] immedateDetail = type[1].split(",");
                     String [] advanceDetail = type[2].split(",");
                     String [] vacantDetail = type[3].split(",");
                     if(manualDetail.length != 3 || immedateDetail.length != 3 || advanceDetail.length != 3 || vacantDetail.length != 3){
                         //TODO: add err msg
                         return "";
                     }
                     float allTime = 0.0f;
                     int maxTime       = 0;
                     int manualTime    = Integer.valueOf(manualDetail[1].replaceAll(" ", ""));
                     int immedateTime  = Integer.valueOf(immedateDetail[1].replaceAll(" ", ""));
                     int advanceTime   = Integer.valueOf(advanceDetail[1].replaceAll(" ", ""));
                     int vacantTime    = Integer.valueOf(vacantDetail[1].replaceAll(" ", ""));
                    
                     maxTime = Math.max(Math.max(manualTime, immedateTime), Math.max(advanceTime, vacantTime));
                     allTime = manualTime + immedateTime + advanceTime + vacantTime;
                    
                     int manualPerValue   = Math.round(manualTime/allTime*100);
                     int immedatePerValue = Math.round(immedateTime/allTime*100);
                     int advancePerValue  = Math.round(advanceTime/allTime*100);
                     int vacantPerValue   = Math.round(vacantTime/allTime*100);
                     boolean dealMax      = false;
                    
                     Piechat manualChat = new Piechat();
                     manualChat.setLegendItem(manualDetail[0]);
                     manualChat.setPerValue(""+manualPerValue+"%");
                     if(maxTime == manualTime){
                         manualChat.setPerValue(""+(100 - (immedatePerValue + advancePerValue + vacantPerValue))+"%");
                         dealMax = true;
                     }
                     manualChat.setValue(manualTime);
                    
                     Piechat immedateChat = new Piechat();
                     immedateChat.setLegendItem(immedateDetail[0]);
                     immedateChat.setPerValue(""+immedatePerValue+"%");
                     if(!dealMax && maxTime == immedateTime){
                         immedateChat.setPerValue(""+(100 - (manualPerValue + advancePerValue + vacantPerValue))+"%");
                         dealMax = true;
                     }
                     immedateChat.setValue(immedateTime);
                    
                     Piechat advanceChat = new Piechat();
                     advanceChat.setLegendItem(advanceDetail[0]);
                     advanceChat.setPerValue(""+advancePerValue+"%");
                     if(!dealMax && maxTime == advanceTime){
                         advanceChat.setPerValue(""+(100 - (manualPerValue + immedatePerValue + vacantPerValue))+"%");
                         dealMax = true;
                     }
                     advanceChat.setValue(advanceTime);
                    
                     Piechat vacantChat = new Piechat();
                     vacantChat.setLegendItem(vacantDetail[0]);
                     vacantChat.setPerValue(""+vacantPerValue+"%");
                     if(!dealMax && maxTime == vacantTime){
                         vacantChat.setPerValue(""+(100 - (manualPerValue + immedatePerValue + advancePerValue))+"%");
                     }
                     vacantChat.setValue(vacantTime);
                    
                     chatList.add(manualChat);
                     chatList.add(immedateChat);
                     chatList.add(advanceChat);
                     chatList.add(vacantChat);
                     String []title = vacantDetail[2].split("\\|");
                     if(title.length != 2){
                         return "";
                     }
                     parameters.put("piechat_title", title[1]);
                 
                      DateFormat format = new SimpleDateFormat("yyyyMMdd",Locale.ENGLISH);
                    String reportName = format.format(new Date())+utilizationStatisticsReport;
                   
                    if(this.repost_type.equals("excel")){ //excel            
                        InputStream inputStream = reportGenerator.getReportStream("Utilization_Statistics_Report",parameters,chatList,ReportConstants.REPORT_XLS);
                        reportGenerator.renderReportToClient(inputStream, reportName,ReportConstants.REPORT_XLS);       
                        }else{ //pdf
                          InputStream inputStream = reportGenerator.getReportStream("Utilization_Statistics_Report",parameters,chatList,ReportConstants.REPORT_PDF.toLowerCase());
                          reportGenerator.renderReportToClient(inputStream, reportName,ReportConstants.REPORT_PDF.toLowerCase());       
                        }   
                  }
                  return "";
              }



              public  InputStream getXLSReportStream(JasperPrint jasperPrint, String fileName, List jasperPrintList)throws ReportException {
                  try {
                     
                      ByteArrayOutputStream baos = new ByteArrayOutputStream();
                     
                      //JRXlsExporter exporter = new JRXlsExporter();
                      JExcelApiExporter exporter=new JExcelApiExporter();

                      if(jasperPrint != null)
                      {
                          exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                      }
                     
                      if(jasperPrintList != null)
                      {
                          exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,jasperPrintList);
                      }
                     
                      if(jasperPrint == null && jasperPrintList == null)
                          throw new ReportException("");
                     
                      exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
                      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);           
                     
                      // Add by johnny
                      exporter.setParameter(
                              JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
                              Boolean.TRUE);
                      exporter.setParameter(
                              JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
                              Boolean.FALSE);
                      exporter.exportReport();
                     
                      byte[] reportBytes = baos.toByteArray();
                     
                      return new ByteArrayInputStream(reportBytes);
                     
                  } catch (Exception e) {
                      e.printStackTrace();
                      throw new ReportException("", e);
                  }
                 
              }


          解決方法:看到紅色部分沒有,就是因為開始用的上面注釋點的JRXlsExporter,就是因為這個類,不支持圖片。所以后來改成JExcelApiExporter(),問題就解決了。
            希望我的這篇文章 能遇到和我同樣的問題的朋友們,我在這個問題上花費了2 天時間,我是一個一個的排查,終于找到了問題的所在



          posted on 2008-06-17 13:04 找個美女做老婆 閱讀(4914) 評論(1)  編輯  收藏

          評論

          # re: 關于Ireport,JasperReport生成Excel報表,chart 圖 不顯示的問題 2008-11-11 15:11 大模大樣

          非常感謝,你幫我解決了別外一個問題。。。  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          本blog已經搬到新家了, 新家:www.javaly.cn
           http://www.javaly.cn

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 磐安县| 成都市| 崇信县| 海伦市| 威海市| 汉中市| 南丹县| 壶关县| 昌邑市| 报价| 梅河口市| 茂名市| 台中县| 壶关县| 杭锦后旗| 鲁山县| 湖口县| 东阳市| 太仆寺旗| 吕梁市| 武川县| 惠水县| 西安市| 周口市| 平江县| 黑河市| 夹江县| 东乌珠穆沁旗| 五指山市| 台山市| 洛南县| 开化县| 五莲县| 富源县| 子洲县| 隆德县| 巴里| 青阳县| 阳曲县| 东辽县| 拜泉县|