今日寒窗苦读,必定有我;
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>
一,导出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<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;
}
}