1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > POI Word 图表 柱状图 条形图 折线图 饼图

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

时间:2023-08-14 08:27:55

相关推荐

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

poi Excel 图表:/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>mons</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>mons</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);}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。