|
|
@@ -8,6 +8,7 @@ import io.swagger.annotations.*;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.codec.Charsets;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
@@ -36,12 +37,10 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springblade.core.mp.support.Condition;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
@@ -174,58 +173,73 @@ public class WeatherController {
|
|
|
@RequestParam String endTime,
|
|
|
HttpServletResponse response) {
|
|
|
|
|
|
- // 1. 查询区域信息
|
|
|
+ // 1. 参数校验
|
|
|
+ if (contractId == null || contractId.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("合同ID不能为空");
|
|
|
+ }
|
|
|
+ if (startTime == null || startTime.isEmpty() || endTime == null || endTime.isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("开始/结束时间不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 查询业务数据
|
|
|
ProjectContractArea area = this.projectContractAreaClient.queryContractAreaByContractId(contractId);
|
|
|
- // 2. 查询天气数据
|
|
|
+ if (area == null || area.getId() == null) {
|
|
|
+ throw new RuntimeException("未查询到合同对应的区域信息");
|
|
|
+ }
|
|
|
List<WeatherInfo> list = this.weatherInfoService.getWeatherInfoListByRecordTime(area.getId(), startTime, endTime);
|
|
|
|
|
|
- // 3. 模板文件路径
|
|
|
- String templatePath = "/mnt/sdc/Users/hongchuangyanfa/Desktop/excel/weather.xlsx";
|
|
|
- // String templatePath = "C:\\upload\\excel\\weather.xlsx"; // 本地测试路径
|
|
|
+ // 3. 模板文件校验
|
|
|
+ String templatePath = "C:\\upload\\excel\\weather.xlsx";
|
|
|
+ File templateFile = new File(templatePath);
|
|
|
+ if (!templateFile.exists() || !templateFile.isFile()) {
|
|
|
+ throw new RuntimeException("Excel模板文件不存在:" + templatePath);
|
|
|
+ }
|
|
|
|
|
|
- // 4. 读取模板文件(使用try-with-resources自动关闭流)
|
|
|
- try (InputStream templateStream = new FileInputStream(new File(templatePath));
|
|
|
+ // 4. 读取并填充Excel模板
|
|
|
+ try (InputStream templateStream = new FileInputStream(templateFile);
|
|
|
Workbook workbook = WorkbookFactory.create(templateStream);
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
|
|
|
|
|
- // 5. 获取第一个工作表
|
|
|
+ // 获取/创建工作表
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
if (sheet == null) {
|
|
|
- sheet = workbook.createSheet("天气台账"); // 防止模板sheet不存在
|
|
|
+ sheet = workbook.createSheet("WeatherRecord");
|
|
|
}
|
|
|
|
|
|
- // 6. 填充数据(从第2行开始,行索引从0开始)
|
|
|
+ // 填充数据(防Null)
|
|
|
int startRow = 1;
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
Row row = sheet.createRow(startRow + i);
|
|
|
WeatherInfo info = list.get(i);
|
|
|
+ String dateStr = info.getRecordTime() != null ? DateUtil.format(info.getRecordTime(), "yyyy-MM-dd") : "";
|
|
|
|
|
|
- // 格式化日期
|
|
|
- String dateStr = DateUtil.format(info.getRecordTime(), "yyyy-MM-dd");
|
|
|
-
|
|
|
- // 填充各列数据
|
|
|
- row.createCell(0).setCellValue(dateStr); // 日期
|
|
|
- row.createCell(1).setCellValue(info.getWeather()); // 天气
|
|
|
- row.createCell(2).setCellValue(info.getTempLow() + "~" + info.getTempHigh()); // 温度
|
|
|
- row.createCell(3).setCellValue(info.getAirTemp()); // 平均温度
|
|
|
- row.createCell(4).setCellValue(info.getWindLevel()); // 风力
|
|
|
+ row.createCell(0).setCellValue(dateStr);
|
|
|
+ row.createCell(1).setCellValue(info.getWeather() == null ? "" : info.getWeather());
|
|
|
+ row.createCell(2).setCellValue((info.getTempLow() == null ? "" : info.getTempLow()) + "~" + (info.getTempHigh() == null ? "" : info.getTempHigh()));
|
|
|
+ row.createCell(3).setCellValue(info.getAirTemp() == null ? "" : info.getAirTemp());
|
|
|
+ row.createCell(4).setCellValue(info.getWindLevel() == null ? "" : info.getWindLevel());
|
|
|
}
|
|
|
|
|
|
- // 7. 写入字节流
|
|
|
+ // 写入字节流
|
|
|
workbook.write(outputStream);
|
|
|
- // 8. 设置响应头(核心)
|
|
|
- String fileName = "天气台账.xlsx";
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
- String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);
|
|
|
- // 9. 写入响应流
|
|
|
- response.setContentLength(outputStream.size());
|
|
|
- outputStream.writeTo(response.getOutputStream());
|
|
|
- response.flushBuffer();
|
|
|
+
|
|
|
+ // ========== 核心:完全照搬你成功的代码逻辑 ==========
|
|
|
+ // 1. 只编码中文核心名称(和你"工程划分导入模版"的编码方式一致)
|
|
|
+ String fileName = URLEncoder.encode("天气台账", StandardCharsets.UTF_8.name());
|
|
|
+ // 2. 删掉response.reset()(你成功的代码里没有,避免清空关键头)
|
|
|
+ // 3. Content-Type和你成功的代码完全一致(用vnd.ms-excel,不要用xlsx专属的)
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding(StandardCharsets.UTF_8.name());
|
|
|
+ // 4. 响应头用小写Content-disposition + 只保留filename参数 + 拼接.xlsx后缀(和你完全一致)
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
|
+
|
|
|
+ // 5. 用BufferedOutputStream包装(和你成功的代码一致,提升流稳定性)
|
|
|
+ OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
|
|
|
+ toClient.write(outputStream.toByteArray());
|
|
|
+ toClient.flush();
|
|
|
+ toClient.close();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* 查询单个数据
|
|
|
*
|