Java利用Apache poi导出图表
jar
compile('org.apache.poi:poi:4.0.1')
compile('org.apache.poi:poi-scratchpad:4.0.1')
compile('org.apache.poi:poi-ooxml:4.0.1')
compile('org.apache.poi:ooxml-schemas:1.4') public static class Inbound_chartExport{
public XSSFSheet sheet;//操作的sheet
public String title;//头部标题
public String seriesTitle;//系列标题
public String xName;//x轴标题
public String yName;
public int startRow;//开始行数
public int size;//结束行数
public int xStartCol;//x轴列数
public int yStartCol;
public int col1;//图标放置位置
public int row1;
public int col2;
public int row2;
}
public void excelBarChart(Inbounds.Inbound_chartExport inbound){
XSSFDrawing drawing = inbound.sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, inbound.col1, inbound.row1, inbound.col2, inbound.row2);
XSSFChart chart = drawing.createChart(anchor);//图表
chart.setTitleText(inbound.title);
chart.setTitleOverlay(false);
XDDFChartLegend legend = chart.getOrAddLegend();//图例项
legend.setPosition(LegendPosition.TOP_RIGHT);
// Use a category axis for the bottom axis.
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);//x轴和位置
bottomAxis.setTitle(inbound.xName);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);//y轴和位置
leftAxis.setTitle(inbound.yName);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);//生成Data
XDDFDataSource<String> xs = XDDFDataSourcesFactory.fromStringCellRange(inbound.sheet, new CellRangeAddress(inbound.startRow, inbound.size, inbound.xStartCol, inbound.xStartCol));//x轴range区域
XDDFNumericalDataSource<Double> ys1 = XDDFDataSourcesFactory.fromNumericCellRange(inbound.sheet, new CellRangeAddress(inbound.startRow, inbound.size, inbound.yStartCol, inbound.yStartCol));//y轴range区域
XDDFChartData.Series series1 = data.addSeries(xs, ys1);//生成Series 系列/维度
series1.setTitle(inbound.seriesTitle, null);
chart.plot(data);
XDDFBarChartData bar = (XDDFBarChartData) data;//由数据生成图表,Bar柱状图,Pie饼状图,Line折线图,Scatter散点图
bar.setBarDirection(BarDirection.BAR);//柱状如方向 BAR 竖向 COL横向
CTChart ctChart = chart.getCTChart();
CTPlotArea ctPlotArea = ctChart.getPlotArea();
CTBarChart ctBarChart = ctPlotArea.getBarChartArray(0);
CTBarSer ctBarSer = ctBarChart.getSerArray(0);
CTDLbls newDLbls = ctBarSer.addNewDLbls();//数据标签
CTBoolean ctBoolean = ctBarChart.addNewVaryColors();//多样颜色,true 选用category作为图例项,false选用系列作为图例项,最好false
ctBoolean.setVal(false);
newDLbls.setShowCatName(ctBoolean);//数据标签 显示类别名称
newDLbls.setShowSerName(ctBoolean);//数据标签 显示序列名称
newDLbls.setShowPercent(ctBoolean);
newDLbls.setShowBubbleSize(ctBoolean);
newDLbls.setShowLeaderLines(ctBoolean);
newDLbls.setShowLegendKey(ctBoolean);//图例化标签
newDLbls.addNewShowVal().setVal(true);//数据标签 显示值
solidFillSeries(data, 0, PresetColor.CHARTREUSE);//颜色PresetColor
}
private static void solidFillSeries(XDDFChartData data, int index, PresetColor color) {
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(color));
XDDFChartData.Series series = data.getSeries().get(index);
XDDFShapeProperties properties = series.getShapeProperties();
if (properties == null) {
properties = new XDDFShapeProperties();
}
properties.setFillProperties(fill);
series.setShapeProperties(properties);
}
Excel导出(Java)资料
柱状图(poi 3.17):https://stackoverflow.com/questions/38913412/create-bar-chart-in-excel-with-apache-poi
饼图(poi 3.17):https://stackoverflow.com/questions/34718734/apache-poi-supports-only-scattercharts-and-linecharts-why
利用导入的模板导出(poi 3.17之前):https://blog.cacoveanu.com/2015/2015.08.27.15.15.charts.apache.poi.html
poi 4.0.1chart(Example):https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/
官方文档:https://poi.apache.org/apidocs/dev/org/apache/poi/xddf/usermodel/chart/package-summary.html
中文事例:https://blog.csdn.net/qq_24365145/article/details/89146549
https://blog.csdn.net/Lonely_boy_/article/details/88870079
Java利用Apache poi导出图表的更多相关文章
- java 通过Apache poi导出excel代码demo实例
package com.zuidaima.excel.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutput ...
- Java利用Apache POI将数据库数据导出为excel
将数据库中的数据导出为excel文件,供其他人查看 public class POITest { public static void main(String[] args) { POITest te ...
- 使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...
- Read / Write Excel file in Java using Apache POI
Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year o ...
- 利用Apache POI 实现简单的Excel表格导出
1.利用POI API实现简单的Excel表格导出 首先假设一个学生实体类: package com.sun.poi.domain; import java.io.Serializable; impo ...
- Java使用Apache POI进行Excel导入和导出
Manve依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> ...
- apache poi导出excel报表
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...
- java中使用poi导出excel表格数据并且可以手动修改导出路径
在我们开发项目中,很多时候会提出这样的需求:将前端的某某数据以excel表格导出,今天就给大家写一个简单的模板. 这里我们选择使用poi导出excel: 第一步:导入需要的jar包到 lib 文件夹下
- Java中用Apache POI生成excel和word文档
概述: 近期在做项目的过程中遇到了excel的数据导出和word的图文表报告的导出功能.最后决定用Apache POI来完毕该项功能.本文就项目实现过程中的一些思路与代码与大家共享.同一时候.也作为自 ...
随机推荐
- Codeforces Round #486 (Div. 3) C "Equal Sums" (map+pair<>)
传送门 •题意 给k个数列,从中k个数列中找出任意2个数列 i ,j 使得数列i删除第x个数,和数列j删除第y个数的和相等 若存在,输出 i ,x 和 j,y •思路 每个数列之间的联系为数列的和之间 ...
- Asp.Net MVC HttpPost用法
一个Action只能用一个http 特性,例如:HttpPost 不能与HttpGet 或者多个HttpPost重复使用,否则会出错 也可以用 [AcceptVerbs("put" ...
- Java的编译原理
概述 java语言的"编译期"分为前端编译和后端编译两个阶段.前端编译是指把*.java文件转变成*.class文件的过程; 后端编译(JIT, Just In Time Comp ...
- 无法正常卸载pr
控制面板找不到pr和ps,根本无法卸载,我试过官方工具没用,也试过ccleaner,也检测不到?
- snort规则中byte_test参数详解
例子: byte_test:4,>,1000,20 这里是从本规则内前面匹配的位置结尾开始,向后偏移20个字节,再获取后面的4个字节的数据,与十进制数据1000进行比较,如果大于1000,就命中 ...
- 不等"金九银十",金风八月,我早已拿下字节跳动的offer
字节跳动,我是在网上投的简历,之前也投过一次,简历都没通过删选,后来让师姐帮我改了一下简历,重新投另一个部门,获得了面试机会.7月23日,中午HR打电话过来预约了下午4点半面试,说会在线写代码,让我准 ...
- 【Java例题】7.1 线程题1-时间显示线程
1.时间显示线程.设计一个示线程子类,每秒钟显示一次当前时间:然后编写主类,在主函数中定义一个线程对象,并启动这个线程 package chapter7; import java.text.Simpl ...
- docker 容器之间互联
容器之间的互联 一. 实验目的: 1. 熟悉容器之间基本的网络原理: 2. 掌握容器之间互联的方法: 二. 实验环境: Ubuntu16.04+Docker 三. 实验内容: ...
- sql存储过程中循环批量插入
前几天有一个需求很头痛,部门是有上下级关系的,在给部门的经理赋予角色和权限的时候,通常我们都会认为假如经理A的部门是1,那么我给了他部门1 的管理权限,那么1的下级部门101,102,103 &quo ...
- Spark 系列(十四)—— Spark Streaming 基本操作
一.案例引入 这里先引入一个基本的案例来演示流的创建:获取指定端口上的数据并进行词频统计.项目依赖和代码实现如下: <dependency> <groupId>org.apac ...