我正在尋找將jfreechart(可在JSP + struts中導出)導出到Excel表單的選項。將Jfreechart圖像導出爲excel
我已經嘗試'設置響應頭來鍵入msexcel'將整個JSP導出爲excel,但它不呈現jfreechart圖像。
是否有任何其他選項將jfreechart導出爲excel?
我正在尋找將jfreechart(可在JSP + struts中導出)導出到Excel表單的選項。將Jfreechart圖像導出爲excel
我已經嘗試'設置響應頭來鍵入msexcel'將整個JSP導出爲excel,但它不呈現jfreechart圖像。
是否有任何其他選項將jfreechart導出爲excel?
我已經使用jfreechart生成了圖表,並使用POI將其導出爲excel。
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的方式,但它是可怕的事情,不要這樣做。