java導出Echarts圖表的示例代碼(柱狀圖/餅形圖/折線圖)
更新時間:2025年06月11日 10:59:30 作者:cccl.
這篇文章主要為大家詳細介紹了java導出Echarts圖表的示例代碼,包括柱狀圖,餅形圖,折線圖等,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下
1、Apache POI依賴
代碼如下所示:
<dependencies>
<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>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、導出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("文件下載出錯!");
}
}
3、service
//導出word報告 XWPFDocument exportWord() throws Exception;
4、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、創(chuàng)建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柱形圖可創(chuàng)建多個方法進行調用創(chuàng)建,如果數據類型不一樣可在創(chuàng)建工具類創(chuàng)建多個方法進行調用
wordUtil.WordReadZc(doc, xAxisDate, yAxisDat4, name);
return doc;
}
5、工具類
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<Integer> 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;
}
}
到此這篇關于java導出Echarts圖表的示例代碼(柱狀圖/餅形圖/折線圖)的文章就介紹到這了,更多相關java導出Echarts圖表內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
@ComponentScan在spring中無效的原因分析及解決方案
這篇文章主要介紹了@ComponentScan在spring中無效的原因分析及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
java自定義日志輸出文件(log4j日志文件輸出多個自定義日志文件)
打印日志的在程序中是必不可少的,如果需要將不同的日志打印到不同的地方,則需要定義不同的Appender,然后定義每一個Appender的日志級別、打印形式和日志的輸出路徑,下面看一個示例吧2014-01-01

