yangyb 1 тиждень тому
батько
коміт
cca9add119

+ 16 - 7
mall-service/src/main/java/com/txz/mall/service/impl/StoreOrderServiceImpl.java

@@ -42,7 +42,6 @@ import vo.*;
 import javax.servlet.http.HttpServletResponse;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.math.BigDecimal;
 import java.net.URLEncoder;
 import java.util.*;
@@ -243,23 +242,31 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
 
     @Override
     public void exportFile(StoreOrderDTO dto, HttpServletResponse response) {
-        OutputStream outputStream = null;
         try {
-            outputStream = response.getOutputStream();
             response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
             response.setCharacterEncoding("utf-8");
             String fileName = URLEncoder.encode("导出订单", "UTF-8").replaceAll("\\+", "%20");
             response.setHeader(
                     "Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
+
             List<StoreOrderVO> voList = orderList(dto);
             List<StoreProductPO> list = voList.stream().map(vo -> {
                 StoreProductPO po = new StoreProductPO();
                 BeanUtils.copyProperties(vo, po);
                 return po;
             }).collect(Collectors.toList());
+            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+            if (CollectionUtils.isEmpty(list)) {
+                list.add(new StoreProductPO());
+            }
             EasyExcel.write(outputStream, StoreProductPO.class).sheet("导出订单").doWrite(list);
+            // 将数据写入响应输出流
+            outputStream.writeTo(response.getOutputStream());
+            outputStream.close();
+            response.getOutputStream().flush();
         } catch (Exception e) {
-            throw new ServiceException("模板下载失败, 请联系管理员");
+            log.error("导出订单失败: ", e);
+            throw new ServiceException("模板下载失败, 请联系管理员: " + e.getMessage());
         }
     }
 
@@ -1340,8 +1347,10 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
                     "Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
             // 使用字节数组输出流作为中间缓冲
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-            ArrayList<OrderDeliveryPO> list = new ArrayList<>();
-            EasyExcel.write(outputStream, OrderDeliveryPO.class).sheet("导出发货模版").doWrite(list);
+            // 创建一个空列表,但确保Excel文件包含表头
+            EasyExcel.write(outputStream, OrderDeliveryPO.class)
+                    .sheet("导出发货模版")
+                    .doWrite(new ArrayList<OrderDeliveryPO>());
 
             // 将数据写入响应输出流
             outputStream.writeTo(response.getOutputStream());
@@ -1358,7 +1367,7 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
         String name = file.getOriginalFilename();
         String suffix = FileUtil.extName(name);
         log.info("获取到的文件名为----->{},后缀为----->{},入参------->{}", name, suffix);
-        if ("xlsx".equals(suffix) || "xls".equals(suffix)) {
+        if ("xlsx".equals(suffix) && !"xls".equals(suffix)) {
             throw new ServiceException("请传入xlsx或xls文档格式文件");
         }
         try {