有的时候,我们需要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. Java性能优化权威指南-读书笔记(四)-JVM性能调优-延迟

    延迟指服务器处理一个请求所花费的时间,单位一般是ms.s. 本文主要讲降低延迟可以做的服务器端JVM优化. JVM延迟优化 新生代 新生代大小决定了应用平均延迟 如果平均Minor GC持续时间大于应 ...

  2. JPush Wiki

    极光推送包含有通知与自定义消息两种类型的推送.本文描述他们的区别,以及建议的应用场景. 功能角度 通知 或者说 Push Notification,即指在手机的通知栏(状态栏)上会显示的一条通知信息. ...

  3. Android之webView入门

    WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...

  4. Duilib非官方更新贴~

    GitHub: https://github.com/movsb/duilib.git 2014-07-20: [76a04d1]    [BugFix] 修复无法解析类似<Control/&g ...

  5. Sonar+Hudson+Maven构建系列之三:安装Hudson

    摘要:其实前面介绍过Sonar,后面Hudson安装就方便了.安装Hudson之前说说Hudson相关的事,现在世面上的有两种与Hudson相关的CI工具,一个是Hudson,一个是Jenkins,这 ...

  6. 使用SQL Server维护计划实现数据库定时自动备份

    在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员每天守到晚上1点去备份数据库.要实现数据库的定 ...

  7. Android Studio 一些使用经验

    一.Mac或Win 7 配置环境,以gradle为例 (1).可以在这里找gradle下载,或者去官网啦 Mac添加环境变量: 1.启动Terminal终端 2.输入cd ~/ 进入当前用户的home ...

  8. android去掉标题栏

    在AndroidManifest.xml修改 把 <applicationandroid:allowBackup="true"android:icon="@draw ...

  9. ntpd时间同步 安装与配置

    1,安装 yum -y install ntp vim /etc/ntp.conf 默认配置: driftfile /var/lib/ntp/drift restrict default kod no ...

  10. 使用OUYA第一次启动OUYA

    使用OUYA第一次启动OUYA 1.4  使用OUYA 初次使用OUYA时,其启动以后的设置过程耗时较长,也比较繁琐,因此本节将会对其做个详细介绍,让读者的使用过程更加顺利些!好的开端总归是一个不错的 ...