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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM