模板调整
This commit is contained in:
@@ -12,6 +12,8 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hongawen
|
* @author hongawen
|
||||||
@@ -36,10 +38,10 @@ public class ReportController extends BaseController {
|
|||||||
@PostMapping("/generateReport")
|
@PostMapping("/generateReport")
|
||||||
@ApiOperation("生成测试报告")
|
@ApiOperation("生成测试报告")
|
||||||
@ApiImplicitParam(name = "param", value = "查询参数", required = true)
|
@ApiImplicitParam(name = "param", value = "查询参数", required = true)
|
||||||
public void generateReport(@RequestBody DevReportParam devReportParam) {
|
public void generateReport(@RequestBody DevReportParam devReportParam, HttpServletResponse response) {
|
||||||
String methodDescribe = getMethodDescribe("list");
|
String methodDescribe = getMethodDescribe("list");
|
||||||
LogUtil.njcnDebug(log, "{},终端参数为:{}", methodDescribe, devReportParam);
|
LogUtil.njcnDebug(log, "{},终端参数为:{}", methodDescribe, devReportParam);
|
||||||
reportService.generateReport(devReportParam);
|
reportService.generateReport(devReportParam,response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package com.njcn.gather.report.service;
|
|||||||
|
|
||||||
import com.njcn.gather.report.pojo.DevReportParam;
|
import com.njcn.gather.report.pojo.DevReportParam;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hongawen
|
* @author hongawen
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
* @data 2025/1/9 20:59
|
* @data 2025/1/9 20:59
|
||||||
*/
|
*/
|
||||||
public interface IReportService {
|
public interface IReportService {
|
||||||
void generateReport(DevReportParam devReportParam);
|
void generateReport(DevReportParam devReportParam, HttpServletResponse response);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ import org.springframework.core.io.ClassPathResource;
|
|||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
@@ -66,7 +67,7 @@ public class ReportServiceImpl implements IReportService {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateReport(DevReportParam devReportParam) {
|
public void generateReport(DevReportParam devReportParam, HttpServletResponse response) {
|
||||||
// 读取模板文件
|
// 读取模板文件
|
||||||
Resource baseModel = new ClassPathResource("model/BaseModel.docx");
|
Resource baseModel = new ClassPathResource("model/BaseModel.docx");
|
||||||
try (FileInputStream baseModelFis = new FileInputStream(baseModel.getFile())) {
|
try (FileInputStream baseModelFis = new FileInputStream(baseModel.getFile())) {
|
||||||
@@ -77,15 +78,23 @@ public class ReportServiceImpl implements IReportService {
|
|||||||
dealBaseModel(baseModelDocument, devReportParam);
|
dealBaseModel(baseModelDocument, devReportParam);
|
||||||
// 处理数据页中的信息
|
// 处理数据页中的信息
|
||||||
dealDataModel(baseModelDocument, devReportParam);
|
dealDataModel(baseModelDocument, devReportParam);
|
||||||
//最终文件输出的路径
|
// //最终文件输出的路径
|
||||||
FileOutputStream out = new FileOutputStream("C:\\Users\\hongawen\\Desktop\\testModel\\BaseDataModel" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx");
|
// FileOutputStream out = new FileOutputStream("C:\\Users\\hongawen\\Desktop\\testModel\\BaseDataModel" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx");
|
||||||
// 4. 保存新的Word文档
|
// // 4. 保存新的Word文档
|
||||||
try {
|
// try {
|
||||||
baseModelDocument.write(out);
|
// baseModelDocument.write(out);
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
throw new BusinessException("生成报告文件失败");
|
// throw new BusinessException("生成报告文件失败");
|
||||||
}
|
// }
|
||||||
out.close();
|
// out.close();
|
||||||
|
|
||||||
|
// 设置响应头
|
||||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||||
|
String fileName = "调试报告" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx";
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||||
|
// 保存新的Word文档到响应输出流
|
||||||
|
baseModelDocument.write(response.getOutputStream());
|
||||||
|
response.getOutputStream().flush();
|
||||||
System.out.println("报告生成成功!");
|
System.out.println("报告生成成功!");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user