首页 > 其他分享 >springboot~写一个从excel读取json到List<Map>的方法

springboot~写一个从excel读取json到List<Map>的方法

时间:2022-10-30 18:00:54浏览次数:65  
标签:map springboot mapper excel List cell columnName put new

excel读出来的json,它是一个字符串,不是标准json,所以需要对字符串字段进行解析

  • 复杂的excel如图
  • 通过poi解析json,通过jackson完成对字段的解析
public static List<Map<String, Object>> read(String filePath) throws IOException {
File file = new File(filePath); //creating a new file instance
FileInputStream fis = new FileInputStream(file); //obtaining bytes from the file
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0); //creating a Sheet object to retrieve object
Iterator<Row> itr = sheet.iterator(); //iterating over excel file
Row head = sheet.getRow(0);
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> mapList = new ArrayList<>();
itr.next();//跳过第一行
while (itr.hasNext()) {
Row row = itr.next();

Iterator<Cell> cellIterator = row.cellIterator(); //iterating over each column
Map<String, Object> map = new HashMap<>();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String columnName = head.getCell(cell.getColumnIndex()).toString();
switch (cell.getCellType()) {
case STRING: //field that represents string cell type
try {
JsonNode node = mapper.readTree(cell.getStringCellValue());
if (node.isArray()) {
ArrayNode arrayNode = (ArrayNode) node;
try {
List<Map> innerList = new ArrayList<>();
for (JsonNode jsonNode : arrayNode) {
innerList.add(mapper.convertValue(jsonNode, Map.class));
}
map.put(columnName, innerList);
} catch (Exception ex) {
//简单类型的数组
map.put(columnName, mapper.readValue(cell.getStringCellValue(), new TypeReference<List<String>>() {
}));
}

} else {
map.put(columnName, mapper.convertValue(node, Map.class));
}
} catch (JsonParseException ex) {
map.put(columnName, cell.getStringCellValue());
}
break;
case NUMERIC:
map.put(columnName, cell.getStringCellValue());
break;
default:
}
}
mapList.add(map);
}

return mapList;
}
  • 解析后的List如下

    这种对象,在java中就可以直接当对象使用了




作者:仓储大叔,张占岭,

标签:map,springboot,mapper,excel,List,cell,columnName,put,new
From: https://blog.51cto.com/u_15765017/5807594

相关文章

  • wpf利用ReoGrid控件进行Excel表格展示及导出
    Nuget包安装Nuget包管理器直接搜索ReoGrid进行安装,如图   前端根据官方文档(https://reogrid.net/document/installation/)在前端引入命名空间:xmlns:rg="clr-na......
  • 十四,SpringBoot-整合Quartz
     Quartz是一个任务调度框架,在以前我们使用它的时候都是xml配置的方式,在spingboot中只需要几个注解就可以轻松搞定。主要用到以下几个注解:1.@Scheduled可以理解成触发器2.......
  • SpringBoot 阶段测试 1
    SpringBoot阶段测试1目录SpringBoot阶段测试11、使用JDK8新语法完成下列集合练习:1.1List中有1,2,3,4,5,6,7,8,9几个元素要求;(1)将奇、偶数分别汇聚成一个List(2)分别求......
  • LinkedList实现类
    packagecom.msb.test03;importjava.util.Iterator;importjava.util.LinkedList;/***@author:liu*日期:16:29:54*描述:IntelliJIDEA*版本:1.0*/......
  • C++ DoubleLinkedList
    C++DoubleLinkedListForthisassignment,createaclassthatrepresentsadoublelinkedlist(forwardandbackwardnavigation),calledDoubleLinkedList,ofin......
  • springboot源码剖析(四) 上下文启动
    概念      springboot在启动流程中最重要的事情便是加载启动spring组件,比如加载IOC容器,启动springMVC等。实现原理  使用AnnotationConfig......
  • springboot源码剖析(三) 环境配置
    概念      springboot在启动流程中会把环境配置都加载进应用当中。实现原理     使用环境配置器用来加载和解析所有配置文件。配置文件......
  • springboot2
    学习文档:https://blog.csdn.net/ttxbgjj/article/details/122881011---尚硅谷课程笔记https://www.yuque.com/atguigu/springboot  ---尚硅谷官网文档学习视频:https:......
  • springboot源码剖析(二) 事件发布
    概念      springboot在启动流程中会发布一些事件通知依赖组件进行主动更新。      原理是springboot使用到的一种设计模式:观察者模式。优......
  • go excelize 批量写入数据到Excel
    funcCreateXlS(data[][]string,fileNamestring,headerNameArray[]string){f:=excelize.NewFile()sheetName:="sheet1"sheetWords:=[]strin......