excel-start
1 介绍
基于poi的excel导入和基于Jxls的excel导出的基础上进行封装,简化excel的导出和导入,只需要三步即可完成之前需要大量代码实现的功能。
2 安装教程
<dependency>
<groupId>com.gitee.practice-pine</groupId>
<artifactId>excel-start</artifactId>
<version>1.0</version>
</dependency>
3 使用说明
第一步 导入打包之后的pom依赖
第二步 添加配置文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oOCDfI2s-1643076580520)(image-20211230134949485.png)]
在resources文件夹下创建excel文件夹,并且在excel文件夹下创建conf文件夹和template文件夹,
conf文件夹下创建export.properties和import.properties文件,用于存储导入和导出的配置;在template文件夹下放置模板文件
第三步 调用
4 案例
导入excel
1 编写处理解析之后数据的实现类
@Slf4j
@Component
public class DefaultExcelImportEntityImpl implements IExcelImportEntity {
@Override
public List dealData(List<String[]> dataList, String param) {
return dataList;
}
}
//需要实现IExcelImportEntity接口以及添加@Component注解(将本类注入IOC容器)
//可以在此处对数据进行一定的分析、汇总、存储等等,也可以直接返回
2 在import.properties文件中配置实现类的路径
import.configCode.classpath=cn.edu.lnpu.stopcarmanager.mapper.TestExcelImportEntityImpl
// import.configCode.classpath 中的configCode是前端传入的,选择不同的实现类
3 调用
@PostMapping("/import")
public void importExcel(String inParam, @RequestParam("file") MultipartFile file, HttpServletRequest request){
// 4.解析Excel
ImportExcelParamIn in = new ImportExcelParamIn();
in.setConfigCode("configCode");
ImportExcelParamOut importExcelParamOut = entity.importExcel(in , file);
log.info("解析Excel - > {}" , JSON.toJSONString(importExcelParamOut));
}
// excel解析时可以传入额外参数,一般用于条件 ( in.setMoreParam();)
// in.setConfigCode("configCode"); 中的参数和import.configCode.classpath中的configCode对应,用于选择不同的实现类
导出excel
1 编写实现类,查找那些数据,存入excel
@Slf4j
@Component
public class TestExcelExportEntityImpl implements IExcelExportEntity {
// 导出测试方法、此处的key需要和模板中的批注对应,这里是导出的重点,一定要看看模板的批注是怎么写的
@Override
public Map<String, Object> export(String param) {
List<Map> dataList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("a", "a" + i);
dataMap.put("b", "b" + i);
dataMap.put("c", "c" + i);
dataList.add(dataMap);
}
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("items", dataList);
return dataMap;
}
}
//需要实现IExcelExportEntity接口以及添加@Component注解(将本类注入IOC容器)
//可以在此处对数据进行一定的分析、汇总、存储等,然后在将数据存入excel
2 在export.properties中配置实现类路径
export.ConfigCode.classpath=cn.edu.lnpu.stopcarmanager.mapper.TestExcelExportEntityImpl
export.ConfigCode.template.file.path=template.xlsx
// export.configCode.classpath 中的configCode是前端传入的,选择不同的实现类
// export.ConfigCode.template.file.path 中的configCode是前端传入的,选择不同的模板
3 调用
@PostMapping("/export")
public void exportExcel(String inParam, HttpServletRequest request, HttpServletResponse response) {
log.info("exportExcel inParam -> {}", JSON.toJSONString(inParam));
ExportExcelParamIn in = new ExportExcelParamIn();
in.setConfigCode("ConfigCode");
entity.exportExcel(in, response);
}
// excel导出时可以传入额外参数,一般用于条件导出 ( in.setMoreParam();)
// in.setConfigCode("configCode"); 中的参数和export.configCode.classpath中的configCode对应,用于选择不同的实现类也与 export.ConfigCode.template.file.path中的ConfigCode对应,用于选择不同的模板
git地址 : https://gitee.com/practice-pine/excel-start
希望大家小手点一点,嘿嘿