栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java导出echars图行(可一次导出多个柱状图)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java导出echars图行(可一次导出多个柱状图)

Apache POI依赖代码如下所示:


    
        org.apache.poi
        poi-ooxml
        4.1.2
    
    
        org.apache.poi
        poi-ooxml-schemas
        4.1.2
    
    
        org.apache.poi
        poi
        4.1.2
    
    
        org.apache.poi
        poi-scratchpad
        4.1.2
    
    
        org.apache.xmlbeans
        xmlbeans
        3.1.0
    
    
        org.apache.poi
        ooxml-schemas
        1.4
    

一,导出word报告Controller

  @ApiOperation(value = "导出word报告", notes = "导出word报告")
   @GetMapping("/exportWord")
   public void exportWord(HttpServletResponse response) throws Exception {
      XWPFDocument word = commonService.exportWord();
     	try {
			 response.setHeader("Content-disposition",
			"attachment;filename=" + new String("analysis_report.doc".getBytes(), "utf-8"));
			  response.setContentType("application/x-msdownload");
               OutputStream os = response.getOutputStream();
                 word.write(os);
                 os.close();
        } catch (IOException e) {
            logger.info("文件下载出错!");
        }
    }

二,service

//导出word报告
XWPFDocument  exportWord() throws Exception;

三,servicelmpl

package com.iecas.satresource.service.impl;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.FileOutputStream;
@Override
public XWPFDocument exportWord() {
    // 1、创建word文档对象
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph para;
    XWPFRun run;
     //2.// x轴标题
     String name = " ";
      // 3、X轴(分类轴)数据
   String[] xAxisData = new String[] {
            "2022-01","2022-02","2022-03","2022-04","2022-05","2022-06",
            "2022-07","2022-08","2022-09","2022-10","2022-11","2022-12",
    };
        yAxis.setTitle("流量(个)"); // Y轴标题
    yAxis.setCrossBetween(AxisCrossBetween.BETWEEN); // 设置图柱的位置:BETWEEN居中
     // 4、y轴数据
    Integer[] yAxisData = new Integer[]{
            20, 37, 21, 75, 79, 118,
            29, 112, 51, 85, 101, 89
    };
 //如一次性导出多个ECharts柱形图可创建多个方法进行调用创建,如果数据类型不一样可在创建工具类创建多个方法进行调用
    wordUtil.WordReadZc(doc, xAxisDate, yAxisDat4, name);
    return doc;
    }

三:工具类

package com.iecas.satresource.utils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.IOException;


public class WordUtil {
 XWPFChart chart = null;
  public XWPFChart WordRead(XWPFDocument doc, String[] xAxisDate, Integer[] yAxisDate, String name) {
      try {
        this.chart = doc.createChart(5400000, 3600000);
    } catch (InvalidFormatException var15) {
        var15.printStackTrace();
    } catch (IOException var16) {
        var16.printStackTrace();
    }

    this.chart.setTitleOverlay(false);
    XDDFChartLegend legend = this.chart.getOrAddLegend();
    legend.setPosition(LegendPosition.TOP);
    XDDFCategoryAxis xAxis = this.chart.createCategoryAxis(AxisPosition.BOTTOM);
    //标题名称
    xAxis.setTitle(name);
    //x数据
    XDDFCategoryDataSource xAxisSource = XDDFDataSourcesFactory.fromArray(xAxisDate);
    XDDFValueAxis yAxis = this.chart.createValueAxis(AxisPosition.LEFT);
    yAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
    XDDFBarChartData barChart;
    Series barSeries;
    //y数据
      XDDFNumericalDataSource yAxisSource1 = XDDFDataSourcesFactory.fromArray(yAxisDate);
        barChart = (XDDFBarChartData)this.chart.createData(ChartTypes.BAR, xAxis, yAxis);
        barChart.setBarDirection(BarDirection.COL);
        barSeries = (Series)barChart.addSeries(xAxisSource, yAxisSource1);
        barSeries.setTitle("", (CellReference)null);
        this.chart.plot(barChart);
            return this.chart;
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号