当前位置: 首页 > news >正文

树结构导入

  @Testpublic void testExcel1() {// 写法1:JDK8+ ,不用额外写一个DemoDataListener// since: 3.0.0-beta1EasyExcelFactory.read(new File("C:\\Users\\Admin\\Desktop\\树导入.xlsx"), null, new ReadListener<Map>() {public static final int BATCH_COUNT = 100;private List<Map> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);@Overridepublic void invoke(Map data, AnalysisContext context) {cachedDataList.add(data);}@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {//List<Map>System.out.println(cachedDataList);System.out.println("+++++++++");//转换后的List<List<String>>System.out.println(mapToList(cachedDataList));
//                insertBatch(mapToList(cachedDataList));?}}).headRowNumber(0).build().readAll();}//将List<Map>集合转成List<List<String>>public static List<List<String>> mapToList(List<Map> maps) {List<List<String>> dataList = new ArrayList<>();for (Map map : maps) {List<String> list = new ArrayList<>();map.forEach((k, v) -> list.add(ObjectUtils.isEmpty(v) ? "" : v.toString()));dataList.add(list);}return dataList;}public List<CategoryVO> insertBatch(List<List<String>> datas){// 定义主键id 对应数据库中cat_idShort index = 1;Short parentId = null;String levelName = null;//分类名称// tempMap为临时变量,记录那些已经添加过了 key为CatName+层级 value 为记录对象,例如// <Home_1,CategoryVO> 表示存储第一级对象Map<String, CategoryVO> tempMap = new HashMap<String, CategoryVO>();// 记录最终需要插入数据库的数据List<CategoryVO> insertDatas = new ArrayList<CategoryVO>();CategoryVO level = null;int allCols = datas.get(0).size();//获取导入数据的列数for (int column = 0; column < allCols; column++){for (int i = 0; i < datas.size(); i++){List<String> rows = datas.get(i);//设置层级的关联关系if (column == 0){// 如果是第一列,parentid 默认为0parentId = (short) 0;}else{// 如果不是一列,则在tempMap中寻找对应的父类id作为parentid,当前列向后退一列parentId = Short.valueOf(tempMap.get(rows.get(column - 1) + "_" + String.valueOf(column)).getCatId());}levelName = rows.get(column);if (StringUtils.isEmpty(levelName)){System.out.println("+++++++++失败,表格中有空白+++++++++");}//tempMap中没有记录过则表示是要插入的数据if (!tempMap.containsKey(levelName + "_" + (column + 1))){level = new CategoryVO();level.setCatId(String.valueOf(index++));level.setCatName(levelName);level.setParentId(String.valueOf(parentId));level.setGrade(String.valueOf(Byte.valueOf(String.valueOf(column + 1))));tempMap.put(levelName + "_" + (column + 1), level);//添加到最终要导入的列表中insertDatas.add(level);}}}//批量插入System.out.println(insertDatas);return insertDatas;}
http://www.lryc.cn/news/327806.html

相关文章:

  • Promise封装ajax
  • 47 vue 常见的几种模型视图不同步的问题
  • 以太网/USB 数据采集卡 24位16通道 labview 256K同步采样
  • python基础 | 核心库:PIL
  • #Linux系统编程(共享内存)
  • 蓝桥杯备考随手记: practise01
  • 【openGL4.x手册09】转换反馈
  • 记录一次报错提示npx update-browserslist-db@latest
  • 【Go】二、Go的基本数据类型
  • 十一、Spring源码学习之registerListeners方法
  • Oracle 控制文件详解
  • 活体成像应用染料CY3.5-NHS星戈瑞
  • 【优选算法】专题1 -- 双指针 -- 复写0
  • GESP Python编程三级认证真题 2024年3月
  • 前端理论总结(css3)——link/import区别 // 伪类/伪元素
  • ntp服务器搭建
  • 对象的内存布局
  • docker centos7离线安装ElasticSearch单机版
  • 【计算机网络】IP 协议
  • 刷题DAY38 | LeetCode 509-斐波那契数 70-爬楼梯 746-使用最小花费爬楼梯
  • 蓝桥杯-卡片换位
  • Unity 布局控制器Content Size Fitter
  • Python的面向对象、封装、继承、多态相关的定义,用法,意义
  • Elasticsearch 向量搜索
  • 2024蓝桥杯每日一题(背包)
  • Redis桌面客户端
  • 让Unity的协程变得简单
  • 2.9 Python缩进规则(包含快捷键)
  • 任务记录.
  • andv vue 实现多张图片上传