有的时候,我们需要Excel中的数据,通过一个图画,可视化的表现出来。 那么这个时候,应该如何做呢?现在就借花献佛,以Apache POI自己提供的一个例子为例,给大家演示一下POI的API 如何画图的。下面是一个最终的效果图。然后分别给大家解释每段代码的作用和意义。

代码如下,

  1. import java.io.FileOutputStream;
  2. import org.apache.poi.ss.usermodel.*;
  3. import org.apache.poi.ss.util.*;
  4. import org.apache.poi.ss.usermodel.charts.*;
  5. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  6. /**
  7. * Illustrates how to create a simple scatter chart.
  8. *
  9. * @author Roman Kashitsyn
  10. */
  11. public class ScatterChart {
  12. public static void main(String[] args) throws Exception {
  13. Workbook wb = new XSSFWorkbook();
  14. Sheet sheet = wb.createSheet("Sheet 1");
  15. final int NUM_OF_ROWS = 3;
  16. final int NUM_OF_COLUMNS = 10;
  17. // Create a row and put some cells in it. Rows are 0 based.
  18. Row row;
  19. Cell cell;
  20. for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
  21. row = sheet.createRow((short) rowIndex);
  22. for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
  23. cell = row.createCell((short) colIndex);
  24. cell.setCellValue(colIndex * (rowIndex + 1));
  25. }
  26. }
  27. Drawing drawing = sheet.createDrawingPatriarch();
  28. ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
  29. Chart chart = drawing.createChart(anchor);
  30. ChartLegend legend = chart.getOrCreateLegend();
  31. legend.setPosition(LegendPosition.TOP_RIGHT);
  32. ScatterChartData data = chart.getChartDataFactory().createScatterChartData();
  33. ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
  34. ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
  35. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
  36. ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
  37. ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
  38. ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
  39. data.addSerie(xs, ys1);
  40. data.addSerie(xs, ys2);
  41. chart.plot(data, bottomAxis, leftAxis);
  42. // Write the output to a file
  43. FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx");
  44. wb.write(fileOut);
  45. fileOut.close();
  46. }
  47. }

下面逐一来分解:

1.下面的代码新建一个工作簿和工作表单的对象

  1. Workbook wb = new XSSFWorkbook();
  2. Sheet sheet = wb.createSheet("Sheet 1");

2.下面这段代码是用生成初始化数据的,总共的数据有3行10列。

  1. final int NUM_OF_ROWS = 3;
  2. final int NUM_OF_COLUMNS = 10;
  3. // Create a row and put some cells in it. Rows are 0 based.
  4. Row row;
  5. Cell cell;
  6. for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
  7. row = sheet.createRow((short) rowIndex);
  8. for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
  9. cell = row.createCell((short) colIndex);
  10. cell.setCellValue(colIndex * (rowIndex + 1));
  11. }
  12. }

3. 下面这段代码设置了画图的区域:从第5行开始,到15行结束;总共占用10列

  1. Drawing drawing = sheet.createDrawingPatriarch();
  2. ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);

4.创建一个离散图的坐标系

  1. Chart chart = drawing.createChart(anchor);
  2. ChartLegend legend = chart.getOrCreateLegend();
  3. legend.setPosition(LegendPosition.TOP_RIGHT);
  4. ScatterChartData data = chart.getChartDataFactory().createScatterChartData();
  5. ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);
  6. ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
  7. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

5.往离散图上填充数据

  1. ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
  2. ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
  3. ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
  4. data.addSerie(xs, ys1);
  5. data.addSerie(xs, ys2);

其中,下面的方法定义

  1. DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));

如下,

  1. public static ChartDataSource<Number> fromNumericCellRange(Sheet sheet, CellRangeAddress cellRangeAddress)

从上面可以看出,其实填充数据的关键方法是,

  1. <pre name="code" class="java">new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)

那么这个方式是如何定义的呢?

  1. public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)

从上面可以看出,其让我们制定数据是从那一行开始的,那一行结束的,那一列开始的,那一列结束。

在上面的代码的5句话中,分别把第1行的1到10列做为基准,然后把第2行的1到10列做一个比较,画出曲线系列1

把第1行的1到10列做为基准,然后把第3行的1到10列做一个比较,画出曲线系列2

6. 开始画图

  1. chart.plot(data, bottomAxis, leftAxis);

7. 保存成一个Excel文件

    1. FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx");
    2. wb.write(fileOut);
    3. fileOut.close();

如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图的更多相关文章

  1. java使用POI操作excel文件,实现批量导出,和导入

    一.POI的定义 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作Exc ...

  2. java使用Apache POI操作excel文件

    官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...

  3. Java使用POI操作Excel文件

    1.简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式文件读和写的功能. 2.依赖的jar包 <!-- ex ...

  4. 如何用Apache POI操作Excel文件-----如何对一个单元格加注解?

    有的时候,我们需要通过操作Apache POI,在生成Cell数据的同时,能对其生成的Cell,加上注解(comments),类似于下面的. 那么对于这种情况,我们的代码应该如何写呢? 借花献佛,我就 ...

  5. 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?

    在POI的第一节入门中,我们提供了两个简单的例子,一个是如何用Apache POI新建一个工作薄,另外一个例子是,如果用Apache POI新建一个工作表.那么在这个章节里面,我将会给大家演示一下,如 ...

  6. (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug

    如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...

  7. 使用Apache POI操作Excel文件---在已有的Excel文件中插入一行新的数据

    package org.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...

  8. [转载]Java操作Excel文件的两种方案

    微软在桌面系统上的成功,令我们不得不大量使用它的办公产品,如:Word,Excel.时至今日,它的源代码仍然不公开已封锁了我们的进一步应用和开发.在我们实际开发企业办公系统的过程中,常常有客户这样子要 ...

  9. 使用POI操作Excel时对事先写入模板的公式强制执行

    场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式则不自动被调用,必须手动双击该Cell才能生 ...

随机推荐

  1. [Android Pro] proguard.cfg 配置文件

    转载自:http://my.oschina.net/zhangzhihao/blog/72393 # ------------------------------------- # android 原 ...

  2. IBM V7000

    IBM_V7000 http://www.tuicool.com/articles/7vI7ja http://www.ibm.com/support/knowledgecenter/en/ST3FR ...

  3. FileHelper-文件操作辅助类

    using System; using System.Collections.Generic; using System.IO; using System.Text; namespace Whir.S ...

  4. [译]SQL Server 之 索引基础

    SQL Server中,索引以B-tree的结构组织数据.B-tree代表平衡树,但是SQL Server使用一种叫做B+的树. B+树不是总是保持严格的平衡的树. 首先,索引有两个主要的部件:一个页 ...

  5. poj 3252 组合数

        主要考察组合数知识,初始化的时候参考公式 首先先推个公式,就是长度为len的Round Numbers的个数.      长度为len,第一位肯定是1了.      那么后面剩下 len-1位 ...

  6. java中基本输入输出流的解释(flush方法的使用)

    转自:http://fsz521job.itpub.net/post/5606/34827 网络程序的很大一部分是简单的输入输出,即从一个系统向另一个系统移动字节.字节就是字节,在很大程度上,读服务器 ...

  7. thinkphp where()条件查询

    今天来给大家讲下查询最常用但也是最复杂的where方法,where方法也属于模型类的连贯操作方法之一,主要用于查询和操作条件的设置.where方法的用法是ThinkPHP查询语言的精髓,也是Think ...

  8. AngularJS学习笔记一:简单入门

    阿里云网站的前端是AngularJS实现的. 先下载AngularJS的开发工具包,我下载的angular-1.4.0. 在合适位置引入js文件: <script src="angul ...

  9. ural 1244. Gentlemen

    1244. Gentlemen Time limit: 0.5 secondMemory limit: 64 MB Let's remember one old joke: Once a gentle ...

  10. Catching Fish[HDU1077]

    Catching Fish Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...