仅通过POI的话,只能实现折线图和散点图
但POI还支持Open Xml,通过这种方式可以实现很多类型的chart
(https://blog.csdn.net/u011279583/article/details/105196805)

效果参考:

POI版本

 <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>ooxml-schemas</artifactId><version>1.3</version></dependency>

代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.apache.poi.ss.usermodel.charts.AxisCrosses;
import org.apache.poi.ss.usermodel.charts.AxisPosition;
import org.apache.poi.ss.usermodel.charts.ChartDataSource;
import org.apache.poi.ss.usermodel.charts.DataSources;
import org.apache.poi.ss.usermodel.charts.LegendPosition;
import org.apache.poi.ss.usermodel.charts.LineChartSeries;
import org.apache.poi.ss.util.CellRangeAddress;
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.charts.XSSFChartAxis;
import org.apache.poi.xssf.usermodel.charts.XSSFChartLegend;
import org.apache.poi.xssf.usermodel.charts.XSSFLineChartData;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDispBlanksAs;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.STDispBlanksAs;public class XSSFUtils {public static void test(XSSFSheet sheet) {Map<String, Object> params = new HashMap<String, Object> ();// 图表位置(B36左上角:AA53左上角)int[] position = new int[] {getColumnIndexByAddress("B"), 35, getColumnIndexByAddress("AA"), 52};// x轴坐标区域(B60:B90)int[] xAxisRange = new int[] {59, 89, getColumnIndexByAddress("B"), getColumnIndexByAddress("B")};// 每个系列的数据(D60:D90、J60:J90、P60:P90)// 每个系列的标题(D59、J59、P59)List<Integer[]> seriesDataRangeList = new ArrayList<Integer[]>();seriesDataRangeList .add(new Integer[] {59, 89, getColumnIndexByAddress("D"), getColumnIndexByAddress("D"),58, getColumnIndexByAddress("D")});seriesDataRangeList .add(new Integer[] {59, 89, getColumnIndexByAddress("J"), getColumnIndexByAddress("J"),58, getColumnIndexByAddress("J")});seriesDataRangeList .add(new Integer[] {59, 89, getColumnIndexByAddress("P"), getColumnIndexByAddress("P"),58, getColumnIndexByAddress("P")});params.put("chartPosition", position);params.put("xAxisRange", xAxisRange);params.put("seriesDataRangeList ", seriesDataRangeList );createLineChart(sheet, params);}/*** create line chart* @param sheet* @param params*             chartPosition            int[]{startRow, endRow, startCol, endCol}*             xAxisRange               int[]{startRow, endRow, startCol, endCol}*             seriesDataRangeList      List<Integer[]>{startRow, endRow, startCol, endCol, titleRow, titleCol}*/private static void createLineChart(XSSFSheet sheet, Map<String, Object> params) {int[] position = (int[]) params.get("chartPosition");int[] xAxisRange = (int[]) params.get("xAxisRange");List<Integer[]> seriesDataRangeList = (List<Integer[]>) params.get("seriesDataRangeList ");XSSFDrawing drawing = sheet.createDrawingPatriarch();XSSFClientAnchor anchor =drawing.createAnchor(0, 0, 0, 0, position[0], position[1], position[2], position[3]);XSSFChart chart = drawing.createChart(anchor);XSSFChartLegend legned = chart.getOrCreateLegend();legned.setPosition(LegendPosition.TOP);// set blank values as gapsCTDispBlanksAs disp = CTDispBlanksAs.Factory.newInstance();disp.setVal(STDispBlanksAs.GAP);chart.getCTChart().setDispBlanksAs(disp);XSSFLineChartData chartData = chart.getChartDataFactory().createLineChartData();XSSFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);XSSFChartAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);bottomAxis.setCrosses(AxisCrosses.AUTO_ZERO);bottomAxis.setNumberFormat("yyyy/m/d");leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);leftAxis.setMinimum(0);// 设置x轴坐标区域(B60:B90)ChartDataSource<String> xAxisData =DataSources.fromStringCellRange(sheet,new CellRangeAddress(xAxisRange[0], xAxisRange[1], xAxisRange[2], xAxisRange[3]));// 设置数据区域,即每个系列的数据(D60:D90、J60:J90、P60:P90)for (Integer[] seriesDataRange : seriesDataRangeList ) {ChartDataSource<Number> yAxisData =DataSources.fromNumericCellRange(sheet,new CellRangeAddress(seriesDataRange[0], seriesDataRange[1], seriesDataRange[2], seriesDataRange[3]));LineChartSeries series = chartData.addSeries(xAxisData, yAxisData);// 设置每个系列的标题(D59、J59、P59)series.setTitle(sheet.getRow(seriesDataRange[4]).getCell(seriesDataRange[5]).getStringCellValue());}chart.plot(chartData, bottomAxis, leftAxis);// unsmooth series(好像必须写在polt方法之后)CTPlotArea plotArea = chart.getCTChart().getPlotArea();for (CTLineChart ch : plotArea.getLineChartList()) {for (CTLineSer ser : ch.getSerList()) {CTBoolean ctBool = CTBoolean.Factory.newInstance();ctBool.setVal(false);ser.setSmooth(ctBool);}}}/*** get column index by column address*/public static int getColumnIndexByAddress(String columnAddress) {int colNum = 0;for (int i = 0; i < columnAddress.length(); i++) {char ch = columnAddress.charAt(columnAddress.length() - 1 - i);colNum += (ch - 'A' + 1) * Math.pow(26, i);}return colNum - 1;}
}

【POI】Java+poi生成Excel(.xlsx)折线图相关推荐

  1. apache poi 生成word 3D折线图 maven

    首先pom导入poi依赖,要用高版本的poi,我用的5.0.0是可以的.注意此处有坑,在5.0.0高版本中原来的poi-ooxml-schemas已经改名为poi-ooxml-lite<depe ...

  2. 画【Python折线图】的一百个学习报告(三、自动生成单一数据折线图)

    画[Python折线图]的一百个学习报告(三.自动生成单一数据折线图) 目录 画[Python折线图]的一百个学习报告(三.自动生成单一数据折线图) 前言 学习环境 探究目标 分析过程 实践过程 成果 ...

  3. Java后台生成Excel表格

    Java后台生成Excel表格 功能简述 需求 目标 实现 1.首先在项目中导入jxl相关jar包,并引入 2.编写代码 3.结果 后记 功能简述 由于公司业务需求,我需要实现一个完全通过Java后台 ...

  4. php制作曲线柱形图的框架,用GD图库生成横竖柱状图折线图的类_php

    最近写的一个GD图库用以生成横竖柱状图和折线图的类库,算是一个教学例程吧 Class ImageReport{ var $X;//图片大小X轴 var $Y;//图片大小Y轴 var $R;//背影色 ...

  5. java 实现生成excel表头,A-Z;AA-ZZ;AAA-ZZZ支持无限,数字转字母 ,字母转数字

    java 实现生成excel表头,A-Z;AA-ZZ;AAA-ZZZ支持无限,数字转字母 ,字母转数字 public static void main(String[] args) {String k ...

  6. Excel柱状图折线图组合怎么做 Excel百分比趋势图制作教程

    Excel柱状图折线图组合怎么做 Excel百分比趋势图制作教程 用excel作图时候经常会碰到做柱状图和折线图组合,这样的图一般难在折线图的数据很小,是百分比趋势图,所以经常相对前面主数据太小了,在 ...

  7. java使用poi在word中生成柱状图、折线图、饼图、柱状图+折线图组合图、动态表格、文本替换、图片替换、更新内置Excel数据、更新插入的文本框内容、合并表格单元格;

    本文参考地址:https://blog.csdn.net/wangxiaoyingWXY/article/details/95377533 在参考文章的基础上,增加了扩展.感谢被参考的妹子.另外该博客 ...

  8. 使用POI实现读取Excel数据并生成柱状图、折线图、饼状图的工具类

    最近有一个需求是要通过程序自动根据excel中的数据来动态生成相应的图表.其中就有柱状图,折线图,饼状图.但是没想到用wps生成非常的简单,一点就可以生成,使用POI生成的过程非常的难受.不得不将这次 ...

  9. java使用poi在word中生成柱状图、折线图、饼图、柱状图+折线图组合图、动态表格、文本替换、图片替换 springboot项目

    本文参考地址:https://blog.csdn.net/u014427811/article/details/100771314 在参考文章的基础上,增加了模板样例 模板样例地址 百度网盘 链接:h ...

  10. Java使用poi和jfreechart生成excel图表图片

    最近项目在频繁的操作excel,里边涉及到很多和图表有关的东西.有时候需要使用java操作excel自带的图标,比较复杂的我们都是使用excel模板的形式实现.  除此之外,也有一些功能只需要生成对应 ...

最新文章

  1. 数据库及页面乱码问题
  2. 系统架构性能问题诊断及优化思路,纯干货!
  3. Linux C : Makefile 的编写和示例
  4. JWT令牌创建和解析讲解
  5. git遇到的问题-- Another git process seems to be running in this repository
  6. Python 中如何解决 asyncio 文件描述符最大数量限制问题
  7. Android中DisplayMetrics 获取手机屏幕分辨率
  8. sts 的js代码不变色_[黑科技] 使用 Laravel Livewire 来构建实时搜索功能(不使用一行 JS 代码)...
  9. 【JDBC】JDBC的使用(数据库的增删改查询)
  10. c语言实现统计过程控制,SPC统计过程控制的课程
  11. Excel查看多列重复项
  12. 权限管理实现——权限过滤器
  13. easyui-filebox java上传附件,在EasyUI项目中使用FileBox控件实现文件上传处理
  14. python绘图设置时间坐标轴_Matplotlib绘图双纵坐标轴设置及控制设置时间格式
  15. iOS复习中有关SDWebImage可能知识点总结
  16. wps 制作 组织结构图
  17. 证件照制作v2.9.32
  18. 拼多多2亿美元战略投资国美,家电市场将会如何变化?
  19. 广告业务系统 之 辅助决策 —— “ AB 实验平台”
  20. html静态页面作业——京东网购商城模板(8页) HTML+CSS+JavaScript 学生DW网页设计作业成品 HTML网页设计制作大作业

热门文章

  1. 大势前瞻!文旅还是短视频,你弯道超车风口在这了
  2. 程序员常用网站汇总记录(持续更新)
  3. 程序员的生活需要仪式感
  4. 24 分钟让 AI 跑起飞车类游戏
  5. 过程控制系统中的模块技术MTP
  6. 【Excel】csv文件修改分隔符
  7. 图解 Deployment Controller 工作流程
  8. OpenCV---calcHist函数说明
  9. 在DELL G3-3500上安装win10+Ubuntu双系统
  10. R语言描述性统计:使用mean函数计算dataframe数据中指定数据列的均值