第一步,首先添加依赖
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.0.3</version> </dependency>
第二步,在公共类里面写一个beanUtil类
package com.ruoyi.system.order; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.ImportParams; import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; public class ExcelUtil { 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); } //导出 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)); } public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){ defaultExport(list, fileName, response); } private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) { Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list); if (workbook != null) { downLoadExcel(fileName, response, workbook); } } private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) { try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) { Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF); if (workbook != null) { downLoadExcel(fileName, response, workbook); } } //导入 public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){ if (StringUtils.isBlank(filePath)){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params); }catch (NoSuchElementException e){ throw new RuntimeException("模板不能为空"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } return list; } public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){ if (file == null){ return null; } ImportParams params = new ImportParams(); params.setTitleRows(titleRows); params.setHeadRows(headerRows); List<T> list = null; try { list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params); }catch (NoSuchElementException e){ throw new RuntimeException("excel文件不能为空"); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return list; } }
第三步,实体类。orderNum指定excel的列。name为excel表头。
public class UserTest implements Serializable { /** 用户id */ private String userId; /** 用户昵称 */ @Excel(name = "用户昵称",orderNum = "0") private String userNickname; // @ApiModelProperty(value = "联系方式") @Excel(name = "联系方式",orderNum = "1") @JsonProperty("bindPhone") private String bindPhone; /** 用户头像链接 */ @Excel(name = "用户头像链接",orderNum = "2") private String avatar;}
第四步,就是controller->service->mapper>xml
@RestController @CrossOrigin @RequestMapping(value = "/foreign") public class ExcelTest { @Autowired private IJJmopUserMapper ijJmopUserMapper; @RequestMapping(value = "/find/excelTest",method = RequestMethod.GET) public void findUserTest(HttpServletResponse response){ Map map1 = new HashMap(); List <UserTest>list = new ArrayList(); List<JJmopUser>jJmopUserList =ijJmopUserMapper.selectJJmopUserList(map1); //我这里因为测试实体类有区别,所以做了一个转换,正常可以直接jJmopUserList 替代list for (JJmopUser jJmopUser:jJmopUserList){ UserTest userTest = new UserTest(); BeanUtils.copyProperties(jJmopUser,userTest); list.add(userTest); } ExcelUtil.exportExcel(list,"用户信息","sheet1",UserTest.class,"testDATA.xls", (HttpServletResponse) response); } }
本博客也是借鉴其他大佬的,原文链接:Springboot 最简单的结合MYSQL数据实现EXCEL表格导出及数据导入_小目标青年的博客-优快云博客_springboot导入excel数据