This file is tested on POI3.0-alpha1 version.

Step. 1
Get the JFreeChart and POI from the Web.

Step. 2
Edit your net.sf.jasperreports.engine.export.JRXlsExporter.java file
--
// edited by kkckc
protected ExporterElements getExporterElements()
{
//  return JRGridLayout.NO_IMAGES_EXPORTER;
         return JRGridLayout.UNIVERSAL_EXPORTER;
}

// added by kkckc
protected HSSFPatriarch patriarch =null;       

// edited by kkckc
protected void createSheet(String name)
{
     sheet = workbook.createSheet(name);
     patriarch = sheet.createDrawingPatriarch();
}

// added by kkckc
protected void exportImage(JRPrintImage image, JRExporterGridCell gridCell, int colIndex, int rowIndex) {
         short forecolor = getNearestColor(image.getForecolor()).getIndex();
         HSSFFont cellFont = getLoadedFont(getDefaultFont(), forecolor);
         HSSFCellStyle cellStyle =
                 getLoadedCellStyle(
                         HSSFCellStyle.SOLID_FOREGROUND,
                         forecolor,
                         HSSFCellStyle.ALIGN_LEFT,
                         HSSFCellStyle.VERTICAL_TOP,
                         (short) 0,
                         cellFont,
                         gridCell
                 );
         //createMergeRegion(gridCell, colIndex, rowIndex, cellStyle);           
         /*
          x1 - the x coordinate within the first cell.
          y1 - the y coordinate within the first cell.
          x2 - the x coordinate within the second cell.
          y2 - the y coordinate within the second cell.
          col1 - the column (0 based) of the first cell.
          row1 - the row (0 based) of the first cell.
          col2 - the column (0 based) of the second cell.
          row2 - the row (0 based) of the second cell.           
         */
        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, image.getWidth(), image.getHeight(), (short) colIndex, rowIndex,
                (short) (colIndex + gridCell.colSpan), rowIndex + gridCell.rowSpan);
        try {
            patriarch.createPicture(anchor, workbook.addPicture(image.getRenderer().getImageData(), HSSFWorkbook.PICTURE_TYPE_JPEG));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
}

---
You can use this code with Free License.
But you must mention the source auther(kkckc) and site before you use it.
----

회사에서 JFreeChart를 사용하고 있던 중 Excel에서 이미지가 Export되지 않는 부분을 고친 것입니다.
배포는 가능하지만 원저자와 출처를 반드시 밝혀주시기 바랍니다. :)



Posted by kkckc
,