将Jfreechart图像导出为ex​​cel

问题描述:

我正在寻找将jfreechart(可在JSP + struts中导出)导出到Excel表单的选项。将Jfreechart图像导出为ex​​cel

我已经尝试'设置响应头来键入msexcel'将整个JSP导出为ex​​cel,但它不呈现jfreechart图像。

是否有任何其他选项将jfreechart导出为ex​​cel?

我已经使用jfreechart生成了图表,并使用POI将其导出为ex​​cel。

ServletOutputStream stream=resp.getOutputStream(); 
resp.setContentType("application/vnd.ms-excel"); 
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
int imageWidth= 700; 
int imageHeigth = 400; 

JFreeChart chart=ChartFactory.createBarChart("Test Chart", "", "", dataset, PlotOrientation.VERTICAL, true, true, false); 

ChartUtilities.writeChartAsJPEG(outStream, chart, imageWidth, imageHeigth); 

byte[] imageInByte = outStream.toByteArray(); 
outStream.close(); 

int pictureIdx =wb.addPicture(imageInByte,Workbook.PICTURE_TYPE_JPEG); 

Drawing drawing = sheet.createDrawingPatriarch(); 

CreationHelper helper = wb.getCreationHelper(); 

//add a picture shape 
ClientAnchor anchor = helper.createClientAnchor(); 
//set top-left corner of the picture, 
//subsequent call of Picture#resize() will operate relative to it 
anchor.setCol1(3); 
anchor.setRow1(4); 
Picture pict = drawing.createPicture(anchor, pictureIdx); 

//auto-size picture relative to its top-left corner 
pict.resize(); 
wb.write(outStream); 
stream.write(outStream.toByteArray()); 

Apache POI不会帮助你,因为它有一个限制(请阅读Apache网站的手册)。它只能绘制线条和分散的图表,看起来很差。 图书馆适合使用excel,但不适合构建图表。我的建议是使用预先准备好的模板,对其进行调整,并将必要的数据从Java传输到模板。 另一种使用JPEG的方式,但它是可怕的事情,不要这样做。