POI处理Excel,条形图,散点图,折线图

  • 一、引入Maven依赖
  • 二、条形图实现
  • 2.1实现效果展示
  • 2.2 代码实现如下:
  • 三、散点图实现-----对应Excel中的XY散点图
  • 3.1初始数据准备
  • 3.2实现效果展示
  • 3.3代码实现如下:

项目中需要使用到Java来处理Excel图表的情况,记录一次Java使用POI来处理条形图,散点图,折线图。直接上代码:

一、引入Maven依赖

// An highlighted block
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.4</version>
</dependency>

二、条形图实现

------------------------------------------条形图开始展示-------------------------------------------

2.1实现效果展示

2.2 代码实现如下:

// 处理条形图
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.BarDirection;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;import com.ceshi.FileUtil;/*** @Author Lee* @Date 2021/3/20* POI实现Excel条形图**/
public class XSSFUtils {public static void main(String[] args) throws Exception{XSSFWorkbook wb = new XSSFWorkbook();String sheetName = "Sheet1";FileOutputStream fileOut = null;try {XSSFSheet sheet = wb.createSheet(sheetName);List<String> testList = new ArrayList<String>();testList.add("测试数据1");testList.add("测试数据2");testList.add("测试数据3");testList.add("测试数据4");testList.add("测试数据5");testList.add("测试数据6");testList.add("测试数据7");List<Integer> areaList = new ArrayList<Integer>();// 第二行,测试值areaList.add(-10);areaList.add(-90);areaList.add(98);areaList.add(95);areaList.add(85);areaList.add(77);areaList.add(32);//创建一个画布XSSFDrawing drawing = sheet.createDrawingPatriarch();//前四个默认0,[0,10]:从0列10行开始;[10,30]:宽度10个单元格,30向下扩展到30行XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 10, 10, 30);//创建一个chart对象XSSFChart chart = drawing.createChart(anchor);//标题chart.setTitleText("测试标题");//标题覆盖chart.setTitleOverlay(false);//图例位置XDDFChartLegend legend = chart.getOrAddLegend();legend.setPosition(LegendPosition.TOP);//分类轴标(X轴),标题位置XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.TOP);bottomAxis.setTitle("X轴标题");//值(Y轴)轴,标题位置XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);leftAxis.setTitle("Y轴标题");//CellRangeAddress(起始行号,终止行号, 起始列号,终止列号)//分类轴标(X轴)数据,单元格范围位置[0, 0]到[0, 6]
//          XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, 6));//直接传入数据处理XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(testList.toArray(new String[testList.size()]));//数据1,单元格范围位置[1, 0]到[1, 6]
//          XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6));//直接传入数据处理XDDFNumericalDataSource<Integer> area = XDDFDataSourcesFactory.fromArray(areaList.toArray(new Integer[areaList.size()]));//bar:条形图,XDDFChartData可以看看可以实现的视图种类XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);//设置为可变颜色bar.setVaryColors(false);//条形图方向,纵向/横向:纵向bar.setBarDirection(BarDirection.COL);//图表加载数据,条形图1XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(countries, area);//条形图例标题series1.setTitle("测试", null);XDDFSolidFillProperties fill = new XDDFSolidFillProperties();//条形图,填充颜色series1.setFillProperties(fill);CTPlotArea plotArea = chart.getCTChart().getPlotArea();//绘制chart.plot(bar);//柱状图1上显示数值plotArea.getBarChartArray(0).getSerArray(0).addNewDLbls();plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowVal().setVal(true);plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowLegendKey().setVal(false);plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowCatName().setVal(false);plotArea.getBarChartArray(0).getSerArray(0).getDLbls().addNewShowSerName().setVal(false);//***** 非常重要,可以设置excel中的对应(以互补色代表负值),对于条形图有负数的情况,一定要设置,否则颜色无法填充plotArea.getBarChartArray(0).getSerArray(0).addNewInvertIfNegative().setVal(false);// 将输出写入excel文件String tmpFileName = "/data/tmp/" + System.currentTimeMillis() + ".xlsx";FileUtil.createNewFile(tmpFileName);fileOut = new FileOutputStream(tmpFileName);wb.write(fileOut);} catch (Exception e) {e.printStackTrace();} finally {wb.close();if (fileOut != null) {fileOut.close();}}}
}

------------------*明显的分割线-------------------------
重要提示::如果条形图中有负值的情况,请务必设置这个
plotArea.getBarChartArray(0).getSerArray(0).addNewInvertIfNegative().setVal(false);
坑我已经踩过了,记录一下

------------------------------------------条形图End-------------------------------------------

三、散点图实现-----对应Excel中的XY散点图

------------------------------------------散点图Start-------------------------------------------

3.1初始数据准备

自己组装数据同样可以作为原始数据去画图,具体代码注释

3.2实现效果展示

3.3代码实现如下:

// 处理散点图
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.PresetLineDash;
import org.apache.poi.xddf.usermodel.XDDFLineProperties;
import org.apache.poi.xddf.usermodel.XDDFPresetLineDash;
import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
import org.apache.poi.xddf.usermodel.chart.AxisCrossBetween;
import org.apache.poi.xddf.usermodel.chart.AxisCrosses;
import org.apache.poi.xddf.usermodel.chart.AxisPosition;
import org.apache.poi.xddf.usermodel.chart.ChartTypes;
import org.apache.poi.xddf.usermodel.chart.LegendPosition;
import org.apache.poi.xddf.usermodel.chart.XDDFCategoryDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFScatterChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;/*** @Author Lee* @Date 2021/3/20* POI实现Excel条形图**/
public class XSSFUtils {public static void main(String[] args) throws Exception{String tmpFileName = "/data/tmp/testzhexiantu.xlsx";File file = new File(tmpFileName);System.out.println(file.exists());InputStream fis = new FileInputStream(file);XSSFWorkbook wb = new XSSFWorkbook(fis);XSSFSheet sheet = wb.getSheetAt(0);//创建一个画布XSSFDrawing drawing = sheet.createDrawingPatriarch();//前四个默认0,[0,25]:从0列25行开始;[15,45]:宽度15个单元格,45向下扩展到45行ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 25, 15, 45);//创建一个chart对象XSSFChart chart = drawing.createChart(anchor);//标题chart.setTitleText("测试标题1");//标题覆盖chart.setTitleOverlay(false);//图例位置XDDFChartLegend legend = chart.getOrAddLegend();legend.setPosition(LegendPosition.TOP);//分类轴标(X轴),标题位置   //****** 如果是多条线,且X轴时数值类型的,切记::::要用XDDFValueAxisXDDFValueAxis bottomAxis = chart.createValueAxis(AxisPosition.TOP);
//          //****** 如果是多条线,用XDDFCategoryAxis,会出现乱码情况//XDDFCategoryAxis bottomAxis = chart.createXCategoryAxis(AxisPosition.TOP);bottomAxis.setTitle("测试标题2");//值(Y轴)轴,标题位置XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.BOTTOM);leftAxis.setTitle("测试标题3");//CellRangeAddress(起始行号,终止行号, 起始列号,终止列号)//分类轴标(X轴)数据,单元格范围位置[0, 0]到[0, 6]XDDFNumericalDataSource<Double> x = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 7, 0, 0));
//          //已有数值处理//XDDFNumericalDataSource<Integer> x = XDDFDataSourcesFactory.fromArray(xList.toArray(new Integer[xList.size()]));//数据,单元格范围位置[1, 0]到[1, 6]
//          XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6));//XDDFNumericalDataSource<Integer> area = XDDFDataSourcesFactory.fromArray(new Integer[] {17098242,9984670,9826675,9596961,8514877,7741220,3287263});XDDFNumericalDataSource<Double> y = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 7, 1, 1));//已有数值处理//XDDFNumericalDataSource<Integer> y = XDDFDataSourcesFactory.fromArray(yList.toArray(new Integer[yList.size()]));//          XDDFNumericalDataSource<Integer> x1 = XDDFDataSourcesFactory.fromArray(x1List.toArray(new Integer[x1List.size()]));//scatter:XY散点图XDDFScatterChartData scatter = (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);//设置为可变颜色//         //图表加载数据,条形图1XDDFSolidFillProperties fill = new XDDFSolidFillProperties();XDDFScatterChartData.Series series1 = (XDDFScatterChartData.Series) scatter.addSeries(x, y);
//          //条形图例标题series1.setTitle("测试1", null);XDDFCategoryDataSource x1 = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(12, 14, 0, 0));XDDFNumericalDataSource<Double> y1 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(12, 14, 1, 1));//图表加载数据,条形图1XDDFScatterChartData.Series series2 = (XDDFScatterChartData.Series) scatter.addSeries(x1, y1);
//          //条形图例标题series2.setTitle("测试2", null);
//          scatter.addSeries(x1, y1);//条形图,填充颜色series2.setFillProperties(fill);XDDFNumericalDataSource<Double> x2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(19, 21, 0, 0));XDDFNumericalDataSource<Double> y2 = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(19, 21, 1, 1));
//          //图表加载数据,条形图1XDDFScatterChartData.Series series3 = (XDDFScatterChartData.Series) scatter.addSeries(x2, y2);
//          //条形图例标题series3.setTitle("测试3", null);
//          XDDFLineProperties line4 = new XDDFLineProperties();line4.setPresetDash(new XDDFPresetLineDash(PresetLineDash.DASH_DOT));//虚线series3.setLineProperties(line4);//条形图,填充颜色series3.setFillProperties(fill);//绘制chart.plot(scatter);FileOutputStream fileOut = new FileOutputStream(tmpFileName);wb.write(fileOut);wb.close();}
}

重要提示::如果是多条线,且X轴时数值类型的,切记:::要用XDDFValueAxis
如果使用XDDFCategoryAxis,X轴会把每个值作为一个点来处理,X轴会出现韩文,乱码
坑我已经踩过了,记录一下

------------------------------------------散点图End-------------------------------------------

POI处理Excel,条形图,散点图,折线图相关推荐

  1. POI EXCEL 图表、折线图、条形图,柱状图、饼图、散点图

    POI Word生成图表:POI Word 图表.柱状图.条形图.折线图.饼图_u014644574的博客-CSDN博客_poi word 图表 1.pom.xml <dependency> ...

  2. Excel实例:Excel图表可视化:条形图、折线图、散点图和步骤图

    原文链接:http://tecdat.cn/?p=16539 Excel提供了相当广泛的功能来创建图形,即Excel所谓的  图表.您可以通过选择插入>图表来访问Excel的图表功能  .我们将 ...

  3. 地图图表、柱状图、条形图、折线图、中国地图、世界地图、省市地图、仪表盘、雷达图、饼图、散点图、气泡图、瀑布图、堆叠图、热力图、桑基图、关系图、漏斗图、Axure原型、rp原型、产品原型

    地图图表.柱状图.条形图.折线图.中国地图.世界地图.省市地图.仪表盘.雷达图.饼图.散点图.瀑布图.气泡图.堆叠图.热力图.桑基图.关系图.漏斗图.Axure原型.rp原型.产品原型.大屏设计必备组 ...

  4. 可视化框架、Axure原型、大屏可视化、图表组件、图表元件库、统计图表、数据可视化模板、条形图、折线图、散点图、时间轴、仪表盘、饼图、散点图、雷达图、高山图、登录模板、弹窗、弹幕、预警、散点图

    可视化框架.数据可视化综合管理平台.大屏可视化.图表组件.图表元件库.统计图表.数据可视化模板.条形图.折线图.散点图.时间轴.仪表盘.饼图.散点图.雷达图.高山图.登录模板.弹窗.弹幕.预警.散点图 ...

  5. matlab散点图折线图_什么是散点图以及何时使用

    matlab散点图折线图 When you were learning algebra back in high school, you might not have realized that on ...

  6. Py之Seaborn:数据可视化Seaborn库的柱状图、箱线图(置信区间图)、散点图/折线图、核密度图/等高线图、盒形图/小提琴图/LV多框图的组合图/矩阵图实现

    Py之Seaborn:数据可视化Seaborn库的柱状图.箱线图(置信区间图).散点图/折线图.核密度图/等高线图.盒形图/小提琴图/LV多框图的组合图/矩阵图实现 目录

  7. graphics | 基础绘图系统(七)——各式各样的散点图/折线图

    前面已经用了六篇推文系统地介绍了R语言的基础绘图系统的主要函数用法,以及柱状图.直方图.箱形图.扇形图等常见图形的绘制方法,接下来将计划用四篇推文介绍使用基础绘图系统能够绘制的其他各类图形. 本篇介绍 ...

  8. MATLAB的动态图,伪色彩图、矢量图、流线图,散点图折线图的绘制例子--流体力学;LBM

    还是老规矩先宣传一下QQ群群: 格子玻尔兹曼救星:293267908.免费群!一切为了早日毕业. 最近群友问画图的挺多,动态图,伪色彩图.矢量图.流线图,散点图折线图..我在这里贡献一下自己的MATL ...

  9. echarts+vue——散点图+折线图——技能提升

    最近在写后台管理系统时,遇到一个需求,就是散点图+折线图做一个图表,由于之前没有接触过散点图,因此下面记录一下: 在echarts官网上可以看到散点图需要注意的内容如下: 因此组装数据是非常关键的. ...

  10. Android 开发第三方框架制作条形图、折线图、饼状图、水平条形图

    Android 第三方框架MPAndroidChart-v3.1.0的简单使用 这里写目录标题 Android 第三方框架MPAndroidChart-v3.1.0的简单使用 前言 引入库 布局 初始 ...

最新文章

  1. Java专家系列:CPU Cache与高性能编程
  2. php中静态方法的调用,php中静态方法和非静态方法如何相互调用?
  3. OPENCV背景细分background segmentation的实例(附完整代码)
  4. Linux环境进程间通信(五): 共享内存(上)
  5. 有问有答 | 分布式服务框架精华问答
  6. 原创]Windows Gdi入门初级应用(VC SDK)
  7. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_8_File类遍历(文件夹)目录功能...
  8. 3808. 画正方形——AcWing题库
  9. 关于EditPlus3 取消备份后再重新打开 备份设置还原到默认状态的问题
  10. uni —app 录音_uniapp如何实现录音功能
  11. 营业执照争夺背后:吴忌寒正在重塑比特大陆 |链捕手
  12. 利用特性、泛型、反射生成sql操作语句(待修改
  13. 常见的常微分方程的一般解法
  14. canvas 实现图片局部模糊_HTML5 Canvas图片马赛克模糊动画
  15. h5文件格式数据集制作
  16. java excel 边框颜色_poi生成excel整理(设置边框/字体/颜色/加粗/居中/)[转]
  17. python1加到100_python for循环1加到100的和
  18. mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
  19. 694. Number of Distinct Islands
  20. iOS 连接外设的几种方式

热门文章

  1. Codeforces 1293 E. Xenon‘s Attack on the Gangs —— 树上记忆化搜索,单点加改成区间加,有丶东西
  2. 在计算机术语中 英文cad是指,CAD中英语词汇及命令大全
  3. 还在学Django! FastAPI Web 框架教程来了!
  4. 编译原理——实现NFA到DFA 的转换(子集构造法)
  5. android快速充电,快到令人发指 外媒评14款安卓机充电速度
  6. 应用软件安全编程概述
  7. 台式机显示 “未连接到互联网“解决办法
  8. 红米4 android os唤醒,红米4(Redmi 4 高配版)一键救砖教程,轻松刷回官方系统
  9. STA X(X为主存地址)指令发出的微操作命令
  10. 麦当劳巨无霸汉堡合作超人气动漫《机动战士高达》