From 6d07feaf4640694cd7a7037130c90c945e43ad76 Mon Sep 17 00:00:00 2001 From: eden Date: Wed, 9 Nov 2022 17:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SysLogLoginController.java | 7 +-- .../system/controller/SysUserController.java | 7 +-- .../system/service/SysLogLoginService.java | 9 +--- .../maku/system/service/SysUserService.java | 5 +- .../service/impl/SysLogLoginServiceImpl.java | 27 ++-------- .../service/impl/SysUserServiceImpl.java | 22 ++------ .../framework/common/utils/ExcelUtils.java | 51 ++++++++++--------- .../common/utils/HttpContextUtils.java | 12 +++++ .../src/test/java/net/maku/EasyExcelTest.java | 2 +- 9 files changed, 57 insertions(+), 85 deletions(-) diff --git a/maku-boot-system/src/main/java/net/maku/system/controller/SysLogLoginController.java b/maku-boot-system/src/main/java/net/maku/system/controller/SysLogLoginController.java index b1f56f6..507bba4 100644 --- a/maku-boot-system/src/main/java/net/maku/system/controller/SysLogLoginController.java +++ b/maku-boot-system/src/main/java/net/maku/system/controller/SysLogLoginController.java @@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; -import java.util.Map; /** * 登录日志 @@ -40,10 +39,8 @@ public class SysLogLoginController { @GetMapping("export") @Operation(summary = "导出excel") @PreAuthorize("hasAuthority('sys:log:login')") - public Result> export() { - Map map = sysLogLoginService.export(); - - return Result.ok(map); + public void export() { + sysLogLoginService.export(); } } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/controller/SysUserController.java b/maku-boot-system/src/main/java/net/maku/system/controller/SysUserController.java index 2ba2105..eda4c6f 100644 --- a/maku-boot-system/src/main/java/net/maku/system/controller/SysUserController.java +++ b/maku-boot-system/src/main/java/net/maku/system/controller/SysUserController.java @@ -23,7 +23,6 @@ import org.springframework.web.multipart.MultipartFile; import javax.validation.Valid; import java.util.List; -import java.util.Map; /** @@ -155,9 +154,7 @@ public class SysUserController { @GetMapping("export") @Operation(summary = "导出用户") @PreAuthorize("hasAuthority('sys:user:export')") - public Result> export() { - Map map = sysUserService.export(); - - return Result.ok(map); + public void export() { + sysUserService.export(); } } diff --git a/maku-boot-system/src/main/java/net/maku/system/service/SysLogLoginService.java b/maku-boot-system/src/main/java/net/maku/system/service/SysLogLoginService.java index e437850..183ea3d 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/SysLogLoginService.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/SysLogLoginService.java @@ -6,9 +6,6 @@ import net.maku.system.entity.SysLogLoginEntity; import net.maku.system.query.SysLogLoginQuery; import net.maku.system.vo.SysLogLoginVO; -import java.io.IOException; -import java.util.Map; - /** * 登录日志 * @@ -17,7 +14,7 @@ import java.util.Map; public interface SysLogLoginService extends BaseService { /** - * Page page result. + * Page result. * * @param query the query * @return the page result @@ -35,8 +32,6 @@ public interface SysLogLoginService extends BaseService { /** * 导出登录日志 - * - * @return the map */ - Map export(); + void export(); } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/SysUserService.java b/maku-boot-system/src/main/java/net/maku/system/service/SysUserService.java index 4905e55..8c042f8 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/SysUserService.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/SysUserService.java @@ -9,7 +9,6 @@ import net.maku.system.vo.SysUserVO; import org.springframework.web.multipart.MultipartFile; import java.util.List; -import java.util.Map; /** * 用户管理 @@ -51,8 +50,6 @@ public interface SysUserService extends BaseService { /** * 导出用户信息表格 - * - * @return map */ - Map export(); + void export(); } diff --git a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java index 76f6dc4..4a73b91 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysLogLoginServiceImpl.java @@ -1,6 +1,5 @@ package net.maku.system.service.impl; -import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -9,10 +8,7 @@ import lombok.AllArgsConstructor; import lombok.SneakyThrows; import net.maku.framework.common.page.PageResult; import net.maku.framework.common.service.impl.BaseServiceImpl; -import net.maku.framework.common.utils.AddressUtils; -import net.maku.framework.common.utils.ExcelUtils; -import net.maku.framework.common.utils.HttpContextUtils; -import net.maku.framework.common.utils.IpUtils; +import net.maku.framework.common.utils.*; import net.maku.storage.service.StorageService; import net.maku.system.convert.SysLogLoginConvert; import net.maku.system.dao.SysLogLoginDao; @@ -24,11 +20,8 @@ import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; -import java.io.File; -import java.nio.file.Files; -import java.util.HashMap; +import java.util.Date; import java.util.List; -import java.util.Map; /** * 登录日志 @@ -38,7 +31,6 @@ import java.util.Map; @Service @AllArgsConstructor public class SysLogLoginServiceImpl extends BaseServiceImpl implements SysLogLoginService { - private final StorageService storageService; @Override public PageResult page(SysLogLoginQuery query) { @@ -78,21 +70,12 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl export() { + public void export() { List list = list(); List sysLogLoginVOS = SysLogLoginConvert.INSTANCE.convertList(list); - File file = File.createTempFile("system_login_log_excel", ".xlsx"); - // 写入到文件 - ExcelUtils.excelExport(SysLogLoginVO.class, file, sysLogLoginVOS); - - byte[] data = IoUtil.readBytes(Files.newInputStream(file.toPath())); - String path = storageService.getPath(file.getName()); - String url = storageService.upload(data, path); - Map map = new HashMap<>(2); - map.put("path", url); - map.put("filename", file.getName()); - return map; + // 写到浏览器打开 + ExcelUtils.excelExport(SysLogLoginVO.class, "system_login_log_excel" + DateUtils.format(new Date()), null, sysLogLoginVOS); } } \ No newline at end of file diff --git a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java index e97d370..7f4942c 100644 --- a/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java +++ b/maku-boot-system/src/main/java/net/maku/system/service/impl/SysUserServiceImpl.java @@ -1,6 +1,5 @@ package net.maku.system.service.impl; -import cn.hutool.core.io.IoUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.AllArgsConstructor; @@ -10,8 +9,8 @@ import net.maku.framework.common.excel.ExcelFinishCallBack; import net.maku.framework.common.exception.ServerException; import net.maku.framework.common.page.PageResult; import net.maku.framework.common.service.impl.BaseServiceImpl; +import net.maku.framework.common.utils.DateUtils; import net.maku.framework.common.utils.ExcelUtils; -import net.maku.storage.service.StorageService; import net.maku.system.convert.SysUserConvert; import net.maku.system.dao.SysUserDao; import net.maku.system.entity.SysUserEntity; @@ -27,8 +26,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.nio.file.Files; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -43,7 +41,6 @@ import java.util.Map; public class SysUserServiceImpl extends BaseServiceImpl implements SysUserService { private final SysUserRoleService sysUserRoleService; private final SysUserPostService sysUserPostService; - private final StorageService storageService; @Override public PageResult page(SysUserQuery query) { @@ -197,21 +194,12 @@ public class SysUserServiceImpl extends BaseServiceImpl export() { + public void export() { List list = list(Wrappers.lambdaQuery(SysUserEntity.class).eq(SysUserEntity::getSuperAdmin, SuperAdminEnum.NO.getValue())); List userExcelVOS = SysUserConvert.INSTANCE.convert2List(list); - File file = File.createTempFile("system_user_excel", ".xlsx"); - // 写入到文件 - ExcelUtils.excelExport(SysUserExcelVO.class, file, userExcelVOS); - - byte[] data = IoUtil.readBytes(Files.newInputStream(file.toPath())); - String path = storageService.getPath(file.getName()); - String url = storageService.upload(data, path); - Map map = new HashMap<>(2); - map.put("path", url); - map.put("filename", file.getName()); - return map; + // 写到浏览器打开 + ExcelUtils.excelExport(SysUserExcelVO.class, "system_user_excel" + DateUtils.format(new Date()), null, userExcelVOS); } } diff --git a/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java b/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java index 181ebba..75e58a3 100644 --- a/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java +++ b/maku-framework/src/main/java/net/maku/framework/common/utils/ExcelUtils.java @@ -7,43 +7,23 @@ import net.maku.framework.common.excel.ExcelFinishCallBack; import org.apache.commons.lang3.StringUtils; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.List; /** * The type Excel utils. + * {@link } * * @author eden */ public class ExcelUtils { - /** - * 判断excel文件类型 - * - * @param file 源头文件 - * @return type - */ - public static ExcelTypeEnum getExcelFileType(MultipartFile file) { - String filename = file.getOriginalFilename(); - if (StringUtils.isNotBlank(filename)) { - filename = filename.substring(filename.lastIndexOf(".")); - switch (filename) { - case ".csv": - return ExcelTypeEnum.CSV; - case ".xls": - return ExcelTypeEnum.XLS; - case ".xlsx": - return ExcelTypeEnum.XLSX; - default: - throw new IllegalArgumentException("无效的文件"); - } - } - throw new IllegalArgumentException("无效的文件"); - } - /** * 读取excel文件 * @@ -135,4 +115,27 @@ public class ExcelUtils { } } + /** + * 导出数据到web + * 文件下载(失败了会返回一个有部分数据的Excel) + * + * @param head 类名 + * @param excelName excel名字 + * @param sheetName sheet名称 + * @param data 数据 + */ + public static void excelExport(Class head, String excelName, String sheetName, List data) { + try { + HttpServletResponse response = HttpContextUtils.getHttpServletResponse(); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("utf-8"); + // 这里URLEncoder.encode可以防止中文乱码 当然和easy excel没有关系 + String fileName = URLEncoder.encode(excelName, StandardCharsets.UTF_8).replaceAll("\\+", "%20"); + response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); + EasyExcel.write(response.getOutputStream(), head).sheet(StringUtils.isBlank(sheetName) ? "sheet1" : sheetName).doWrite(data); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } diff --git a/maku-framework/src/main/java/net/maku/framework/common/utils/HttpContextUtils.java b/maku-framework/src/main/java/net/maku/framework/common/utils/HttpContextUtils.java index 4928c4a..bc53e78 100644 --- a/maku-framework/src/main/java/net/maku/framework/common/utils/HttpContextUtils.java +++ b/maku-framework/src/main/java/net/maku/framework/common/utils/HttpContextUtils.java @@ -7,6 +7,7 @@ import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; @@ -27,6 +28,17 @@ public class HttpContextUtils { return ((ServletRequestAttributes) requestAttributes).getRequest(); } + + public static HttpServletResponse getHttpServletResponse () { + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + if(requestAttributes == null){ + return null; + } + + return ((ServletRequestAttributes) requestAttributes).getResponse(); + } + + public static Map getParameterMap(HttpServletRequest request) { Enumeration parameters = request.getParameterNames(); diff --git a/maku-server/src/test/java/net/maku/EasyExcelTest.java b/maku-server/src/test/java/net/maku/EasyExcelTest.java index 7433955..7fd1934 100644 --- a/maku-server/src/test/java/net/maku/EasyExcelTest.java +++ b/maku-server/src/test/java/net/maku/EasyExcelTest.java @@ -45,7 +45,7 @@ public class EasyExcelTest { excelClass.setString("test"); excelClass.setDate(new Date()); List data = Arrays.asList(excelClass, excelClass, excelClass, excelClass, excelClass, excelClass, excelClass); - if (!file.exists()) { + if (file.exists()) { ExcelUtils.excelExport(ExcelClass.class, file, data); ExcelUtils.readAnalysis(file, ExcelClass.class, new ServiceA()); } -- Gitee