先进入pom这个只写导出的导入的方法 实体类需要用到的注解要自己去看哦 导出 public static void exportExcel(HttpServletResponse response , String fileName , List> list,Class> clazz) { if(ParamKey.EXCEL_NAME.list.contains(fileName))throw new baseException("文件名称不能包含特殊符号"); if (StringUtils.isEmpty(fileName))fileName = new Date().toString(); String sheetName = fileName; // 使用swagger 会有问题,请直接用浏览器或者用postman try { response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); //防止中文乱码 fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); // 这里需要设置不关闭流 EasyExcel.write(response.getOutputStream(), clazz).autoCloseStream(Boolean.FALSE).sheet(sheetName) .doWrite(list); } catch (Exception e) { throw new baseException("文件下载失败"); } } com.alibaba easyexcel2.2.6
导入
public staticList easyReadExcel(Class cls,MultipartFile file) { List list = new ArrayList<>(); try { InputStream inputStream = file.getInputStream(); list = EasyExcel.read(inputStream) .head(cls) // 设置sheet,默认读取第一个 .sheet() // 设置标题所在行数 .headRowNumber(2) .doReadSync(); }catch (Exception e) { e.printStackTrace(); } return list; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)