结果导出

This commit is contained in:
wanxiaoli 2026-01-21 09:51:18 +08:00
parent 5093d978b4
commit 67739f8a7c

View File

@ -4,10 +4,18 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfd.business.css.domain.ScenarioResult;
import com.yfd.business.css.service.ScenarioResultService;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
@RestController
@RequestMapping("/scenario-results")
@ -54,4 +62,129 @@ public class ScenarioResultController {
"data", data
));
}
/**
* 导出指定条件下的情景结果为Excel文件
*
* @param response HTTP响应对象
* @param scenarioId 场景ID
* @param deviceId 设备ID可选
* @param stepFrom 步骤起始范围可选
* @param stepTo 步骤结束范围可选
* @throws IOException 写入流异常
*/
@GetMapping("/export/by-scenario")
public void exportResultsAsExcel(HttpServletResponse response,
@RequestParam String scenarioId,
@RequestParam(required = false) String deviceId,
@RequestParam(required = false) Integer stepFrom,
@RequestParam(required = false) Integer stepTo) throws IOException {
// 设置响应头信息以便浏览器识别并触发下载行为
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment;filename=\"scenario_results.xlsx\"");
try {
// 查询全部符合过滤条件的数据不分页
QueryWrapper<ScenarioResult> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("scenario_id", scenarioId);
if (deviceId != null && !deviceId.isEmpty()) {
queryWrapper.eq("device_id", deviceId);
}
if (stepFrom != null) {
queryWrapper.ge("step", stepFrom);
}
if (stepTo != null) {
queryWrapper.le("step", stepTo);
}
List<ScenarioResult> results = scenarioResultService.list(queryWrapper.orderByAsc("step"));
// 创建Excel工作簿
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("情景结果数据");
// 创建表头样式
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 12);
headerStyle.setFont(headerFont);
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setAlignment(HorizontalAlignment.CENTER);
// 创建数据样式
CellStyle dataStyle = workbook.createCellStyle();
dataStyle.setBorderBottom(BorderStyle.THIN);
dataStyle.setBorderTop(BorderStyle.THIN);
dataStyle.setBorderRight(BorderStyle.THIN);
dataStyle.setBorderLeft(BorderStyle.THIN);
dataStyle.setWrapText(true);
// 创建表头
Row headerRow = sheet.createRow(0);
String[] headers = {"场景ID", "设备ID", "时点", "属性值", "Keff值"};
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(headerStyle);
}
// 填充数据
int rowNum = 1;
for (ScenarioResult result : results) {
Row row = sheet.createRow(rowNum++);
Cell cell0 = row.createCell(0);
cell0.setCellValue(result.getScenarioId());
cell0.setCellStyle(dataStyle);
Cell cell1 = row.createCell(1);
cell1.setCellValue(result.getDeviceId());
cell1.setCellStyle(dataStyle);
Cell cell2 = row.createCell(2);
cell2.setCellValue(result.getStep());
cell2.setCellStyle(dataStyle);
Cell cell3 = row.createCell(3);
cell3.setCellValue(result.getAttrState().toString());
cell3.setCellStyle(dataStyle);
Cell cell4 = row.createCell(4);
cell4.setCellValue(result.getKeffValue().doubleValue());
cell4.setCellStyle(dataStyle);
}
// 自动调整列宽
for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
// 设置最小列宽
if (sheet.getColumnWidth(i) < 20 * 256) {
sheet.setColumnWidth(i, 20 * 256);
}
// 设置最大列宽
if (sheet.getColumnWidth(i) > 50 * 256) {
sheet.setColumnWidth(i, 50 * 256);
}
}
// 写入响应输出流
workbook.write(response.getOutputStream());
workbook.close();
} catch (Exception e) {
throw new RuntimeException("导出失败:" + e.getMessage(), e);
}
}
}