Excel优雅导出
流程
原来写过一篇文章,是介绍EasyExcel的,但是现在有些业务需要解决,流程如下
1.需要把导出条件转换成中文并存入数据库
2.需要分页导出
3.需要上传FTP或者以后上传OSS
解决方案
大体的流程采用摸板方法模式,这样简化条件转换以及上传FTP操作
public abstract class EasyExcelUtil {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final CloudOrderManagementDao cloudOrderManagementDao;
//上下文对象
private final ExportDTO dto;
//ftp信息
private final FileClient fileClient;
protected final RedisUtil redisUtil;
private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 30,
60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));;
/**
* 构造方法子类必须实现
*
* @param cloudOrderManagementDao dao
* @param dto 上下文对象
* @param fileClient 文件上传对象
* @param redisUtil redis
*/
public EasyExcelUtil(CloudOrderManagementDao cloudOrderManagementDao, ExportDTO dto, FileClient fileClient, RedisUtil redisUtil) {
this.cloudOrderManagementDao = cloudOrderManagementDao;
this.dto = dto;
this.fileClient = fileClient;
this.redisUtil = redisUtil;
}
/**
* 主方法
*/
public final void createExcel() {
try {
File file = this.createFile();
CloudExportInfo exportInfo = this.createExportInfo(file.getName());
threadPoolExecutor.execute(() -> {
this.easyCreate(file);
this.uploadFile(file);
updateExportInfo(exportInfo);
});
} catch (Exception e) {
logger.error("ExcelUtil error{}", e);
}
}
/**
* 创建文件
*
* @return 文件对象
* @throws IOException
*/
private File createFile() throws IOException {
File temp = File.createTempFile("temp", ".xlsx");
logger.info("ExcelUtil创建文件成功{}", temp.getAbsolutePath());
return temp;
}
/**
* 创建导出对象,并存数据库
*
* @param fileName 文件名
* @return 导出对象
*/
private CloudExportInfo createExportInfo(String fileName) {
CloudExportInfo exportInfo = new CloudExportInfo();
exportInfo.setId(dto.getUuid());
exportInfo.setUserId(dto.getUserId());
exportInfo.setCreateBy(dto.getUserName());
exportInfo.setExportStatus(com.blgroup.vision.common.utils.R.CloudConstant.TWO);
exportInfo.setExportType(dto.getExportType());
exportInfo.setQueryParam(this.transitionQuery());
exportInfo.setExportCount(dto.getTotal());
exportInfo.setFileName(fileName);
exportInfo.setExportDir(dto.getBasisDir() + File.separator + fileName);
cloudOrderManagementDao.saveExportInfo(exportInfo);// 初始化导出信息
redisUtil.set(R.CloudConstant.CLOUD_DOWNLOAD_PROGRESS + "_" + dto.getUuid(), "5", 60 * 5);
logger.info("ExcelUtil创建导出信息成功{}", JSON.toJSONString(exportInfo));
return exportInfo;
}
/**
* 上传文件
*
* @param file 文件,策略模式 未来可能支持OSS
*/
private void uploadFile(File file) {
redisUtil.set(R.CloudConstant.CLOUD_DOWNLOAD_PROGRESS + "_" + dto.getUuid(), "95", 60 * 5);
fileClient.uploadFile(file, dto.getBasisDir());
redisUtil.set(R.CloudConstant.CLOUD_DOWNLOAD_PROGRESS + "_" + dto.getUuid(), "100", 60 * 5);
}
/**
* 上传完成,更新导出对象
*
* @param exportInfo
* @return
*/
private CloudExportInfo updateExportInfo(CloudExportInfo exportInfo) {
exportInfo.setExportStatus(com.blgroup.vision.common.utils.R.CloudConstant.ONE);
cloudOrderManagementDao.saveExportInfo(exportInfo);// 初始化导出信息
logger.info("ExcelUtil上完成");
return exportInfo;
}
/**
* 导出方法,可以实现分批导出,也可以一次性导出
*
* @param file 文件
*/
public abstract void easyCreate(File file);
/**
* 对查询字段进行转换
* @return
*/
public abstract String transitionQuery();
}
这样子类只需要实现easyCreate和transitionQuery方法即可
查询条件转换
针对查询条件转换,例如QO字段为'merchantId' 需要转换为商户值需要转换为ID对应的商户名称
如果是每个导出方法写一段转换类太麻烦这里使用注解的方式
在属性上使用表明中文意思和转换方法
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface FieldNameTransition {
/**
* 中文名称
* @return
*/
String value();
String transitionMethod() default "";
}
指定转换类
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
/**
* 指定转换类
*/
public @interface TransitionClass {
Class<?> value();
}
通过反射进行转换
public class ExcelTransitionFieldUtil {
public static Logger logger = LoggerFactory.getLogger(ExcelTransitionFieldUtil.class);
private final ApplicationContext applicationContext;
public ExcelTransitionFieldUtil(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String transition(Object object) {
List<Map<String, String>> resultList = new ArrayList<>();
Object transitionBean = null;
//获取传过来的对象
Class<?> dtoClass = object.getClass();
if (dtoClass.isAnnotationPresent(TransitionClass.class)) {
//获取类上的注解,得到转换类 获取spring ioc中转换类的对象
transitionBean = applicationContext.getBean(dtoClass.getAnnotation(TransitionClass.class).value());
}
//获取对象的全部字段
Field[] fields = dtoClass.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(FieldNameTransition.class)) {
field.setAccessible(true);
//获取字段中需要转换的注解
try {
if (field.get(object) != null && StringUtils.isNotBlank(field.get(object).toString())) {
FieldNameTransition fieldAnnotation = field.getAnnotation(FieldNameTransition.class);
Map<String, String> tempMap = new HashMap<>();
tempMap.put("name", fieldAnnotation.value());
//如果定义了转换方法
if (StringUtils.isNotBlank(fieldAnnotation.transitionMethod()) && transitionBean != null) {
Method method = transitionBean.getClass().getMethod(fieldAnnotation.transitionMethod(), dtoClass);
Object invoke = method.invoke(transitionBean, object);
tempMap.put("value", invoke.toString());
} else {
tempMap.put("value", field.get(object).toString());
}
resultList.add(tempMap);
}
}catch (Exception e){
logger.error("反射转换发生异常 {}", e);
}
}
}
return JSON.toJSONString(resultList);
}
}
使用
QueryMerchantExportQO queryDTO = QueryMerchantExportQO.builder()
.isNeedAudit(request.getParameter("isNeedAudit"))
.keyword(request.getParameter("keyword"))
.merchantType(request.getParameter("merchantType"))
.shopId(request.getParameter("shopId"))
.storeCode(request.getParameter("storeCode"))
.storeType(request.getParameter("storeType"))
.hideFlag(request.getParameter("hideFlag")).build();
ExportDTO exportDTO = ExportDTO.builder().userId(UserUtils.getUser().getId())
.userName(UserUtils.getUser().getName())
.exportType("9")
.uuid(UUID.randomUUID().toString())
.basisDir("/cloudDownFile" + File.separator + LocalDate.now().getYear() + File.separator + LocalDate.now().getMonth().getValue())
.total(Integer.valueOf(request.getParameter("total")))
.build();
FtpInfo ftpInfo = new FtpInfo(server, uname, pwd, port);
new EasyExcelUtil(cloudOrderManagementDao, exportDTO, new FtpFileClient(ftpInfo), redisUtil) {
@Override
public void easyCreate(File file) {
//do something
}
@Override
public String transitionQuery() {
//转换
return new ExcelTransitionFieldUtil(applicationContext).transition(queryDTO);
}
}.createExcel();
}
结尾
现在问题是转换类型只能是String类型,以后可能改进
Excel优雅导出的更多相关文章
- laravel Excel导入导出
1.简介 Laravel Excel 在 Laravel 5 中集成 PHPOffice 套件中的 PHPExcel,从而方便我们以优雅的.富有表现力的代码实现Excel/CSV文件的导入和导出. 该 ...
- 【基于WinForm+Access局域网共享数据库的项目总结】之篇二:WinForm开发扇形图统计和Excel数据导出
篇一:WinForm开发总体概述与技术实现 篇二:WinForm开发扇形图统计和Excel数据导出 篇三:Access远程连接数据库和窗体打包部署 [小记]:最近基于WinForm+Access数据库 ...
- C# 之 EXCEL导入导出
以下方式是本人总结的一些经验,肯定有很多种方法,在此先记下,留待以后补充... 希望朋友们一起来探讨相关想法,请在下方留言. A-1:EXCEL模板导出 非常简单,将EXCEL模板上传到项目中后,将其 ...
- 利用反射实现通用的excel导入导出
如果一个项目中存在多种信息的导入导出,为了简化代码,就需要用反射实现通用的excel导入导出 实例代码如下: 1.创建一个 Book类,并编写set和get方法 package com.bean; p ...
- Excel导入导出的业务进化场景及组件化的设计方案(上)
1:前言 看过我文章的网友们都知道,通常前言都是我用来打酱油扯点闲情的. 自从写了上面一篇文章之后,领导就找我谈话了,怕我有什么想不开. 所以上一篇的(下)篇,目前先不出来了,哪天我异地二次回忆的时候 ...
- java实现excel模板导出
一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...
- Excel导入-----导出(包含所选和全部)操作
在做系统的时候,很多时候信息量太大,这时候就需要进行Excel表格信息的导入和导出,今天就来给大家说一下我使用Excel表格信息导入和导出的心得. 1:首先需要在前端显示界面View视图中添加导入Ex ...
- C#实现Excel模板导出和从Excel导入数据
午休时间写了一个Demo关于Excel导入导出的简单练习 1.窗体 2.引用office命名空间 添加引用-程序集-扩展-Microsoft.Office.Interop.Excel 3.封装的Exc ...
- 如何将jsp页面的table报表转换到excel报表导出
假设这就是你的jsp页面: 我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中.那么这个页面会变成这个样子 在此,强调一下搜索时关键词的重要性,这样一下子可以定位到文章, ...
随机推荐
- RedisEclipse
1.Eclipse配置 2.HelloWorld import redis.clients.jedis.Jedis; public class TestPing { public static voi ...
- Java8 方法引用和构造方法引用
如果不熟悉Java8新特性的小伙伴,初次看到函数式接口写出的代码可能会是一种懵逼的状态,我是谁,我在哪,我可能学了假的Java,(・∀・(・∀・(・∀・*),但是语言都是在进步的,就好比面向对象的语言 ...
- Spring Cloud注册中心之Consul
Consul简介 Consul是HashiCorp公司使用Golang语言开发的一中多服务解决方案工具,相比于其他服务注册中心来说,Consul的功能更为强大,丰富,其中最基本的功能包含下面几点(翻译 ...
- CVE-2017-11882利用
CVE-2017-11882是微软公布的远程执行漏洞,通杀所有office版本及Windows操作系统 工具使用 本文使用的EXP来源于unamer/CVE-2017-11882,然后结合MSF进行渗 ...
- webug第十五关:什么?图片上传不了?
第十五关:什么?图片上传不了? 直接上传php一句话失败,将content type改为图片 成功
- 阿里云的nginx的https配置问题
server { listen 443 ssl; server_name www.xxx域名.com; root html; index index.html index.html; ssl_cert ...
- 深度分析:面试阿里,字节跳动,美团90%被问到的List集合,看完还不懂算我输
1 List集合 1.1 List概述 在Collection中,List集合是有序的,可对其中每个元素的插入位置进行精确地控制,可以通过索引来访问元素,遍历元素. 在List集合中,我们常用到Arr ...
- 牛客编程巅峰赛S2第4场
牛客编程巅峰赛S2第4场 牛牛摆玩偶 题目描述 牛牛有\(n(2 \leq n \leq 10^5)(2≤n≤105)\)个玩偶,牛牛打算把这n个玩偶摆在桌子上,桌子的形状的长条形的,可以看做一维数轴 ...
- 知识解析:C语言函数有一些什么?为你呈现最全函数大全
大家双节快乐呀~国庆节过去了一半,大家放了几天假呀?玩的开心吗? 如果假日没有其他安排,不要宅在家虚度光阴哦~看看我的文章或者视频学习一些知识吧~ 今天为大家分享C语言库函数知识. 以下图片以字母 ...
- mysql 优化数据类型
1.更小的通常更好 选择不会超过范围的最小类型 2.简单就好 例如,整型比字符操作代价更低,因为字符集和校对规则(排序规则)使字符比较比整形比较更复杂. 3.尽量避免null 如果查询中包含可为nul ...