POI實現Excel導入數據庫數據


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");
    }

 

 

以上就是導入的全部內容了,寫的比較糙。有不足的地方 還留言斧正

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM