导出优化
This commit is contained in:
parent
d57335fcf6
commit
6d07feaf46
|
@ -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<Map<String, String>> export() {
|
||||
Map<String, String> map = sysLogLoginService.export();
|
||||
|
||||
return Result.ok(map);
|
||||
public void export() {
|
||||
sysLogLoginService.export();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Map<String, String>> export() {
|
||||
Map<String, String> map = sysUserService.export();
|
||||
|
||||
return Result.ok(map);
|
||||
public void export() {
|
||||
sysUserService.export();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<SysLogLoginEntity> {
|
||||
|
||||
/**
|
||||
* Page page result.
|
||||
* Page result.
|
||||
*
|
||||
* @param query the query
|
||||
* @return the page result
|
||||
|
@ -35,8 +32,6 @@ public interface SysLogLoginService extends BaseService<SysLogLoginEntity> {
|
|||
|
||||
/**
|
||||
* 导出登录日志
|
||||
*
|
||||
* @return the map
|
||||
*/
|
||||
Map<String, String> export();
|
||||
void export();
|
||||
}
|
|
@ -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<SysUserEntity> {
|
|||
|
||||
/**
|
||||
* 导出用户信息表格
|
||||
*
|
||||
* @return map
|
||||
*/
|
||||
Map<String, String> export();
|
||||
void export();
|
||||
}
|
||||
|
|
|
@ -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<SysLogLoginDao, SysLogLoginEntity> implements SysLogLoginService {
|
||||
private final StorageService storageService;
|
||||
|
||||
@Override
|
||||
public PageResult<SysLogLoginVO> page(SysLogLoginQuery query) {
|
||||
|
@ -78,21 +70,12 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl<SysLogLoginDao, SysL
|
|||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Map<String, String> export() {
|
||||
public void export() {
|
||||
List<SysLogLoginEntity> list = list();
|
||||
List<SysLogLoginVO> 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<String, String> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<SysUserDao, SysUserEntity> implements SysUserService {
|
||||
private final SysUserRoleService sysUserRoleService;
|
||||
private final SysUserPostService sysUserPostService;
|
||||
private final StorageService storageService;
|
||||
|
||||
@Override
|
||||
public PageResult<SysUserVO> page(SysUserQuery query) {
|
||||
|
@ -197,21 +194,12 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
|
|||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public Map<String, String> export() {
|
||||
public void export() {
|
||||
List<SysUserEntity> list = list(Wrappers.lambdaQuery(SysUserEntity.class).eq(SysUserEntity::getSuperAdmin, SuperAdminEnum.NO.getValue()));
|
||||
List<SysUserExcelVO> 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<String, String> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,13 +7,17 @@ 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 <a href="https://easyexcel.opensource.alibaba.com/"></a>}
|
||||
*
|
||||
* @author eden
|
||||
*/
|
||||
|
@ -21,30 +25,6 @@ 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文件
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
|
@ -135,4 +115,27 @@ public class ExcelUtils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出数据到web
|
||||
* 文件下载(失败了会返回一个有部分数据的Excel)
|
||||
*
|
||||
* @param head 类名
|
||||
* @param excelName excel名字
|
||||
* @param sheetName sheet名称
|
||||
* @param data 数据
|
||||
*/
|
||||
public static <T> void excelExport(Class<T> head, String excelName, String sheetName, List<T> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String, String> getParameterMap(HttpServletRequest request) {
|
||||
Enumeration<String> parameters = request.getParameterNames();
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ public class EasyExcelTest {
|
|||
excelClass.setString("test");
|
||||
excelClass.setDate(new Date());
|
||||
List<ExcelClass> 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());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user