增加Excel导出统一接口

This commit is contained in:
jishenghua
2024-01-14 15:23:47 +08:00
parent 82bdca35c9
commit 346a65b371
2 changed files with 55 additions and 4 deletions

View File

@@ -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中包含中文或者特殊字符/等)时,匹配不了的问题

View File

@@ -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<String> nameList = StringUtil.strToStringList(head);
String[] names = StringUtil.listToStringArray(nameList);
List<String[]> objects = new ArrayList<>();
if (null != arr) {
for (Object object: arr) {
List<Object> list = (List<Object>) 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);
}
}