java导出Echarts图形(柱状图、饼形图、折线图)
今日寒窗苦读,必定有我;
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;/*** 导出ECharts柱形图*/
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;}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
