导入--多页签

1.前端上传excel文件

2.后台接收并转行为map对象

Map map = new HashMap();
map.put(0, HandbookExportExg.class);
map.put(1, HandbookExportImg.class);
map.put(2, HandbookExportBom.class);
Map returnMap = FileUtils.importExcelByMap(multipartFile, 0, 1, map);

2.解析文件并封装到Map对象中

public static Map importExcelByMap(MultipartFile file, Integer titleRows, Integer headerRows, Map map) {
    if (file == null || map == null) {
        return null;
    }

    Map returnMap = new HashMap();
    Set keySet = map.keySet();
    for (Integer sheetNum : keySet) {
        Class pojoClass = map.get(sheetNum);
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        params.setStartSheetIndex(sheetNum);
        params.setSheetNum(1);
        List list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
            returnMap.put(sheetNum, list);
        } catch (NoSuchElementException e) {
            // throw new NormalException("excel文件不能为空");
            e.printStackTrace();
        } catch (Exception e) {
            //throw new NormalException(e.getMessage());
            e.printStackTrace();
        }
    }
    return returnMap;
}

3.后台进行验证并保存到数据库中

//获得需要导入的数据
List parseExgList = map.get(0);
List parseImgList = map.get(1);
List parseBomList = map.get(2);
//批量插入
bomInputDAOService.batchSaveBomInput(waitSaveBomList);
imgInputDAOService.saveImgInput(waitSaveImgList);
exgInputDAOService.batchSaveExgInput(waitSaveExgList);

 

你可能感兴趣的:(java)