From 346a65b371037dc9bd0408e14c0471a3ba776b83 Mon Sep 17 00:00:00 2001 From: jishenghua <752718920@qq.com> Date: Sun, 14 Jan 2024 15:23:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Excel=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SystemConfigController.java | 26 ++++++++++++--- .../systemConfig/SystemConfigService.java | 33 +++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/jshERP-boot/src/main/java/com/jsh/erp/controller/SystemConfigController.java b/jshERP-boot/src/main/java/com/jsh/erp/controller/SystemConfigController.java index 7af6d140..f73359ef 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/controller/SystemConfigController.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/controller/SystemConfigController.java @@ -1,5 +1,7 @@ package com.jsh.erp.controller; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.jsh.erp.datasource.entities.SystemConfig; import com.jsh.erp.service.depot.DepotService; import com.jsh.erp.service.systemConfig.SystemConfigService; @@ -14,10 +16,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.AntPathMatcher; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.HandlerMapping; @@ -272,6 +271,25 @@ public class SystemConfigController { } } + /** + * Excel导出统一接口 + * @param response + */ + @PostMapping(value = "/exportExcelByParam") + @ApiOperation(value = "生成excel表格") + public void exportExcelByParam(@RequestBody JSONObject jsonObject, + HttpServletResponse response) { + try { + String title = jsonObject.getString("title"); + String head = jsonObject.getString("head"); + String tip = jsonObject.getString("tip"); + JSONArray arr = jsonObject.getJSONArray("list"); + systemConfigService.exportExcelByParam(title, head, tip, arr, response); + } catch (Exception e) { + e.printStackTrace(); + } + } + /** * 把指定URL后的字符串全部截断当成参数 * 这么做是为了防止URL中包含中文或者特殊字符(/等)时,匹配不了的问题 diff --git a/jshERP-boot/src/main/java/com/jsh/erp/service/systemConfig/SystemConfigService.java b/jshERP-boot/src/main/java/com/jsh/erp/service/systemConfig/SystemConfigService.java index 040a4a27..649d98e5 100644 --- a/jshERP-boot/src/main/java/com/jsh/erp/service/systemConfig/SystemConfigService.java +++ b/jshERP-boot/src/main/java/com/jsh/erp/service/systemConfig/SystemConfigService.java @@ -1,5 +1,6 @@ package com.jsh.erp.service.systemConfig; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; @@ -17,6 +18,7 @@ import com.jsh.erp.exception.JshException; import com.jsh.erp.service.log.LogService; import com.jsh.erp.service.platformConfig.PlatformConfigService; import com.jsh.erp.service.user.UserService; +import com.jsh.erp.utils.ExcelUtils; import com.jsh.erp.utils.FileUtils; import com.jsh.erp.utils.StringUtil; import com.jsh.erp.utils.Tools; @@ -34,11 +36,13 @@ import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -483,4 +487,33 @@ public class SystemConfigService { } return inOutManageFlag; } + + /** + * Excel导出统一方法 + * @param title + * @param head + * @param tip + * @param arr + * @param response + * @throws Exception + */ + public void exportExcelByParam(String title, String head, String tip, JSONArray arr, HttpServletResponse response) throws Exception { + List nameList = StringUtil.strToStringList(head); + String[] names = StringUtil.listToStringArray(nameList); + List objects = new ArrayList<>(); + if (null != arr) { + for (Object object: arr) { + List list = (List) object; + String[] objs = new String[100]; + for (int i = 0; i < list.size(); i++) { + if(null != list.get(i)) { + objs[i] = list.get(i).toString(); + } + } + objects.add(objs); + } + } + File file = ExcelUtils.exportObjectsWithoutTitle(title, tip, names, title, objects); + ExcelUtils.downloadExcel(file, file.getName(), response); + } } \ No newline at end of file