1
0
Просмотр исходного кода

fix: 报表改为返回下载链接

Gudge-ZL 2 лет назад
Родитель
Сommit
2d426c0c8d

+ 3 - 3
src/main/java/com/seecoder/BlueWhale/controller/ExcelController.java

@@ -1,11 +1,11 @@
 package com.seecoder.BlueWhale.controller;
 
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import com.seecoder.BlueWhale.service.ExcelService;
+import com.seecoder.BlueWhale.vo.ResultVO;
 
 import org.springframework.web.bind.annotation.GetMapping;
 
@@ -18,8 +18,8 @@ public class ExcelController {
     ExcelService excelService;
 
     @GetMapping
-    public ResponseEntity<byte[]> export(){
-        return excelService.export();
+    public ResultVO<String> export(){
+        return ResultVO.buildSuccess(excelService.export());
     }
     
 }

+ 1 - 3
src/main/java/com/seecoder/BlueWhale/service/ExcelService.java

@@ -1,7 +1,5 @@
 package com.seecoder.BlueWhale.service;
 
-import org.springframework.http.ResponseEntity;
-
 public interface ExcelService {
-    public ResponseEntity<byte[]> export();
+    public String export();
 }

+ 10 - 3
src/main/java/com/seecoder/BlueWhale/serviceImpl/ExcelServiceImpl.java

@@ -1,12 +1,14 @@
 package com.seecoder.BlueWhale.serviceImpl;
 
+import java.io.InputStream;
+
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
 import com.seecoder.BlueWhale.service.ExcelService;
 import com.seecoder.BlueWhale.service.OrderService;
 import com.seecoder.BlueWhale.util.ExcelUtil;
+import com.seecoder.BlueWhale.util.OssUtil;
 
 @Service
 public class ExcelServiceImpl implements ExcelService{
@@ -14,12 +16,17 @@ public class ExcelServiceImpl implements ExcelService{
     @Autowired
     ExcelUtil excelUtil;
 
+    @Autowired
+    OssUtil ossUtil;
+
     @Autowired
     OrderService orderService;
 
     @Override
-    public ResponseEntity<byte[]> export() {
-        return excelUtil.exportExcel(orderService.getAllOrders());
+    public String export() {
+        InputStream inputStream = excelUtil.exportExcel(orderService.getAllOrders());
+        String url = ossUtil.upload("order.xlsx", inputStream);
+        return url;
     }
     
 }

+ 6 - 45
src/main/java/com/seecoder/BlueWhale/util/ExcelUtil.java

@@ -4,10 +4,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URLEncoder;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.poi.hssf.util.HSSFColor;
 import org.apache.poi.ss.usermodel.BorderStyle;
@@ -16,17 +13,11 @@ import org.apache.poi.ss.usermodel.FillPatternType;
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.HorizontalAlignment;
 import org.apache.poi.ss.usermodel.VerticalAlignment;
-import org.apache.poi.util.IOUtils;
 import org.apache.poi.xssf.streaming.SXSSFCell;
 import org.apache.poi.xssf.streaming.SXSSFRow;
 import org.apache.poi.xssf.streaming.SXSSFSheet;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Component;
-import org.springframework.util.StringUtils;
-
 import com.seecoder.BlueWhale.vo.OrderVO;
 
 @Component
@@ -80,34 +71,10 @@ public class ExcelUtil {
         }
     }
 
-    public ResponseEntity<byte[]> buildResponseEntity(InputStream is, String name) throws Exception {
-
-        HttpHeaders header = new HttpHeaders();
-        String fileSuffix = name.substring(name.lastIndexOf('.') + 1);
-        fileSuffix = fileSuffix.toLowerCase();
-
-        Map<String, String> arguments = new HashMap<String, String>();
-        arguments.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-        arguments.put("xls", "application/vnd.ms-excel");
-
-        String contentType = arguments.get(fileSuffix);
-        header.add("Content-Type", (StringUtils.hasText(contentType) ? contentType : "application/x-download"));
-        if (is != null && is.available() != 0) {
-            header.add("Content-Length", String.valueOf(is.available()));
-            header.add("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + URLEncoder.encode(name, "UTF-8"));
-            byte[] bs = IOUtils.toByteArray(is);
-            return new ResponseEntity<>(bs, header, HttpStatus.OK);
-        } else {
-            String string = "数据为空";
-            header.add("Content-Length", "0");
-            return new ResponseEntity<>(string.getBytes(), header, HttpStatus.OK);
-        }
-    }
-
-    public ResponseEntity<byte[]> exportExcel(List<OrderVO> orders) {
+    public InputStream exportExcel(List<OrderVO> orders) {
 
         ByteArrayOutputStream output = null;
-        InputStream inputStream1 = null;
+        InputStream inputStream = null;
         SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
         SXSSFSheet sheet = wb.createSheet();
         // 设置报表头样式
@@ -139,7 +106,7 @@ public class ExcelUtil {
             //将wb中的数据以字节形式存到inputStream1中
             output = new ByteArrayOutputStream();
             wb.write(output);
-            inputStream1 = new ByteArrayInputStream(output.toByteArray());
+            inputStream = new ByteArrayInputStream(output.toByteArray());
             output.flush();
         } catch (Exception e) {
             e.printStackTrace();
@@ -147,21 +114,15 @@ public class ExcelUtil {
             try {
                 if (output != null) {
                     output.close();
-                    if (inputStream1 != null) {
-                        inputStream1.close();
+                    if (inputStream != null) {
+                        inputStream.close();
                     }
                 }
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
-
-        try {
-            return buildResponseEntity(inputStream1, "订单列表.xlsx");
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return null;
+        return inputStream;
 
     }
 }