Java利用Apache poi导出图表
Posted jack-0824
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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导出图表的主要内容,如果未能解决你的问题,请参考以下文章