zoukankan      html  css  js  c++  java
  • 如何用Apache POI操作Excel文件-----如何用Apache POI 画一个离散图

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

    代码如下,

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. /** 
    8.  * Illustrates how to create a simple scatter chart. 
    9.  * 
    10.  * @author Roman Kashitsyn 
    11.  */  
    12. public class ScatterChart {  
    13.   
    14.     public static void main(String[] args) throws Exception {  
    15.         Workbook wb = new XSSFWorkbook();  
    16.         Sheet sheet = wb.createSheet("Sheet 1");  
    17.         final int NUM_OF_ROWS = 3;  
    18.         final int NUM_OF_COLUMNS = 10;  
    19.   
    20.         // Create a row and put some cells in it. Rows are 0 based.  
    21.         Row row;  
    22.         Cell cell;  
    23.         for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {  
    24.             row = sheet.createRow((short) rowIndex);  
    25.             for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {  
    26.                 cell = row.createCell((short) colIndex);  
    27.                 cell.setCellValue(colIndex * (rowIndex + 1));  
    28.             }  
    29.         }  
    30.   
    31.         Drawing drawing = sheet.createDrawingPatriarch();  
    32.         ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);  
    33.   
    34.         Chart chart = drawing.createChart(anchor);  
    35.         ChartLegend legend = chart.getOrCreateLegend();  
    36.         legend.setPosition(LegendPosition.TOP_RIGHT);  
    37.   
    38.         ScatterChartData data = chart.getChartDataFactory().createScatterChartData();  
    39.   
    40.         ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);  
    41.         ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);  
    42.         leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);  
    43.   
    44.         ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));  
    45.         ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));  
    46.         ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));  
    47.   
    48.   
    49.         data.addSerie(xs, ys1);  
    50.         data.addSerie(xs, ys2);  
    51.   
    52.         chart.plot(data, bottomAxis, leftAxis);  
    53.   
    54.         // Write the output to a file  
    55.         FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx");  
    56.         wb.write(fileOut);  
    57.         fileOut.close();  
    58.     }  
    59. }  

    下面逐一来分解:

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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. Workbook wb = new XSSFWorkbook();  
    2.      Sheet sheet = wb.createSheet("Sheet 1");  


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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. final int NUM_OF_ROWS = 3;  
    2. final int NUM_OF_COLUMNS = 10;  
    3.   
    4.        // Create a row and put some cells in it. Rows are 0 based.  
    5.        Row row;  
    6.        Cell cell;  
    7.        for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {  
    8.            row = sheet.createRow((short) rowIndex);  
    9.            for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {  
    10.                cell = row.createCell((short) colIndex);  
    11.                cell.setCellValue(colIndex * (rowIndex + 1));  
    12.            }  
    13.        }  


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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. Drawing drawing = sheet.createDrawingPatriarch();  
    2. ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);  

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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. Chart chart = drawing.createChart(anchor);  
    2. ChartLegend legend = chart.getOrCreateLegend();  
    3. legend.setPosition(LegendPosition.TOP_RIGHT);  
    4.   
    5. ScatterChartData data = chart.getChartDataFactory().createScatterChartData();  
    6.   
    7. ValueAxis bottomAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.BOTTOM);  
    8. ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);  
    9. leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);  

    5.往离散图上填充数据

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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.   
    5. data.addSerie(xs, ys1);  
    6. data.addSerie(xs, ys2);  

    其中,下面的方法定义

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));  
    
    
    

    如下,

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. public static ChartDataSource<Number> fromNumericCellRange(Sheet sheet, CellRangeAddress cellRangeAddress)   

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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <pre name="code" class="java">new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1)  
    
    

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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. 开始画图

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. chart.plot(data, bottomAxis, leftAxis);  


    7. 保存成一个Excel文件

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
      1. FileOutputStream fileOut = new FileOutputStream("ooxml-scatter-chart.xlsx");  
      2. wb.write(fileOut);  
      3. fileOut.close();  
  • 相关阅读:
    基本排序算法汇总
    贪心算法题目汇总
    STL中sort
    栈和队列题目汇总
    cron 计划任务 在线配置
    各种less概念通俗解释
    node 子线程 进程
    内存池
    RPC简介
    koa express 对比
  • 原文地址:https://www.cnblogs.com/telwanggs/p/5787038.html
Copyright © 2011-2022 走看看