首页 > 其他分享 >POI 重叠、并列柱状图(条形图),显示数据,自定义颜色

POI 重叠、并列柱状图(条形图),显示数据,自定义颜色

时间:2024-05-16 16:11:35浏览次数:28  
标签:自定义 createCell cell 柱状图 POI org byte setCellValue row

1、pom.xml

<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>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>


2、poi 柱状图、条形图自定义颜色,重叠条形图,显示数值。
package com.itcodai.course01.entity;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.PresetColor;
import org.apache.poi.xddf.usermodel.XDDFColor;
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.BarGrouping;
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.XDDFChartLegend;
import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
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.CTBarSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDPt;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos;
public class test { public static void main(String[] args) throws IOException { XSSFWorkbook wb = new XSSFWorkbook();
String sheetName = "Sheet1";
FileOutputStream fileOut = null;
try { XSSFSheet sheet = wb.createSheet(sheetName);
//第一行,国家名称
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("俄罗斯");
cell = row.createCell(1);
cell.setCellValue("加拿大");
cell = row.createCell(2);
cell.setCellValue("美国");
cell = row.createCell(3);
cell.setCellValue("中国");
cell = row.createCell(4);
cell.setCellValue("巴西");
cell = row.createCell(5);
cell.setCellValue("澳大利亚");
cell = row.createCell(6);
cell.setCellValue("印度");
// 第二行,乡村地区
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue(17098242);
cell = row.createCell(1);
cell.setCellValue(9984670);
cell = row.createCell(2);
cell.setCellValue(9826675);
cell = row.createCell(3);
cell.setCellValue(9596961);
cell = row.createCell(4);
cell.setCellValue(8514877);
cell = row.createCell(5);
cell.setCellValue(7741220);
cell = row.createCell(6);
cell.setCellValue(3287263);
// 第三行,农村人口
row = sheet.createRow(2);
cell = row.createCell(0);
cell.setCellValue(14590041);
cell = row.createCell(1);
cell.setCellValue(35151728);
cell = row.createCell(2);
cell.setCellValue(32993302);
cell = row.createCell(3);
cell.setCellValue(14362887);
cell = row.createCell(4);
cell.setCellValue(21172141);
cell = row.createCell(5);
cell.setCellValue(25335727);
cell = row.createCell(6);
cell.setCellValue(13724923);
// 第四行,面积平局
row = sheet.createRow(3);
cell = row.createCell(0);
cell.setCellValue(9435701.143);
cell = row.createCell(1);
cell.setCellValue(9435701.143);
cell = row.createCell(2);
cell.setCellValue(9435701.143);
cell = row.createCell(3);
cell.setCellValue(9435701.143);
cell = row.createCell(4);
cell.setCellValue(9435701.143);
cell = row.createCell(5);
cell.setCellValue(9435701.143);
cell = row.createCell(6);
cell.setCellValue(9435701.143);
// 第四行,人口平局
row = sheet.createRow(4);
cell = row.createCell(0);
cell.setCellValue(22475821.29);
cell = row.createCell(1);
cell.setCellValue(22475821.29);
cell = row.createCell(2);
cell.setCellValue(22475821.29);
cell = row.createCell(3);
cell.setCellValue(22475821.29);
cell = row.createCell(4);
cell.setCellValue(22475821.29);
cell = row.createCell(5);
cell.setCellValue(22475821.29);
cell = row.createCell(6);
cell.setCellValue(22475821.29);
//创建一个画布
XSSFDrawing drawing = sheet.createDrawingPatriarch();
//前四个默认0,[0,5]:从0列5行开始;[7,26]:到7列26行结束
//默认宽度(14-8)*12
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 7, 26);
//创建一个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.BOTTOM);
bottomAxis.setTitle("国家");
//值(Y轴)轴,标题位置
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setTitle("面积和人口");
//CellRangeAddress(起始行号,终止行号, 起始列号,终止列号)
//分类轴标(X轴)数据,单元格范围位置[0, 0]到[0, 6]
XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, 6));
// XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(new String[] {"俄罗斯","加拿大","美国","中国","巴西","澳大利亚","印度"});
//数据1,单元格范围位置[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});
//数据2,单元格范围位置[2, 0]到[2, 6]
XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, 6));
//bar:条形图,
XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
//设置为可变颜色
bar.setVaryColors(true);
//条形图方向,纵向/横向:纵向
bar.setBarDirection(BarDirection.COL);
//图表加载数据,条形图1
XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(countries, area);
//条形图例标题
series1.setTitle("面积", null);
XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.RED));
//条形图,填充颜色
series1.setFillProperties(fill);
//图表加载数据,条形图2
XDDFBarChartData.Series series2 = (XDDFBarChartData.Series) bar.addSeries(countries, population);
//条形图例标题
series2.setTitle("人口", null);
XDDFSolidFillProperties fill2 = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.BLUE));
//条形图,填充颜色
series2.setFillProperties(fill2);
CTPlotArea plotArea = chart.getCTChart().getPlotArea();
//绘制
chart.plot(bar);
// 堆积条形图,将2个条形图重叠起来
bar.setBarGrouping(BarGrouping.STACKED);
//修正重叠,使钢筋真正堆叠而不是并排
plotArea.getBarChartArray(0).addNewOverlap().setVal((byte) 100);
//柱状图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);
//柱状图2上显示数值
plotArea.getBarChartArray(0).getSerArray(1).addNewDLbls();
plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowVal().setVal(true);
plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowLegendKey().setVal(false);
plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowCatName().setVal(false);
plotArea.getBarChartArray(0).getSerArray(1).getDLbls().addNewShowSerName().setVal(false);
//给每个条形图设置颜色
CTBarSer ser = plotArea.getBarChartArray(0).getSerArray(0);
//黑色
CTDPt dpt = ser.addNewDPt();
dpt.addNewIdx().setVal(0);
dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 0 });
//红色
dpt = ser.addNewDPt();
dpt.addNewIdx().setVal(1);
dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 255, (byte) 0, (byte) 0 });
//蓝色
dpt = ser.addNewDPt();
dpt.addNewIdx().setVal(2);
dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 255 });
//绿色
dpt = ser.addNewDPt();
dpt.addNewIdx().setVal(3);
dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 128, (byte) 0 });
//黄色
dpt = ser.addNewDPt();
dpt.addNewIdx().setVal(4);
dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 255, (byte) 255, (byte) 0 });
//紫色
dpt = ser.addNewDPt();
dpt.addNewIdx().setVal(5);
dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 80, (byte) 0, (byte) 80 });
//棕色
dpt = ser.addNewDPt();
dpt.addNewIdx().setVal(6);
dpt.addNewSpPr().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 165, (byte) 42, (byte) 42 });
//边框
//ser.getSpPr().addNewLn().addNewSolidFill().addNewSrgbClr().setVal(new byte[] { (byte) 0, (byte) 0, (byte) 0 });
// 打印图表的xml
//System.out.println(chart.getCTChart());
// 将输出写入excel文件
String filename = "排行榜前七的国家.xlsx";
fileOut = new FileOutputStream(filename);
wb.write(fileOut);
} catch (Exception e) { e.printStackTrace();
} finally { wb.close();
if (fileOut != null) { fileOut.close();
} } } }

标签:自定义,createCell,cell,柱状图,POI,org,byte,setCellValue,row
From: https://www.cnblogs.com/youngnote/p/18196133

相关文章

  • ansible自定义模板部署apache服务
    使用Ansible来部署Apache服务是一个很好的选择,因为它可以自动化部署过程,确保所有的服务器上都有相同的配置。以下是一个简单的步骤指南,展示如何使用Ansible来部署Apache服务:1创建角色目录首先,在/etc/ansible/roles下创建apache目录:mkdir-p/etc/ansible/roles/apache2......
  • C++:自定义异常
    #include<iostream>#include<stdexcept>//自定义异常类classMyException:publicstd::exception{public://重写what()函数以提供异常的描述,const表示函数不会改变类的成员变量,noexcept表示不会抛出异常constchar*what()constnoexceptoverride{......
  • 使用joinjs绘制流程图(九)-实战-绘制流程图+节点路径自定义升级版
    效果图自定义路径设计本示例假设当前节点和其他节点不在同一纵坐标的情况下代码<template><divclass="app"><divref="myholder"id="paper"></div></div></template><script>import*asjointfrom'@joint/......
  • OpenPCDet训练自定义数据
    官网也提供了步骤,这里详细介绍下训练自己数据的过程以及中间遇到的一些问题。训练模型这里采用PointRCNN,具体的介绍参考:https://www.cnblogs.com/xiaxuexiaoab/p/18033887一、准备数据集数据集这一块我们需要准备好原始点云数据、物体目标标注文件、以及训练和验证对应的索引号,......
  • MindSponge分子动力学模拟——自定义控制器(2024.05)
    技术背景分子动力学模拟中的控制器(Controller)可以被用于修改模拟过程中的原子坐标和原子速度等参量,从而达到控制系统特定参量的目的。例如控温器可以用于实现NVT系综,控压器可用于实现NPT系综。而在MindSponge分子动力学模拟框架下,控温控压都可以基于控制器Controller来实现。关于......
  • fiddler自定义规则
     请求前拦截某个请求staticfunctionOnBeforeRequest(oSession:Session){if(oSession.url.indexOf("initialCid=")>-1&&oSession.url.indexOf("dm=cd")>-1){oSession.oRequest.FailSession(666,"Blocked",&......
  • 【django学习-24】自定义插件
    1.ModelForm可以帮助我们生成HTML标签,这种是普通的标签classUserModelForm(forms.ModelForm):classMeta:model=models.UserInfofields=["name","password",]form=UserModelForm()2.如果我们要使用bootstrap的标签,该怎么操作呢?2.1:自定义......
  • java poi之XWPFDocument读取word内容并创建新的word(获取表格所有图片)
    Poi的Word文档结构介绍1、poi之word文档结构介绍之正文段落一个文档包含多个段落,一个段落包含多个Runs,一个Runs包含多个Run,Run是文档的最小单元获取所有段落:Listparagraphs=word.getParagraphs();获取一个段落中的所有Runs:ListxwpfRuns=xwpfParagraph.getRuns();获取......
  • TypeScript 中,type 和 interface 定义自定义类型的区别
    在TypeScript中,type 和 interface 都用于定义自定义类型,但它们有一些不同之处。主要区别在于以下几点:语法差异:type:使用 type 关键字来定义类型别名,语法相对简洁,适合用于定义具体的类型结构或组合现有类型。interface:使用 interface 关键字来定义接口,语法更为正式,可......
  • 在 dynamic crm 中创建自定义报表
    一种是直接使用dynamiccrm内置的报表功能,报表向导进行创建,但可自定义的程度有限另一种就是使用SQLServerDataTools工具在VisualStudio中生成*.rdl文件再将文件导入dynamiccrm中进行使用在 但需要注意,dynamic2016/365/8.2,必须搭建VisualStudio2015环境......