com.alibaba easyexcel2.1.6
@ExcelProperty("商品编号") private Integer gid; @ExcelProperty("商品名称") private String gname; @ExcelProperty("商品价格") private Float gprice; @ExcelProperty("商品购买数量") private Integer buynum; @ExcelProperty("商品库存") private Integer gnum; //导出图片格式如下(照片路径一定是要真实路径,不然会报错) //@ExcelProperty(value = {"商品图片"},converter = StringImageConverter.class) //忽略导入 //@ExcelIgnore @ExcelProperty("商品图片") private String gpic;
public class DownExcel { public static void download(HttpServletResponse response, Class t, List list) throws IOException, IllegalAccessException,InstantiationException { response.setContentType("application/vnd.ms-excel");// 设置文本内省 response.setCharacterEncoding("utf-8");// 设置字符编码 response.setHeader("Content-disposition", "attachment;filename=demo.xlsx"); // 设置响应头 EasyExcel.write(response.getOutputStream(), t).sheet("模板").doWrite(list); //用io流来写入数据 } }
//导出为Excel @RequestMapping("/downloadexcel.do") public void getExcel(HttpServletResponse response) throws IllegalAccessException, IOException, InstantiationException { Listlist = sysUserService.getAll(); DownExcel.download(response,SysUser.class,list); }
int saveAll(ListsysUsers);
xml如图所示:
insert into sys_user (name,nick_name,avatar,password,salt,email,mobile,status,dept_id,create_by,create_time,last_update_by,last_update_time) values ( #{item.name}, #{item.nickName}, #{item.avatar}, #{item.password}, #{item.salt}, #{item.email}, #{item.mobile}, #{item.status}, #{item.deptId}, #{item.createBy}, #{item.createTime}, #{item.lastUpdateBy}, #{item.lastUpdateTime} )
void saveList(Listlist);
@Override public void saveList(Listlist) { sysUserMapper.saveAll(list); }
// 有个很重要的点 ExcelListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去 public class ExcelListener extends AnalysisEventListener{ private List list = new ArrayList<>(); /** * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收 */ private static final int BATCH_COUNT = 5; /** * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。 */ private SysUserService sysUserService; /** * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来 */ public ExcelListener(SysUserService sysUserService) { this.sysUserService = sysUserService; } /** * 这个每一条数据解析都会来调用 */ @Override public void invoke(SysUser goods, AnalysisContext analysisContext) { System.out.println("解析到一条数据:========================"+goods.toString()); // 数据存储到datas,供批量处理,或后续自己业务逻辑处理。 list.add(goods); // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM if(list.size() >= BATCH_COUNT){ saveData(); // 存储完成清理datas list.clear(); } } /** * 所有数据解析完成了 都会来调用 */ @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { saveData();//确保所有数据都能入库 } /** * 加上存储数据库 */ private void saveData() { System.out.println("=============================="+list.size()+"条数据,开始存储到数据库"); sysUserService.saveList(list); } }
//导入Excel @RequestMapping("/importexcel.do") @ResponseBody public String importexcel(@RequestParam(value = "excelFile") MultipartFile file) throws IOException{ EasyExcel.read(file.getInputStream(), SysUser.class, new ExcelListener(sysUserService)).sheet().doRead(); return "success"; }
资源下载:https://download.csdn.net/download/qq_37284798/87274449
百度网盘:
链接:https://pan.baidu.com/s/1pd0SfhnDvKsXfa5fQjeMFw
提取码:fpbt