优化导出数据操作

This commit is contained in:
RuoYi 2021-11-17 11:57:55 +08:00
parent 6b4e3c9e1d
commit bbe817b7ce
11 changed files with 59 additions and 47 deletions

View File

@ -431,7 +431,7 @@ public class ExcelUtil<T>
* @return 结果 * @return 结果
* @throws IOException * @throws IOException
*/ */
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)throws IOException public void exportExcel(HttpServletResponse response, List<T> list, String sheetName)
{ {
exportExcel(response, list, sheetName, StringUtils.EMPTY); exportExcel(response, list, sheetName, StringUtils.EMPTY);
} }
@ -446,12 +446,12 @@ public class ExcelUtil<T>
* @return 结果 * @return 结果
* @throws IOException * @throws IOException
*/ */
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title) throws IOException public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title)
{ {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
this.init(list, sheetName, title, Type.EXPORT); this.init(list, sheetName, title, Type.EXPORT);
exportExcel(response.getOutputStream()); exportExcel(response);
} }
/** /**
@ -484,7 +484,7 @@ public class ExcelUtil<T>
* @param sheetName 工作表的名称 * @param sheetName 工作表的名称
* @return 结果 * @return 结果
*/ */
public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException public void importTemplateExcel(HttpServletResponse response, String sheetName)
{ {
importTemplateExcel(response, sheetName, StringUtils.EMPTY); importTemplateExcel(response, sheetName, StringUtils.EMPTY);
} }
@ -496,12 +496,12 @@ public class ExcelUtil<T>
* @param title 标题 * @param title 标题
* @return 结果 * @return 结果
*/ */
public void importTemplateExcel(HttpServletResponse response, String sheetName, String title) throws IOException public void importTemplateExcel(HttpServletResponse response, String sheetName, String title)
{ {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
this.init(null, sheetName, title, Type.IMPORT); this.init(null, sheetName, title, Type.IMPORT);
exportExcel(response.getOutputStream()); exportExcel(response);
} }
/** /**
@ -509,12 +509,12 @@ public class ExcelUtil<T>
* *
* @return 结果 * @return 结果
*/ */
public void exportExcel(OutputStream out) public void exportExcel(HttpServletResponse response)
{ {
try try
{ {
writeSheet(); writeSheet();
wb.write(out); wb.write(response.getOutputStream());
} }
catch (Exception e) catch (Exception e)
{ {
@ -523,7 +523,6 @@ public class ExcelUtil<T>
finally finally
{ {
IOUtils.closeQuietly(wb); IOUtils.closeQuietly(wb);
IOUtils.closeQuietly(out);
} }
} }

View File

@ -1,6 +1,7 @@
package com.ruoyi.project.monitor.controller; package com.ruoyi.project.monitor.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.quartz.SchedulerException; import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
@ -54,12 +55,12 @@ public class SysJobController extends BaseController
*/ */
@PreAuthorize("@ss.hasPermi('monitor:job:export')") @PreAuthorize("@ss.hasPermi('monitor:job:export')")
@Log(title = "定时任务", businessType = BusinessType.EXPORT) @Log(title = "定时任务", businessType = BusinessType.EXPORT)
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysJob sysJob) public void export(HttpServletResponse response, SysJob sysJob)
{ {
List<SysJob> list = jobService.selectJobList(sysJob); List<SysJob> list = jobService.selectJobList(sysJob);
ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class); ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
return util.exportExcel(list, "定时任务"); util.exportExcel(response, list, "定时任务");
} }
/** /**

View File

@ -1,21 +1,23 @@
package com.ruoyi.project.monitor.controller; package com.ruoyi.project.monitor.controller;
import java.util.List; import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize; import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log; import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType; import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.monitor.domain.SysJobLog;
import com.ruoyi.project.monitor.service.ISysJobLogService;
import com.ruoyi.framework.web.controller.BaseController; import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult; import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo; import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.monitor.domain.SysJobLog;
import com.ruoyi.project.monitor.service.ISysJobLogService;
/** /**
* 调度日志操作处理 * 调度日志操作处理
@ -46,12 +48,12 @@ public class SysJobLogController extends BaseController
*/ */
@PreAuthorize("@ss.hasPermi('monitor:job:export')") @PreAuthorize("@ss.hasPermi('monitor:job:export')")
@Log(title = "任务调度日志", businessType = BusinessType.EXPORT) @Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysJobLog sysJobLog) public void export(HttpServletResponse response, SysJobLog sysJobLog)
{ {
List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog); List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class); ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
return util.exportExcel(list, "调度日志"); util.exportExcel(response, list, "调度日志");
} }
/** /**

View File

@ -1,11 +1,13 @@
package com.ruoyi.project.monitor.controller; package com.ruoyi.project.monitor.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.poi.ExcelUtil;
@ -40,12 +42,12 @@ public class SysLogininforController extends BaseController
@Log(title = "登录日志", businessType = BusinessType.EXPORT) @Log(title = "登录日志", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('monitor:logininfor:export')") @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysLogininfor logininfor) public void export(HttpServletResponse response, SysLogininfor logininfor)
{ {
List<SysLogininfor> list = logininforService.selectLogininforList(logininfor); List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class); ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
return util.exportExcel(list, "登录日志"); util.exportExcel(response, list, "登录日志");
} }
@PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')") @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")

View File

@ -1,11 +1,13 @@
package com.ruoyi.project.monitor.controller; package com.ruoyi.project.monitor.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.utils.poi.ExcelUtil;
@ -40,12 +42,12 @@ public class SysOperlogController extends BaseController
@Log(title = "操作日志", businessType = BusinessType.EXPORT) @Log(title = "操作日志", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('monitor:operlog:export')") @PreAuthorize("@ss.hasPermi('monitor:operlog:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysOperLog operLog) public void export(HttpServletResponse response, SysOperLog operLog)
{ {
List<SysOperLog> list = operLogService.selectOperLogList(operLog); List<SysOperLog> list = operLogService.selectOperLogList(operLog);
ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class); ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
return util.exportExcel(list, "操作日志"); util.exportExcel(response, list, "操作日志");
} }
@Log(title = "操作日志", businessType = BusinessType.DELETE) @Log(title = "操作日志", businessType = BusinessType.DELETE)

View File

@ -1,6 +1,7 @@
package com.ruoyi.project.system.controller; package com.ruoyi.project.system.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -48,12 +49,12 @@ public class SysConfigController extends BaseController
@Log(title = "参数管理", businessType = BusinessType.EXPORT) @Log(title = "参数管理", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('system:config:export')") @PreAuthorize("@ss.hasPermi('system:config:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysConfig config) public void export(HttpServletResponse response, SysConfig config)
{ {
List<SysConfig> list = configService.selectConfigList(config); List<SysConfig> list = configService.selectConfigList(config);
ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class); ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
return util.exportExcel(list, "参数数据"); util.exportExcel(response, list, "参数数据");
} }
/** /**

View File

@ -2,6 +2,7 @@ package com.ruoyi.project.system.controller;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -50,12 +51,12 @@ public class SysDictDataController extends BaseController
@Log(title = "字典数据", businessType = BusinessType.EXPORT) @Log(title = "字典数据", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('system:dict:export')") @PreAuthorize("@ss.hasPermi('system:dict:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysDictData dictData) public void export(HttpServletResponse response, SysDictData dictData)
{ {
List<SysDictData> list = dictDataService.selectDictDataList(dictData); List<SysDictData> list = dictDataService.selectDictDataList(dictData);
ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class); ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
return util.exportExcel(list, "字典数据"); util.exportExcel(response, list, "字典数据");
} }
/** /**

View File

@ -1,6 +1,7 @@
package com.ruoyi.project.system.controller; package com.ruoyi.project.system.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -45,12 +46,12 @@ public class SysDictTypeController extends BaseController
@Log(title = "字典类型", businessType = BusinessType.EXPORT) @Log(title = "字典类型", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('system:dict:export')") @PreAuthorize("@ss.hasPermi('system:dict:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysDictType dictType) public void export(HttpServletResponse response, SysDictType dictType)
{ {
List<SysDictType> list = dictTypeService.selectDictTypeList(dictType); List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class); ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
return util.exportExcel(list, "字典类型"); util.exportExcel(response, list, "字典类型");
} }
/** /**

View File

@ -1,6 +1,7 @@
package com.ruoyi.project.system.controller; package com.ruoyi.project.system.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -48,12 +49,12 @@ public class SysPostController extends BaseController
@Log(title = "岗位管理", businessType = BusinessType.EXPORT) @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('system:post:export')") @PreAuthorize("@ss.hasPermi('system:post:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysPost post) public void export(HttpServletResponse response, SysPost post)
{ {
List<SysPost> list = postService.selectPostList(post); List<SysPost> list = postService.selectPostList(post);
ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class); ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
return util.exportExcel(list, "岗位数据"); util.exportExcel(response, list, "岗位数据");
} }
/** /**

View File

@ -1,6 +1,7 @@
package com.ruoyi.project.system.controller; package com.ruoyi.project.system.controller;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -61,12 +62,12 @@ public class SysRoleController extends BaseController
@Log(title = "角色管理", businessType = BusinessType.EXPORT) @Log(title = "角色管理", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('system:role:export')") @PreAuthorize("@ss.hasPermi('system:role:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysRole role) public void export(HttpServletResponse response, SysRole role)
{ {
List<SysRole> list = roleService.selectRoleList(role); List<SysRole> list = roleService.selectRoleList(role);
ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class); ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
return util.exportExcel(list, "角色数据"); util.exportExcel(response, list, "角色数据");
} }
/** /**

View File

@ -2,6 +2,7 @@ package com.ruoyi.project.system.controller;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
@ -62,12 +63,12 @@ public class SysUserController extends BaseController
@Log(title = "用户管理", businessType = BusinessType.EXPORT) @Log(title = "用户管理", businessType = BusinessType.EXPORT)
@PreAuthorize("@ss.hasPermi('system:user:export')") @PreAuthorize("@ss.hasPermi('system:user:export')")
@GetMapping("/export") @PostMapping("/export")
public AjaxResult export(SysUser user) public void export(HttpServletResponse response, SysUser user)
{ {
List<SysUser> list = userService.selectUserList(user); List<SysUser> list = userService.selectUserList(user);
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
return util.exportExcel(list, "用户数据"); util.exportExcel(response, list, "用户数据");
} }
@Log(title = "用户管理", businessType = BusinessType.IMPORT) @Log(title = "用户管理", businessType = BusinessType.IMPORT)
@ -82,11 +83,11 @@ public class SysUserController extends BaseController
return AjaxResult.success(message); return AjaxResult.success(message);
} }
@GetMapping("/importTemplate") @PostMapping("/importTemplate")
public AjaxResult importTemplate() public void importTemplate(HttpServletResponse response)
{ {
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class); ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
return util.importTemplateExcel("用户数据"); util.importTemplateExcel(response, "用户数据");
} }
/** /**