2014-12-19 49 views
0

enter image description here我想繪製jFreeChart在使用apache POI的word文檔,但我無法做到這一點。在這裏,我所做的我不知道哪一個我必須使用適當有人請我help.here我已經截至目前完成的,參數是不正確的,同時生成單詞與POI

 JFreeChart TotalActivityChart = ChartFactory.createBarChart("Total Activities", "Activities", "#Candidate", bar_chart_servlet, PlotOrientation.VERTICAL, false, true, false); 

     /**---------------------------------- Appearance of chart for Total Activity------------------------------------ */ 
     // Adjust appearance (optional) 
     TotalActivityChart.getTitle().setPaint(Color.BLACK); 
     TotalActivityChart.setAntiAlias(true); 
     CategoryPlot plot = (CategoryPlot) TotalActivityChart.getPlot(); 
     plot.setBackgroundPaint(new GradientPaint(0, 0, Color.DARK_GRAY, 0, 100, Color.GRAY)); 
     // set the range axis to display integers only... 
     final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 
     rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
     final CategoryItemRenderer renderer = new CustomRenderer(new Paint[] { new Color(0x3D59AB), new Color(0x009ACD), new Color(0x104E8B), new Color(0x50A6C2) }); 
     plot.setRenderer(renderer); 
     /**--------------------------------------- End of Appearance of Chart--------------------------------------- */ 
     int width = 800; 
     int height = 240; 
     ByteArrayOutputStream chart_out = new ByteArrayOutputStream(); 
     ChartUtilities.writeChartAsPNG(chart_out, TotalActivityChart, width, height); 
     String pictureIdx = document.addPictureData(chart_out.toByteArray(), Workbook.PICTURE_TYPE_PNG); 
     chart_out.close(); 
    int width = 800; 
     int height = 240; 
     ByteArrayOutputStream chart_out = new ByteArrayOutputStream(); 

     ChartUtilities.writeChartAsPNG(chart_out, TotalActivityChart, width, height); 
     String pictureIdx = document.addPictureData(chart_out.toByteArray(), document.PICTURE_TYPE_PNG); 
     //document.createParagraph().createRun().addPicture(chart_out, Document.PICTURE_TYPE_PNG,"my pic", Units.toEMU(200), Units.toEMU(200)); 
     chart_out.close(); 



     run.addPicture(new FileInputStream(pictureIdx), Document.PICTURE_TYPE_PNG, pictureIdx, Units.toEMU(200), Units.toEMU(200)); 


     // Setup the output 
     String contentType="application/msword"; 
     response.addHeader("Content-Disposition", "attachment; filename=\""+ "reportName" + ".docx\""); 
     response.setContentType(contentType); 
     ServletOutputStream out = response.getOutputStream();   
     document.write(out); 

任何一個可以建議如何添加的圖片中這個詞doc..Thanks提前

+0

看起來你已經有了它產生的PNG圖像,是嗎?如果是這樣,爲什麼不使用Apache POI將它添加到word文檔的正確位置? – Gagravarr 2014-12-19 09:05:26

+0

我怎麼能做到這一點? – lucifer 2014-12-19 09:06:18

+0

我如何添加可以請你幫忙? – lucifer 2014-12-19 09:28:57

回答

0

你的問題是這樣的塊這裏:

ByteArrayOutputStream chart_out = new ByteArrayOutputStream(); 

ChartUtilities.writeChartAsPNG(chart_out, TotalActivityChart, width, height); 
String pictureIdx = document.addPictureData(chart_out.toByteArray(), document.PICTURE_TYPE_PNG); 
//document.createParagraph().createRun().addPicture(chart_out, Document.PICTURE_TYPE_PNG,"my pic", Units.toEMU(200), Units.toEMU(200)); 
chart_out.close(); 

run.addPicture(new FileInputStream(pictureIdx), Document.PICTURE_TYPE_PNG, pictureIdx, Units.toEMU(200), Units.toEMU(200)); 

您正在將圖表圖片寫入字節數組,但隨後嘗試將一些隨機文件添加到Word文檔中,這是永遠不會工作的!

相反,通過圖表圖片插入Word文檔中,這應該是罰款,是這樣的:

ByteArrayOutputStream chart_out = new ByteArrayOutputStream(); 

ChartUtilities.writeChartAsPNG(chart_out, TotalActivityChart, width, height); 
String pictureIdx = document.addPictureData(chart_out.toByteArray(), document.PICTURE_TYPE_PNG); 
//document.createParagraph().createRun().addPicture(chart_out, Document.PICTURE_TYPE_PNG,"my pic", Units.toEMU(200), Units.toEMU(200)); 
chart_out.close(); 

byte[] chartPicture = chart_out.toByteArray(); 

run.addPicture(new ByteArrayInputStream(chartPicture), Document.PICTURE_TYPE_PNG, pictureIdx, Units.toEMU(200), Units.toEMU(200)); 

這裏的不同之處在於捕捉你的照片的字節之後,你再交給那些相同的字節POI添加到您的文檔中。你需要給圖片本身,否則它不會工作!

+0

注意顯示在我的word文檔 – lucifer 2014-12-19 11:52:55

+0

看到我張貼的圖片上面的 – lucifer 2014-12-19 11:55:46

+0

請看我的commnents – lucifer 2014-12-19 12:13:55