EasyPoi导出导入 一.引入EasyPoi依赖二.导出示例(合并单元格)1.导出模板2.实体类加注释3.编写逻辑 二.导入示例 一.引入EasyPoi依赖 cn
<dependency> <groupId>cn.afterturngroupId> <artifactId>easypoi-baseartifactId> <version>3.2.0version> dependency> <dependency> <groupId>cn.afterturngroupId> <artifactId>easypoi-WEBartifactId> <version>3.2.0version> dependency> <dependency> <groupId>cn.afterturngroupId> <artifactId>easypoi-annotationartifactId> <version>3.2.0version> dependency>
@excel(name = "真爱粉", width = 15)private String heizi;@Excel(name = "唱", width = 15)private String chang;@Excel(name = "跳", width = 15)private String tiao;@Excel(name = "rap", width = 15)private String rap;@Excel(name = "篮球", width = 15)private String qiu;
public void exportXls(KunKun ikun, httpservletResponse response) { //查询导出的信息列表 List<KunKun> ikuns = kunKunMapper.allList(ikun); //存放excel的表头 ExcelExportEntity是EasyPoi提供的excel属性实体类 List<ExcelExportEntity> entityList = new ArrayList<ExcelExportEntity>(); //表头 ExcelExportEntity title = new ExcelExportEntity("序号","num"); entityList.add(title); //真爱粉对应key,heizi对应的是表头也是你实体类的字段 title = new ExcelExportEntity("真爱粉","heizi"); entityList.add(title); //合并坤坤单元格 ExcelExportEntity groupOne = new ExcelExportEntity("坤坤", "kunkun"); List<ExcelExportEntity> exportEntitieOne = new ArrayList<>(); exportEntitieOne.add(new ExcelExportEntity("唱","chang")); exportEntitieOne.add(new ExcelExportEntity("跳","tiao")); exportEntitieOne.add(new ExcelExportEntity("rap","rap")); exportEntitieOne.add(new ExcelExportEntity("篮球","qiu")); groupOne.setList(exportEntitieOne); entityList.add(groupOne); //存放全部的数据 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); //定义序号 int num = 1; for (KunKun ikun: ikuns) { //存放坤坤表头下的数据 List<Map<String, Object>> listOne = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<>(); Map<String, Object> mapOne = new HashMap<>(); map.put("num",num++); map.put("heizi",ikun.getHeizi()); mapOne.put("chang",ikun.getChang()); mapOne.put("tiao",ikun.getTiao()); mapOne.put("rap",ikun.getRap()); mapOne.put("qiu",ikun.getQiu()); listOne.add(mapOne); map.put("kunkun",listOne); list.add(map); } //获取当前时间 String date = LocalDate.now().toString(); String fileName = date + "坤坤记录导出"; //ExcelExportUtil是EasyPoi提供的导出工具类。 //参数对应:导出文件名字、表头、对应的数据 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(fileName, "sheet1"), entityList, list); try { workbook.write(response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); }finally { try { workbook.close(); response.getOutputStream().close(); } catch (IOException e) { e.printStackTrace(); } } }
public Result<?> importExcel(MultipartFile file) { String extNane = FileUtil.getSuffix(file.getOriginalFilename()); if (!"xls,xlsx".contains(extNane)){ return Result.error("请选择Excel文件!"); } ExcelReader reader = null; try { reader = ExcelUtil.getReader(file.getInputStream()); } catch (IOException e) { e.printStackTrace(); } //从第2行开始读取数据(0开始计数) List<List<Object>> readAll = reader.read(2); if (CollectionUtils.isEmpty(readAll)){ return Result.error("数据为空,请正确填写数据后再上传!"); } //存放导入的数据 List<KunKun> insertList = new ArrayList<>(); try{ readAll.forEach(e ->{ KunKun ikun = new KunKun(); //从1开始取,因为0列是序号列 //真爱粉 fuelSupplyStore.setHeizi(ObjectUtil.isNull(e.get(1)) ? null : ObjectUtil.toString(e.get(1)).trim()); //唱 fuelSupplyStore.setChang(ObjectUtil.isNull(e.get(2)) ? null : ObjectUtil.toString(e.get(2)).trim()); //跳 fuelSupplyStore.setTiao(ObjectUtil.isNull(e.get(3)) ? null : ObjectUtil.toString(e.get(3)).trim()); //rap fuelSupplyStore.setRap(ObjectUtil.isNull(e.get(4)) ? null : ObjectUtil.toString(e.get(4)).trim()); //篮球 fuelSupplyStore.setQiu(ObjectUtil.isNull(e.get(5)) ? null : ObjectUtil.toString(e.get(5)).trim()); }catch (Exception ex){ return Result.error("导入失败"); } //批量新增 kunKunMapper.saveBatch(insertList); return Result.OK("导入成功"); }
来源地址:https://blog.csdn.net/twotwo22222/article/details/131049342
--结束END--
本文标题: 【java】EasyPoi导出导入(合并单元格)
本文链接: https://lsjlt.com/news/406840.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0