赞
踩
- <!-- hutool-all -->
- <dependency>
- <groupId>cn.hutool</groupId>
- <artifactId>hutool-all</artifactId>
- <version>5.8.23</version>
- </dependency>
目标将excel文件的数据进行去重并且进行分组汇总,以下是代码编写的步骤
- //保存导入的数据
- List<List<Object>> readAll;
- //根据路径读取excel表格数据
- ExcelReader reader = ExcelUtil.getReader(FileUtil.file("D:/桌面/day2表名汇总.xlsx"));
- //读取表格数据
- readAll = reader.read();
- //遍历表格数据
- HashMap<String,List<String>> map=new HashMap<>();
- for(List<Object> row : readAll) {
- //获取表格数据第一列
- Object col1= row.get(0).toString().toLowerCase();
- //获取表格数据第二列
- Object col2 = row.get(1).toString().toLowerCase();
- //进行数据去重
- if(map.containsKey(col1)){
- List<String> list = map.get(col1);
- if(!list.contains(col2)){
- list.add(col2.toString());
- }
- }else{
- List<String> list = new ArrayList<>();
- list.add(col2.toString());
- map.put(col1.toString(),list);
- }
- }
- //控制台打印数据
- for(String key:map.keySet()){
- System.out.println(key+" "+map.get(key));
- }
- //设置要导出的路径
- ExcelWriter writer = ExcelUtil.getWriter("D:/桌面/day2表名汇总java导出.xlsx");
- //开始设置
- List<List<String>> lists=new ArrayList<>();
- //根据每行去导出数据
- for(Map.Entry<String,List<String>> entry: map.entrySet()){
- String key = entry.getKey();
- List<String> value = entry.getValue();
- for (String col : value) {
- ArrayList<String> rows = CollUtil.newArrayList(key,col);
- lists.add(rows);
- }
- }
- //开始导出
- writer.write(lists,true);
- //关闭io
- writer.close();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。