版权声明:原创文章欢迎转载,转载请备注来源:https://blog.csdn.net/weixin_41986096 欢迎关注微信公众号摘星族 https://blog.csdn.net/weixin_41986096/article/details/82737130
POI实现Excel导入数据库数据
首先看一下我们导入的数据Excel中的数据格式是什么
上面是我们的一个数据导入时的模板信息,我们需要按照这样过的格式导入数据库数据。
针对这样的数据格式可以通过ReadExcelUtils的工具类去读取到
ReadExcelUtils readExcelUtils = new ReadExcelUtils(file.getInputStream(),fileName);
通过上述代码我们可以获取到ReadExcelUtils对象,再调用其readExcelList()方法即可
List<Map<Integer,Object>> datas =readExcelUtils.readExcelList();
得到List封装的数据就容易多了,看一下导入测试方法:
@RequestMapping(value = "import" , method = RequestMethod.POST)
public Result import(@RequestParam(value = "file", required = false) MultipartFile file){
try{
if(file==null){
return new Result(ResultConstant.FAILED,"文件找不到");
}
String fileName =file.getOriginalFilename();
ReadExcelUtils readExcelUtils = new ReadExcelUtils(file.getInputStream(),fileName);
List<Map<Integer,Object>> datas =readExcelUtils.readExcelList();
for(Map<Integer,Object> mapData : datas){
Business business = new Business();
business.setInfoName(String.valueOf(mapData.get(0)));
business.setChildType(String.valueOf(mapData.get(1)));
business.setAddress(String.valueOf(mapData.get(2)));
business.setInfoContent(String.valueOf(mapData.get(3)));
business.setCreateBy("测试人");
businessService.insertSelective(business);
}
}catch (Exception e){
return new Result(ResultConstant.FAILED,"failed");
}
return new Result(ResultConstant.SUCCESS,"ok");
}
以上就是导入的全部内容了,写的比较糙。有不足的地方 还留言斧正