如何使用java poi或jfree jar在excel中创建动态柱形图

时间:2017-03-08 11:40:46

标签: java excel apache-poi xls xlsx

我需要在Excel报表中创建动态柱形图,并根据所选日期与2列值(销售和保证金)进行比较。我已经使用poi的WriteableWork对象生成excel报告了。但我需要动态生成图表。

此方法是创建Excel报告

public String excelReport() throws ParseException, IOException {
try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    WritableWorkbook w = Workbook.createWorkbook(bos);
    WritableSheet sheet = w.createSheet("Page 1", 0);
    // Create Filename
    String filenameDate = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    attachmentName = "attachment; filename=\"test".xls\"" ;

            int headerCol = 0;
            addBoldCell(sheet, headerCol++, 0, "Date");
            addBoldCell(sheet, headerCol++, 0,"Sale");
            addBoldCell(sheet, headerCol++, 0,"Margin");
     int row = 1;
     int col = 0;
             addNumberCell(sheet, col++, row, date);
             addNumberCell(sheet, col++, row, 20000);
         addNumberCell(sheet, col++, row, 500); 

    w.write();
    w.close();
    bufferSize = bos.size();
    fileInputStream = new ByteArrayInputStream(bos.toByteArray());

} catch (Exception e) {
    log.warn("Error creating an Excel file.", e);
} finally {
    if (out != null) {
        out.close();
        }
    }
}
return SUCCESS;
}

在struts.xml中

<action name="excelReport" method="excelReport" class="com.test.action.ReportAction">
        <result name="success" type="stream">
            <param name="contentType">application/vnd.ms-excel</param>
            <param name="contentDisposition">${attachmentName}</param>
            <param name="inputName">fileInputStream</param>
            <param name="bufferSize">${bufferSize}</param>
        </result>
    </action>

Excel报告我成功了,我想要这个excel值的图表,如下面的示例图表

enter image description here

我尝试了一些poi和jfree样本,但没有得到解决方案。

0 个答案:

没有答案