1

我正在寻找将 jfreechart(在 JSP+struts 中可用)导出到 excel 表的选项。

我尝试“将响应标头设置为键入 msexcel”以将整个 JSP 导出到 excel,但它没有呈现 jfreechart 图像。

有没有其他选项可以将 jfreechart 导出到 excel 中?

4

2 回答 2

1

我使用 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());
于 2012-08-22T14:43:29.887 回答
0

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

于 2017-03-11T04:58:35.403 回答