问题描述: 用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 天时间,我是一个一个的排查,终于找到了问题的所在