首先添加必要的依赖(pom.xml)
<!-- ECharts生成图片 -->
<dependency>
<groupId>org.icepear.echarts</groupId>
<artifactId>echarts-java</artifactId>
<version>1.0.6</version>
</dependency>
<!-- POI处理Word -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- 图片处理 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.19</version>
</dependency>
创建 ECharts 图表生成工具类:
package com.example.demo.util;
import org.icepear.echarts.Chart;
import org.icepear.echarts.Line;
import org.icepear.echarts.charts.line.LineSeries;
import org.icepear.echarts.components.coord.cartesian.CategoryAxis;
import org.icepear.echarts.components.coord.cartesian.ValueAxis;
import org.icepear.echarts.render.Engine;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
public class EChartsUtil {
/**
* 生成示例折线图
*/
public static BufferedImage generateLineChart() throws IOException {
// 创建折线图
Line lineChart = new Line()
.setTitle("销售趋势图", "2023年度各月份销售数据")
.addXAxis(new CategoryAxis()
.setData("1月", "2月", "3月", "4月", "5月", "6月",
"7月", "8月", "9月", "10月", "11月", "12月"))
.addYAxis(new ValueAxis())
.addSeries(new LineSeries()
.setData(120, 132, 101, 134, 90, 230, 210,
230, 180, 230, 210, 200)
.setName("销售额(万元)"));
// 渲染图表
Engine engine = new Engine();
String json = engine.renderJsonOption(lineChart);
// 将图表转换为图片
return engine.renderToImage(json, 800, 500);
}
/**
* 将BufferedImage转换为字节数组
*/
public static byte[] imageToByteArray(BufferedImage image) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
return baos.toByteArray();
}
}
创建 Word 导出服务类,将图表插入到 Word 文档
package com.example.demo.service;
import com.example.demo.util.EChartsUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@Service
public class WordExportService {
/**
* 导出包含ECharts图表的Word文档
*/
public byte[] exportWordWithChart() throws IOException {
// 创建Word文档
XWPFDocument document = new XWPFDocument();
// 添加标题
XWPFParagraph titleParagraph = document.createParagraph();
XWPFRun titleRun = titleParagraph.createRun();
titleRun.setText("销售数据分析报告");
titleRun.setFontSize(16);
titleRun.setBold(true);
titleParagraph.setAlignment(ParagraphAlignment.CENTER);
// 添加段落
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("以下是2023年度销售数据趋势图:");
run.setFontSize(12);
// 生成ECharts图表
BufferedImage chartImage = EChartsUtil.generateLineChart();
// 将图表插入到Word中
XWPFParagraph imageParagraph = document.createParagraph();
XWPFRun imageRun = imageParagraph.createRun();
imageRun.addPicture(new ByteArrayInputStream(
EChartsUtil.imageToByteArray(chartImage)),
Document.PICTURE_TYPE_PNG,
"chart.png",
Units.toEMU(500), // 宽度
Units.toEMU(300)); // 高度
// 添加结论段落
XWPFParagraph conclusionParagraph = document.createParagraph();
XWPFRun conclusionRun = conclusionParagraph.createRun();
conclusionRun.setText("从图表可以看出,本年度销售整体呈现增长趋势,下半年表现尤为突出。");
conclusionRun.setFontSize(12);
// 将文档写入字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
document.write(out);
document.close();
return out.toByteArray();
}
}
创建控制器处理导出请求
package com.example.demo.controller;
import com.example.demo.service.WordExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
@RequestMapping("/export")
public class ExportController {
@Autowired
private WordExportService wordExportService;
@GetMapping("/wordWithChart")
public ResponseEntity<byte[]> exportWordWithChart() throws IOException {
byte[] wordData = wordExportService.exportWordWithChart();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment",
new String("销售数据分析报告.docx".getBytes("UTF-8"), "ISO-8859-1"));
return new ResponseEntity<>(wordData, headers, HttpStatus.OK);
}
}
工作原理:
使用 echarts-java 库在后端生成 ECharts 图表配置将图表渲染为 BufferedImage 对象使用 POI 库创建 Word 文档并插入图表图片通过控制器将生成的 Word 文档以附件形式返回给前端
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...