通过Excel批量导入数据-Java代码

时间:2021-07-07 15:14:58   收藏:0   阅读:22

1. 依赖

        <!-- excel导出 start-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>
        <!-- excel导出 end-->

2. 工具类

/**
     * 将 Excel内容转为List
     *
     * @param file 上传的Excel文件
     * @param name Excel表格中第一个sheet的名字
     * @return 对应的map集合
     **/
    public static List<Map<String, Object>> excelToList(MultipartFile file, String name) throws Exception {
        Workbook workbook = WorkbookFactory.create(file.getInputStream());
        Sheet sheet = workbook.getSheet(name);
        //行数
        int num = sheet.getLastRowNum();
        //列数
        int col = sheet.getRow(0).getLastCellNum();
        List<Map<String, Object>> list = new ArrayList<>();
        String[] colName = new String[col];
        //获取列名
        Row row = sheet.getRow(0);
        for (int i = 0; i < col; i++) {
            String[] s = row.getCell(i).getStringCellValue().split("-");
            colName[i] = s[0];
        }

        //将一行中每列数据放入一个map中,然后把map放入list
        for (int i = 1; i <= num; i++) {
            Map<String, Object> map = new HashMap<>();
            Row row1 = sheet.getRow(i);
            if (row1 != null) {
                for (int j = 0; j < col; j++) {
                    Cell cell = row1.getCell(j);
                    if (cell != null) {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        map.put(colName[j], cell.getStringCellValue());
                    }
                }
            }
            list.add(map);
        }
        return list;
    }

3. 业务代码

public void importExcel(MultipartFile file) {
        try {
            List<Map<String, Object>> importList = ExcelUtil.excelToList(file, "Sheet1");
            List<User> list = getUserList(importList);
            int flag = 0;
            List<User> userList = new ArrayList<>(16);
            for (User u : list) {
                userList.add(h);
                flag++;
                if (0 == flag % 500 || flag == list.size()) {
                    UserDao.addBatchUser(userList);
                    userList.clear();
                    log.info("执行前 {} 条成功", flag);
                }
            }
        } catch (Exception e) {
            log.info("导入Excel失败");
            e.printStackTrace();
        }
    }
    private List<User> getUserList(List<Map<String, Object>> importList) {
List<User> list = new ArrayList<>(16);
importList.forEach(e -> {
User user = new User();
if (Objects.nonNull(e.get("姓名"))) {
user.setUsername(e.get("姓名").toString());
}
if (Objects.nonNull(e.get("年龄"))) {
user.setAge(e.get("年龄").toString());
}
if (Objects.nonNull(e.get("手机号"))) {
user.setPhone(e.get("手机号").toString());
}
list.add(user);
});
return list;
}

 

原文:https://www.cnblogs.com/cgy-home/p/14981302.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!