Java导出通过Word模板导出docx文件并通过QQ邮箱发送
package com.gitee.pifeng.monitoring.server.business.server.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gitee.pifeng.monitoring.server.business.server.dao.*;
import com.gitee.pifeng.monitoring.server.business.server.entity.*;
import com.gitee.pifeng.monitoring.server.business.server.service.IAlarmService;
import com.gitee.pifeng.monitoring.server.config.WordTemplateConfig;
import com.gitee.pifeng.monitoring.server.util.db.SelectEmailsService;
import org.apache.poi.util.StringUtil;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* description 导出word版本的报表
*
* @author yhj
* @date 2025-01-06 17:08:15
*/
@RequestMapping("/ExportWord")
@RestController
public class ExportWordReportController {
@Autowired
private WordTemplateConfig wordTemplateConfig;
/**
* 告警服务接口
*/
@Autowired
private IAlarmService alarmService;
@Autowired
private IMonitorInstanceDao monitorInstanceDao;
@Autowired
private IMonitorAlarmRecordDao monitorAlarmRecordDao;
@Autowired
private IMonitorDbDao monitorDbDao;
@Autowired
private IMonitorTcpDao monitorTcpDao;
@Autowired
private IMonitorHttpDao monitorHttpDao;
@Autowired
private SelectEmailsService emailsService;
public void exportWordTemplate(HttpServletResponse response, Map<String, String> data, String filename) {
String companyName = emailsService.selectCompanyName();
data.put("company", companyName);
try {
String filePath = URLDecoder.decode(getClass().getClassLoader().getResource("templates/static/WordReportTemplete.docx").getPath(), "UTF-8");
// 读取Word模板
FileInputStream fis = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(fis);
// 将填充后的文档写入响应输出流
wordTemplateConfig.replaceTextInDocument(document, data);
// 设置响应头,指定下载文件类型和名称
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + filename + ".docx");
document.write(response.getOutputStream());
// 关闭资源
document.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void exportEmptyWordTemplate(HttpServletResponse response, Map<String, String> data) {
String companyName = emailsService.selectCompanyName();
String filename = URLEncoder.encode(String.valueOf(new StringBuilder(companyName).append("现场暂无数据"))).replaceAll("\\+", "%20");
data.put("company", companyName);
try {
String filePath = URLDecoder.decode(getClass().getClassLoader().getResource("templates/static/EmptyWordReportTemplete.docx").getPath(), "UTF-8");
// 读取Word模板
FileInputStream fis = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(fis);
// 将填充后的文档写入响应输出流
wordTemplateConfig.replaceTextInDocument(document, data);
// 设置响应头,指定下载文件类型和名称
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + filename + ".docx");
document.write(response.getOutputStream());
// 关闭资源
document.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 本日服务器报错信息word导出
*
* @param response
*/
@GetMapping("/getThisDayWordReport")
public void getDayWordReport(HttpServletResponse response) {
String companyName = emailsService.selectCompanyName();
Map<String, String> data = new HashMap<>();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String TodayDay = simpleDateFormat.format(date);
data.put("Date", TodayDay);
Integer flag = 0; // 0代表今日
//文件名,以下方法用于防止中文编码错误
String name = String.valueOf(new StringBuilder(companyName).append(TodayDay).append("服务器报错信息"));
String filename = URLEncoder.encode(name).replaceAll("\\+", "%20");
thisWordInfo(response, flag, TodayDay, data, filename);
}
// 将导出的信息填充给word
private void thisWordInfo(HttpServletResponse response, Integer flag, String Date, Map<String, String> data, String filename) {
// 查询所有IP
List<String> IpList = getIpList();
if (IpList.size() == 0) {
exportEmptyWordTemplate(response, data);
} else {
List<ExportWordReport> listA = new ArrayList<>();
if (!IpList.get(0).isEmpty()) {
String IPA = IpList.get(0);
if (flag == 0) {
// 当日报错个数
listA = alarmService.selectWordDayAlarmTotal(IPA, Date);
} else if (flag == 1) {
// 本周报错个数
listA = alarmService.selectWordWeekAlarmTotal(IPA, Date);
} else if (flag == 2) {
// 本月报错个数
listA = alarmService.selectWordMonthAlarmTotal(IPA, Date);
} else if (flag == 3) {
// 本年报错个数
listA = alarmService.selectWordYearAlarmTotal(IPA, Date);
}
// 汇总各项报错个数
Map<String, Integer> mapA = listA.stream().collect(Collectors.toMap(ExportWordReport::getName, ExportWordReport::getTotal));
// 查询该IP下所有应用程序的名称
String applicationName = getApplicationNameList(IPA);
// 查询该IP下所有数据库名字
String DBName = getDBNameList(IPA);
// 查询该IP下所有中间件名字
String MiddleWareName = getMiddleNameList(IPA);
// 查询该IP下所有接口名称
String InterfaceName = getInterfaceNameList(IPA);
// 填充word里面的字段
data.put("IPA", IPA);
data.put("ApplicationAName", !applicationName.isEmpty() ? applicationName : "0个");
data.put("DBAName", !DBName.isEmpty() ? DBName : "0个");
data.put("MiddleWareAName", !MiddleWareName.isEmpty() ? MiddleWareName : "0个");
data.put("InterfaceAName", !InterfaceName.isEmpty() ? InterfaceName : "0个");
// 服务器
data.put("ServerATotal", mapA.get("SERVER") != null ? String.valueOf(mapA.get("SERVER")) : "0");
// 应用
data.put("ApplicationATotal", mapA.get("INSTANCE") != null ? String.valueOf(mapA.get("INSTANCE")) : "0");
// 数据库
data.put("DBATotal", mapA.get("DATABASE") != null ? String.valueOf(mapA.get("DATABASE")) : "0");
// 中间件
data.put("MiddleWareATotal", mapA.get("TCP4SERVICE") != null ? String.valueOf(mapA.get("TCP4SERVICE")) : "0");
// 接口
data.put("InterfaceATotal", mapA.get("HTTP4SERVICE") != null ? String.valueOf(mapA.get("HTTP4SERVICE")) : "0");
}
if (!IpList.get(1).isEmpty()) {
List<ExportWordReport> listB = new ArrayList<>();
String IPB = IpList.get(1);
if (flag == 0) {
listB = alarmService.selectWordDayAlarmTotal(IPB, Date);
} else if (flag == 1) {
// 本周报错信息
listB = alarmService.selectWordWeekAlarmTotal(IPB, Date);
} else if (flag == 2) {
// 本月报错个数
listB = alarmService.selectWordMonthAlarmTotal(IPB, Date);
} else if (flag == 3) {
// 本年报错个数
listB = alarmService.selectWordYearAlarmTotal(IPB, Date);
}
// 汇总各项报错个数
Map<String, Integer> mapB = listB.stream().collect(Collectors.toMap(ExportWordReport::getName, ExportWordReport::getTotal));
// 查询该IP下所有应用程序的名称
String applicationName = getApplicationNameList(IPB);
// 查询该IP下所有数据库名字
String DBName = getDBNameList(IPB);
// 查询该IP下所有中间件名字
String MiddleWareName = getMiddleNameList(IPB);
// 查询该IP下所有接口名称
String InterfaceName = getInterfaceNameList(IPB);
data.put("IPB", IPB);
data.put("ApplicationBName", !applicationName.isEmpty() ? applicationName : "0个");
data.put("DBBName", !DBName.isEmpty() ? DBName : "0个");
data.put("MiddleWareBName", !MiddleWareName.isEmpty() ? MiddleWareName : "0个");
data.put("InterfaceBName", !InterfaceName.isEmpty() ? InterfaceName : "0个");
// 服务器
data.put("ServerBTotal", mapB.get("SERVER") != null ? String.valueOf(mapB.get("SERVER")) : "0");
// 应用
data.put("ApplicationBTotal", mapB.get("INSTANCE") != null ? String.valueOf(mapB.get("INSTANCE")) : "0");
// 数据库
data.put("DBBTotal", mapB.get("DATABASE") != null ? String.valueOf(mapB.get("DAT