我试图使用apache POI在单词doc中绘制jFreeChart,但我无法做到这一点。这就是我所做的我不知道哪个api我必须正确使用某人请求助于我现在所做的事情,
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中添加图片。提前谢谢
答案 0 :(得分: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以添加到您的文档中。你需要自己提供图片,否则它将不起作用!
答案 1 :(得分:0)
我也遇到了同样的问题,我将poi版本从3.9更改为4.0.1 而且有效。