POI Word 图表、柱状图、条形图、折线图、饼图

poi Excel 图表:https://blog.csdn.net/u014644574/article/details/105695787

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.poi</groupId><artifactId>poi-scratchpad</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 Word生成图表-柱状图

package test;import java.io.FileOutputStream;import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Units;
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.XDDFChartData;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;/*** poi Word生成图表-柱状图*/
public class CreateWordXDDFChart {// Methode to set title in the data sheet without creating a Table but using the sheet data only.// Creating a Table is not really necessary.static CellReference setTitleInDataSheet(XWPFChart chart, String title, int column) throws Exception {XSSFWorkbook workbook = chart.getWorkbook();XSSFSheet sheet = workbook.getSheetAt(0);XSSFRow row = sheet.getRow(0);if (row == null)row = sheet.createRow(0);XSSFCell cell = row.getCell(column);if (cell == null)cell = row.createCell(column);cell.setCellValue(title);return new CellReference(sheet.getSheetName(), 0, column, true, true);}public static void main(String[] args) throws Exception {try (XWPFDocument document = new XWPFDocument()) {// create the dataString[] categories = new String[] { "Lang 1", "Lang 2", "Lang 3" };Double[] valuesA = new Double[] { 10d, 20d, 30d };Double[] valuesB = new Double[] { 15d, 25d, 35d };// create the chartXWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);// create data sourcesint numOfPoints = categories.length;String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));String valuesDataRangeA = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));String valuesDataRangeB = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);XDDFNumericalDataSource<Double> valuesDataA = XDDFDataSourcesFactory.fromArray(valuesA, valuesDataRangeA, 1);XDDFNumericalDataSource<Double> valuesDataB = XDDFDataSourcesFactory.fromArray(valuesB, valuesDataRangeB, 2);// create axisXDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);// Set AxisCrossBetween, so the left axis crosses the category axis between the categories.// Else first and last category is exactly on cross points and the bars are only half visible.leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);// create chart dataXDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);((XDDFBarChartData) data).setBarDirection(BarDirection.COL);// create series// if only one series do not vary colors for each bar((XDDFBarChartData) data).setVaryColors(false);XDDFChartData.Series series = data.addSeries(categoriesData, valuesDataA);// XDDFChart.setSheetTitle is buggy. It creates a Table but only half way and incomplete.// Excel cannot opening the workbook after creatingg that incomplete Table.// So updating the chart data in Word is not possible.// series.setTitle("a", chart.setSheetTitle("a", 1));series.setTitle("a", setTitleInDataSheet(chart, "a", 1));/*// if more than one series do vary colors of the series((XDDFBarChartData)data).setVaryColors(true);series = data.addSeries(categoriesData, valuesDataB);//series.setTitle("b", chart.setSheetTitle("b", 2));series.setTitle("b", setTitleInDataSheet(chart, "b", 2));*/// plot chart datachart.plot(data);// create legendXDDFChartLegend legend = chart.getOrAddLegend();legend.setPosition(LegendPosition.LEFT);legend.setOverlay(false);// 打印图表的xml// System.out.println(chart.getCTChart());// Write the output to a filetry (FileOutputStream fileOut = new FileOutputStream("CreateWordXDDFChart.docx")) {document.write(fileOut);}}}
}

 

3、poi Word生成图表-折线图

package test;import java.io.FileOutputStream;import org.apache.poi.util.Units;
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.MarkerStyle;
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.XDDFLineChartData;
import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;/*** poi Word生成图表-折线图*/
public class CreateWordXDDFChart2 {public static void main(String[] args) throws Exception {try (XWPFDocument document = new XWPFDocument()) {// create the chartXWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);// 标题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 });// 数据1,单元格范围位置[2, 0]到[2, 6]// XDDFNumericalDataSource<Double> population = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, 6));// LINE:折线图,XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis);// 图表加载数据,折线1XDDFLineChartData.Series series1 = (XDDFLineChartData.Series) data.addSeries(countries, area);// 折线图例标题series1.setTitle("面积", null);// 直线series1.setSmooth(false);// 设置标记大小series1.setMarkerSize((short) 6);// 设置标记样式,星星series1.setMarkerStyle(MarkerStyle.STAR);// 绘制chart.plot(data);// 打印图表的xml// System.out.println(chart.getCTChart());// Write the output to a filetry (FileOutputStream fileOut = new FileOutputStream("CreateWordXDDFChart.docx")) {document.write(fileOut);}}}
}

 

4、poi Word生成图表-饼图

package test;import java.io.FileOutputStream;import org.apache.poi.util.Units;
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.XDDFChartData;
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.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;/*** poi Word生成图表-饼图*/
public class CreateWordXDDFChart2 {public static void main(String[] args) throws Exception {try (XWPFDocument document = new XWPFDocument()) {// create the chartXWPFChart chart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);// 标题chart.setTitleText("地区排名前七的国家");// 标题是否覆盖图表chart.setTitleOverlay(false);// 图例位置XDDFChartLegend legend = chart.getOrAddLegend();legend.setPosition(LegendPosition.TOP_RIGHT);// CellRangeAddress(起始行号,终止行号, 起始列号,终止列号)// 分类轴标数据,// XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, 6));XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(new String[] { "俄罗斯", "加拿大", "美国", "中国", "巴西", "澳大利亚", "印度" });// 数据1,// XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6));XDDFNumericalDataSource<Integer> values = XDDFDataSourcesFactory.fromArray(new Integer[] { 17098242, 9984670, 9826675, 9596961, 8514877, 7741220, 3287263 });// XDDFChartData data = chart.createData(ChartTypes.PIE3D, null, null);XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);// 设置为可变颜色data.setVaryColors(true);// 图表加载数据data.addSeries(countries, values);// 绘制chart.plot(data);// 打印图表的xml// System.out.println(chart.getCTChart());// Write the output to a filetry (FileOutputStream fileOut = new FileOutputStream("CreateWordXDDFChart.docx")) {document.write(fileOut);}}}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/143364.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

我计算机桌面的word图标改变了咋办,桌面上word图标异常的处理方法

今天打开电脑&#xff0c;不知什么原因&#xff0c;所有word图标都变成了灰色&#xff0c;能正常打开&#xff0c;但看起来非常不舒服&#xff0c;怎样让他恢复原来的样子呢&#xff0c;学习啦小编采取了以下几个操作步骤来恢复&#xff0c;有需要的朋友可以来看看哦。 解决Wor…

Word 图表按章节自动编号

在撰写文章&#xff0c;我们通常会遇到需要这样的问题 如何自动对各个章节的标题进行自动编号&#xff1f; 如何按照章节对图表进行编号&#xff1f; 这一问题我也遇到了很多次&#xff0c;后来在网上找了一些资料&#xff0c;发现可以使用Word中自带的多级列表进行初步解决…

java poi word 图表_Java操作Poi--word图表中颜色的设置

在做java 动态增删图表中&#xff0c;如果将原来的标签数据删除了&#xff0c;在新增标签数据&#xff0c;发现颜色没设置&#xff0c;然后找不到对应的方法。 将word模板另外存word.xml格式&#xff0c;你会发现word模板其实是一大堆标签&#xff0c;你操作的是标签。然后打开…

如何在毕业论文Word中插入图表目录

前言&#xff1a;近来项目结题需要撰写结题报告&#xff0c;在Word中需要添加图表目录&#xff08;即按住Ctrl并单击可直接跳转到图表相应位置进行访问&#xff09;&#xff0c;回想起来自己本科毕设论文当中并没有添加图目录&#xff0c;显得稍逊一番&#xff0c;今进行简单教…

Word图表的中英题注及引用

大部分中文期刊都要求对使用到的图片和表格插入中英对照题注。以下是在word文档中的具体操作方法。 1、选中表格 2、“引用”–>“插入题注” 3、“新建标签”–>输入标签名称 4、“确定” 5、此时生成了英文题注 6、还有一个中文的题注&#xff0c;重复步骤2-4&…

Word 2010创建图表的详细操作流程

Word 2010中内置多种图表模板供用户选择使用&#xff0c;不仅如此&#xff0c;用户还可以根据自身需求创建自定义的图表模板&#xff0c;从而提高工作效率。 Word 2010创建图表的详细操作流程如下&#xff1a; 第1步&#xff0c;打开Word2010文档窗口&#xff0c;切换到“插入…

MongoDB数据查询

1、查询某个集合的所有&#xff1a; db.集合名称.find(); 或者 db.集合名称.find({}); MongoDB的集合名称相当于SQL的表名 如同SQL语句&#xff1a; SELECT * FROM TABLENAME 2、根据条件查询&#xff1a; db.集合名称.find({"column":"value1"})…

MongoDB的查询语法

1.查询 2.排序查询 sort() 方法对数据进行排序&#xff0c;sort() 方法可以通过参数指定排序的字段&#xff0c;并使用 1 和 -1 来指定排序的方式&#xff0c;其中 1 为升序排列&#xff0c;而 -1 是用于降序排列 3.分页列表查询 skip(), limilt(), sort()三个放在一起执行的时…

MongoDB中的数据查询

MongoDB中的查询大致上分为以下五种&#xff1a; 一、操作符查询&#xff1a; MongoDB中的操作符查询&#xff0c;其与RDBMS Where中的语句比较是这样的&#xff1a; 例如&#xff1a; db.demo.find({"score" : {$gt : 85}}) 其意思是查询数据库中成绩大于85分…

mongoDB聚合查询

管道 管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数。MongoDB的聚合管道将MongoDB文档在一个管道处理完毕后将结果传递给下一个管道处理。管道操作是可以重复的。 聚合管道操作 可参考菜鸟文档&#xff1a;菜鸟文档 命令 功能描述 $project指定输出…

MongoDB的文档查询

MongoDB查询文档使用find()方法&#xff0c;find()方法以非结构化的方式来显示所有文档。 一、语法 db.集合名称.find(query,projection) quey&#xff1a;可选参数&#xff0c;使用查询操作符指定查询条件projection&#xff1a;可选参数&#xff0c;使用投影操作符指定返回…

【2023 · CANN训练营第一季】应用开发(初级)第五章——媒体数据处理

1.媒体数据处理 受网络结构和训练方式等因素的影响&#xff0c;绝大多数神经网络模型对输入数据都有格式上的限制。在计算机视觉领域&#xff0c;这个限制大多体现在图像的尺寸、色域、归一化参数等。如果源图或视频的尺寸、格式等与网络模型的要求不一致时&#xff0c;我们需…

【mongodb】查询mongodb的数据库存放路径

通过yum方式安装mongodb,我们需要了解mongodb实际存放数据的位置以及相关日志所在的文件&#xff0c;我们可以通过如下命令来了解: ps -ax | grep mongod 通过上面的输出&#xff0c;我们了解到Mongodb的配置使用的是/etc/mongod.conf文件&#xff0c;继而&#xff0c;我们可以…

MongoDB aggregate 管道聚合查询

文章目录 数据准备条件查询聚合查询模糊查询拆分数据关联查询根据用户组查询用户根据用户查询用户组 控制显示字段置顶合并字段新增或修改字段计算总数分页查询分页查询加总数排序分组 数据准备 UserPO Data EqualsAndHashCode(callSuper true) Accessors(chain true) NoAr…

mongodb查询

MongoDB 查询文档使用 find() 方法。 find() 方法以非结构化的方式来显示所有文档。 db.集合.find(query, projection) query &#xff1a;可选&#xff0c;使用查询操作符指定查询条件 projection &#xff1a;可选&#xff0c;使用投影操作符指定返回的键。查询时返回文档中所…

vue-element-admin实践系列(二)初始化系统的页面元素

vue-element-admin实践系列 vue-element-admin实践系列(一)代码部署及运行demovue-element-admin实践系列(二)初始化系统的页面元素 文章目录 vue-element-admin实践系列1、修改默认参数1.1 修改启动端口1.2 修改网页title1.3 修改网站 ico1.4 效果如下 2、自定义左侧导航栏2.…

一元多项式的加减法(C语言)

要求&#xff1a;以文件形式输入需要进行运算的两个一元多项式&#xff0c;并将结果以文件的形式进行输出。 #include<stdio.h> #include<stdlib.h> #include<math.h>//链表的构成 typedef struct Node{float ratio;int index;struct Node* next; }*PNode,*…

一元多项式计算器(C语言实现)

一、实验要求 二、代码实现 以下C语言代码实现了多项式的加减法、乘法、求导和顶点求值的功能。 &#xff08;水平有限&#xff0c;代码存在不规范之处&#xff09; 编程环境&#xff1a;Window11 Visual Studio #define _CRT_SECURE_NO_WARNINGS 1 # include <stdio.h…

c语言一元多项式课程设计,一元多项式的计算数据结构课程设计.doc

一元多项式的计算数据结构课程设计.doc 一元多项式的计算加&#xff0c;减摘要题目一元多项式计算任务能够按照指数降序排列建立并输出多项式&#xff1b;能够完成两个多项式的相加、相减&#xff0c;并将结果输入&#xff1b;目录1引言2需求分析3概要设计4详细设计5测试结果6…

基于C++的一元多项式相加

资源下载地址&#xff1a;https://download.csdn.net/download/sheziqiong/85996014 资源下载地址&#xff1a;https://download.csdn.net/download/sheziqiong/85996014 1.实验目的 了解链式存储结构的基本知识&#xff1b;掌握算法思想和数据结构的描述&#xff1b;结合一元…