Przeglądaj źródła

新增excel导入和导出

赵冬冬 4 lat temu
rodzic
commit
ead67a7a60

+ 54 - 3
forest-admin/admin-server/src/main/java/com/hwrj/cloud/admin/controller/PmsProductController.java

@@ -19,6 +19,8 @@ import com.hwrj.cloud.common.api.CommonResult;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
 import org.springframework.util.StringUtils;
 import org.springframework.validation.BindingResult;
 import org.springframework.validation.annotation.Validated;
@@ -26,8 +28,10 @@ import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+import java.io.*;
+import java.net.URLEncoder;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * 商品管理Controller
@@ -76,7 +80,7 @@ public class PmsProductController {
     @ApiOperation("根据供应商选择生产地")
     @GetMapping(value = "/plOfPro/select")
     @ResponseBody
-    public CommonResult plOfProSelect(@RequestParam(value = "compId",required = false) Long compId) {
+    public CommonResult plOfProSelect(@RequestParam(value = "compId", required = false) Long compId) {
 
         List<SelectVo> plOfProSelect = productService.plOfProSelect(compId);
         return CommonResult.success(plOfProSelect);
@@ -182,7 +186,7 @@ public class PmsProductController {
     @ApiOperation("通过id查询商品信息")
     @RequestMapping(value = "/getProductByProductId", method = RequestMethod.GET)
     @ResponseBody
-    public CommonResult<PmsProductOutput> getProductByProductId(@RequestParam("productId") Long productId ) {
+    public CommonResult<PmsProductOutput> getProductByProductId(@RequestParam("productId") Long productId) {
         PmsProductOutput pmsProduct = productService.getProductByProductId(productId);
         return CommonResult.success(pmsProduct);
     }
@@ -194,4 +198,51 @@ public class PmsProductController {
         productService.importBasic(file);
         return CommonResult.success();
     }
+
+    @PostMapping(value = "/out")
+    public void out(HttpServletResponse response) throws IOException {
+
+        //定义文件名和文件地址
+        String downloadUrl = "excel/model/productModel.xlsx";
+        Resource resource = null;
+        try {
+            //获取资源文件
+            resource = new ClassPathResource(downloadUrl);
+        } catch (Exception e) {
+            //自定义业务异常
+            throw new RuntimeException("导出模板失败!");
+        }
+        byte[] buff = new byte[1024];
+        BufferedInputStream bis = null;
+        OutputStream os = null;
+        try {
+            // 配置文件下载
+            response.setHeader("content-type", "application/octet-stream");
+            response.setContentType("application/octet-stream");
+            // 下载文件能正常显示中文
+            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("商品导入模板.xlsx", "UTF-8"));
+
+            os = response.getOutputStream();
+            bis = new BufferedInputStream(resource.getInputStream());
+            int i = bis.read(buff);
+            while (i != -1) {
+                os.write(buff, 0, buff.length);
+                os.flush();
+                i = bis.read(buff);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new RuntimeException("文件下载出错!");
+        } finally {
+            if (bis != null) {
+                try {
+                    bis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+
+
+    }
 }

+ 2 - 0
forest-admin/admin-server/src/main/java/com/hwrj/cloud/admin/service/PmsProductService.java

@@ -14,6 +14,7 @@ import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 
 /**
@@ -95,4 +96,5 @@ public interface PmsProductService {
     int verify(Long id);
 
     void importBasic(MultipartFile file);
+
 }

+ 3 - 1
forest-admin/admin-server/src/main/java/com/hwrj/cloud/admin/service/impl/PmsProductServiceImpl.java

@@ -34,6 +34,7 @@ import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.math.BigDecimal;
@@ -477,7 +478,7 @@ public class PmsProductServiceImpl implements PmsProductService {
         if (file != null) {
             List<Object> list = null;
             try {
-                list = EasyExcelUtil.readExcel(file, new PmsProductExcel(), 3, 2);
+                list = EasyExcelUtil.readExcel(file, new PmsProductExcel(), 1, 2);
             } catch (IOException e) {
                 //e.printStackTrace();
                 throw new RuntimeException("上传文件错误!");
@@ -608,4 +609,5 @@ public class PmsProductServiceImpl implements PmsProductService {
         return dest;
     }
 
+
 }

BIN
forest-admin/admin-server/src/main/resources/excel/model/productModel.xlsx