导入--多页签

1.前端上传excel文件

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

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

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

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

    Map<Integer, List> returnMap = new HashMap<Integer, List>();
    Set<Integer> 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<T> 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<HandbookExportExg> parseExgList = map.get(0);
List<HandbookExportImg> parseImgList = map.get(1);
List<HandbookExportBom> parseBomList = map.get(2);
//批量插入
bomInputDAOService.batchSaveBomInput(waitSaveBomList);
imgInputDAOService.saveImgInput(waitSaveImgList);
exgInputDAOService.batchSaveExgInput(waitSaveExgList);
发布了68 篇原创文章 · 获赞 19 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/tyjlearning/article/details/102686642