|
|
@@ -349,12 +349,9 @@ public class SalesForecastSummaryController {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
@GetMapping("/exportTemplate")
|
|
|
@ApiOperation(value="导出提交数据模板")
|
|
|
public void exportTemplate(HttpServletResponse response) throws IOException {
|
|
|
-
|
|
|
// 1. 创建 Excel 工作簿
|
|
|
Workbook workbook = new XSSFWorkbook();
|
|
|
// 2. 创建工作表
|
|
|
@@ -365,10 +362,12 @@ public class SalesForecastSummaryController {
|
|
|
// 定义表头字段(按你要求的顺序)
|
|
|
String[] headers = {"物料号", "物料名称", "品牌名", "规格", "花纹", "预测数量"};
|
|
|
|
|
|
- // 4. 设置表头样式(可选,优化视觉效果)
|
|
|
+ // 4. 设置表头样式(优化视觉效果)
|
|
|
CellStyle headerStyle = workbook.createCellStyle();
|
|
|
Font font = workbook.createFont();
|
|
|
font.setBold(true); // 加粗
|
|
|
+ font.setFontName("微软雅黑");
|
|
|
+ font.setFontHeightInPoints((short) 12);
|
|
|
headerStyle.setFont(font);
|
|
|
headerStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
|
|
|
|
|
|
@@ -377,46 +376,69 @@ public class SalesForecastSummaryController {
|
|
|
Cell cell = headerRow.createCell(i);
|
|
|
cell.setCellValue(headers[i]);
|
|
|
cell.setCellStyle(headerStyle);
|
|
|
- // 自动调整列宽(适配内容)
|
|
|
+ // 自动调整列宽并加宽(适配中文)
|
|
|
sheet.autoSizeColumn(i);
|
|
|
+ sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 2048);
|
|
|
}
|
|
|
|
|
|
- // ========== 完善数据查询和填充逻辑 ==========
|
|
|
- Map<String, Object> res = getUserLinkGoods();
|
|
|
- // 修复1:Gson转换(兼容原有写法,避免类型强转报错)
|
|
|
+ // ========== 核心:查询数据并填充 ==========
|
|
|
List<PjpfStockDesc> pjpfStockDescList = new ArrayList<>();
|
|
|
- if (res.get("pjpfStockDescList") != null && res.get("pjpfStockDescList") instanceof List) {
|
|
|
- // 直接强转,保留你原有逻辑,避免Gson转换复杂问题
|
|
|
- pjpfStockDescList = (List<PjpfStockDesc>) res.get("pjpfStockDescList");
|
|
|
+ try {
|
|
|
+ Map<String, Object> res = getUserLinkGoods();
|
|
|
+ // 安全转换数据列表
|
|
|
+ if (res.get("pjpfStockDescList") != null && res.get("pjpfStockDescList") instanceof List) {
|
|
|
+ pjpfStockDescList = (List<PjpfStockDesc>) res.get("pjpfStockDescList");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("查询物料数据异常:" + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- // 填充数据行(匹配所有指定字段)
|
|
|
+ // 6. 定义数据行样式
|
|
|
+ CellStyle dataStyle = workbook.createCellStyle();
|
|
|
+ dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+
|
|
|
+ // 7. 填充数据行
|
|
|
int rowNum = 1;
|
|
|
- for (PjpfStockDesc item : pjpfStockDescList) {
|
|
|
- Row row = sheet.createRow(rowNum++);
|
|
|
- // 1. id(处理null,避免空指针)
|
|
|
- // 2. 物料号(对应实体code字段,修复原错误的materialCode)
|
|
|
- row.createCell(0).setCellValue(item.getCode() != null ? item.getCode() : "");
|
|
|
- // 3. 物料名称(对应cname字段)
|
|
|
- row.createCell(1).setCellValue(item.getCname() != null ? item.getCname() : "");
|
|
|
- // 4. 品牌名(对应brandName字段,无值则为空)
|
|
|
- row.createCell(2).setCellValue(item.getBrandName() != null ? item.getBrandName() : "");
|
|
|
- // 5. 规格(对应typeNo字段)
|
|
|
- row.createCell(3).setCellValue(item.getTypeNo() != null ? item.getTypeNo() : "");
|
|
|
- // 6. 花纹(对应brandItem字段)
|
|
|
- row.createCell(4).setCellValue(item.getBrandItem() != null ? item.getBrandItem() : "");
|
|
|
- // 7. 预测数量(模板留空,供用户填写)
|
|
|
- row.createCell(5).setCellValue("");
|
|
|
+ if (pjpfStockDescList != null && !pjpfStockDescList.isEmpty()) {
|
|
|
+ for (PjpfStockDesc item : pjpfStockDescList) {
|
|
|
+ Row row = sheet.createRow(rowNum++);
|
|
|
+ // 1. 物料号
|
|
|
+ row.createCell(0).setCellValue(item.getCode() != null ? item.getCode() : "");
|
|
|
+ // 2. 物料名称
|
|
|
+ row.createCell(1).setCellValue(item.getCname() != null ? item.getCname() : "");
|
|
|
+ // 3. 品牌名(核心:映射PubDescSeg4_Name)
|
|
|
+ row.createCell(2).setCellValue(item.getBrandName() != null ? item.getBrandName() : "");
|
|
|
+ // 4. 规格
|
|
|
+ row.createCell(3).setCellValue(item.getTypeNo() != null ? item.getTypeNo() : "");
|
|
|
+ // 5. 花纹(核心:映射pattern)
|
|
|
+ row.createCell(4).setCellValue(item.getBrandItem() != null ? item.getBrandItem() : "");
|
|
|
+ // 6. 预测数量(默认填0,供用户修改)
|
|
|
+ row.createCell(5).setCellValue(0);
|
|
|
+
|
|
|
+ // 统一设置数据行样式
|
|
|
+ for (int i = 0; i < 6; i++) {
|
|
|
+ row.getCell(i).setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 无数据时添加提示行
|
|
|
+ Row emptyRow = sheet.createRow(1);
|
|
|
+ Cell emptyCell = emptyRow.createCell(0);
|
|
|
+ emptyCell.setCellValue("暂无可用的物料数据");
|
|
|
+ emptyCell.setCellStyle(dataStyle);
|
|
|
+ // 预测数量列默认填0
|
|
|
+ emptyRow.createCell(5).setCellValue(0);
|
|
|
+ emptyRow.getCell(5).setCellStyle(dataStyle);
|
|
|
}
|
|
|
|
|
|
- // 6. 设置响应头,触发文件下载
|
|
|
+ // 8. 设置响应头,触发文件下载(解决中文乱码)
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
- // 文件名编码,避免中文乱码
|
|
|
String fileName = URLEncoder.encode("物料预测模板.xlsx", StandardCharsets.UTF_8.toString());
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
|
|
|
|
|
- // 7. 写出 Excel 文件到响应流
|
|
|
+ // 9. 写出 Excel 文件到响应流
|
|
|
try (OutputStream outputStream = response.getOutputStream()) {
|
|
|
workbook.write(outputStream);
|
|
|
outputStream.flush();
|
|
|
@@ -519,7 +541,6 @@ public class SalesForecastSummaryController {
|
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
|
resultMap.put("pjpfStockDescList", stockList);
|
|
|
resultMap.put("pjpfBrandDescList", brandList);
|
|
|
-
|
|
|
return resultMap;
|
|
|
}
|
|
|
|