开发中经常会遇到excel的处理,导入导出解析等等。正好最近在项目里面需要用到EasyPOI,非常
容易,简单上手,一行代码实现导入导出。
在springboot项目里面也可以使用如下坐标
cn.afterturn
easypoi-spring-boot-starter
4.0.0
2、导出功能
/** * @Author Steel.D * @Description * @Date 2019-7-30 16:45 * @Param excel的模型集合,请求,响应 * @return **/ @RequestMapping("/load") public void loadExcel(ModelMap map, HttpServletRequest request, HttpServletResponse response){ //将接收的参数进行处理 String transactor = request.getParameter("transactor"); String summary = request.getParameter("summary"); String opetime = request.getParameter("opetime"); //传过来的数据格式为2019-09-12 - 2019-09-30 需要进行处理 opetime = opetime.replaceAll(" ", ""); Mapparams1 = new HashMap<>(); if ( StringUtils.isNotEmpty(opetime)){ String substring = opetime.substring(0, 10); String substring1 = opetime.substring(11); Date date = DateUtils.strToDate(substring); Date date1 = DateUtils.strToDate(substring1); params1.put("opetime",date); params1.put("endtime",date1); } params1.put("transactor",transactor); params1.put("summary",summary); List bills = financeDao.all(params1); ExportParams params = new ExportParams("流水详情", "概览", ExcelType.XSSF); params.setFreezeCol(2); map.put(NormalExcelConstants.DATA_LIST, bills); map.put(NormalExcelConstants.CLASS, Finance.class); map.put(NormalExcelConstants.PARAMS, params); map.put(NormalExcelConstants.FILE_NAME, "编易教育账单流水"); //封装数据进行数据导出 PoiBaseView.render(map,request,response,NormalExcelConstants.EASYPOI_EXCEL_VIEW);
}
3、导入Excel功能
/**
* @Author Steel.D
* @Description
* @Date 2019-7-31 17:32
* @Param
* @return
**/
@PostMapping("/upload")
public ResponseInfo upload(@RequestParam("file") MultipartFile file){
//上传的Excel进行数据模型解析封装 ,四个参数分别为,上传的文件,excel表标题行数,头行数,实体类class ListtStudents = EasyPoiUtil.importExcel(file, 1, 1, TStudent.class); studentService.sava(tStudents); return new ResponseInfo("1","成功"); }
/** * @Author Steel.D * @Description easypoi导入导出通用工具类 * @Date 2019-7-31 9:29 * @Param * @return **/ public class EasyPoiUtil { /** * 功能描述:复杂导出Excel,包括文件名以及表名。创建表头 * * @author Steel.D * @Date 2019-7-31 9:30 * @param list 导出的实体类 * @param title 表头名称 * @param sheetName sheet表名 * @param pojoClass 映射的实体类 * @param isCreateHeader 是否创建表头 * @param fileName * @param response * @return */ public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) { ExportParams exportParams = new ExportParams(title, sheetName); exportParams.setCreateHeadRows(isCreateHeader); defaultExport(list, pojoClass, fileName, response, exportParams); } /** * 功能描述:复杂导出Excel,包括文件名以及表名,不创建表头 * * @author Steel.D * @Date 2019-7-31 9:35 * @param list 导出的实体类 * @param title 表头名称 * @param sheetName sheet表名 * @param pojoClass 映射的实体类 * @param fileName 文件名 * @param response * @return */ public static void exportExcel(List> list, String title, String sheetName, Class> pojoClass, String fileName, HttpServletResponse response) { defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName)); } /** * 功能描述:Map 集合导出 * * @author Steel.D * @Date 2019-7-31 9:45 * @param list 实体集合 * @param fileName 导出的文件名称 * @param response * @return */ public static void exportExcel(List
@Excel(name="账单id") private Integer id; @Excel(name="经办人") private String transactor; @Excel(name = "摘要") private String summary; @Excel(name="详情",width = 50) private String details; @Excel(name="收入") private Integer income; @Excel(name = "支出") private Integer payment; @Excel(name = "累计") private Integer count; @Excel(name = "备注",width = 20) private String comments; @Excel(name="处理时间",exportFormat ="yyyy-MM-dd HH:mm:ss",width = 30)