昨天在开发软工项目-宿舍管理系统,涉及到学生信息的导入与宿舍信息的导入,虽然我提前将数据表建立好并且也已经把具体的方法写好了,但是今天在写controller层的映射下方法的时候想到了一个大问题,涉及到可用性,成百上千名学生的入学,那么我不可能一个个的将他们的信息用表单提交吧?这个时候我就在想可不可以用Excel表格将它的数据导入到springboot再利用数据库进行操作?

首先导入的数据要具备可操作性的话必须要进行封装,在java里通常都是用对象封装数据,本次测试也是使用对象封装我们在Excel里得到的数据。

Excel导入测试:

1、创建springboot项目(基于测试方便,目前只需要导入web等依赖,不需要导入数据库相关的依赖)与Excel数据表

2、设计Untils层,特别是针对Excel数据的转换与封装

3、设计controller层测试

1、创建springboot项目与Excel数据表

spring MailSender发送表格 spring excel导入_学习

spring MailSender发送表格 spring excel导入_User_02

User类

@Data
public class User {
    private String username;
    private String password;
}

html文件(只负责一个文件上传)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
<h3>测试导入EXCEL文件</h3>
<!-- 这篇文章有这个enctype属性值的讲解 -->
<form action="file" enctype="multipart/form-data" method="post">
    <input type="file" name="filename">
    <input type="submit">
</form>
</body>
</html>

2、设计Untils层,特别是针对Excel数据的转换与封装

FileOption类

public class FileOption {

    public static List<User> fileOptions( MultipartFile file) {

        List<User> userList = new ArrayList<>();

        /*获取文件名*/
        String fileName = file.getOriginalFilename();
        /*获取文件后缀*/
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        /*创建文件输入流对象*/
        InputStream ins = null;
        try {
            ins = file.getInputStream();
        } catch (IOException e) {
            System.out.println("文件导入异常");
            e.printStackTrace();
        }
        /*获取工作薄对象*/
        Workbook wb = null;
        try {
            /*根据文件类型将文件输入流对象给到工作薄对象的内容里去*/
            if (suffix.equals("xlsx")) {
                wb = new XSSFWorkbook(ins);
            } else {
                wb = new HSSFWorkbook(ins);
            }
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("文件版本识别异常");
        }

        /*获取excel表单*/
        Sheet sheet = wb.getSheetAt(0);

        /* line = 2 :从表的第三行开始获取记录*/
        if (null != sheet) {
            for (int line = 2; line <= sheet.getLastRowNum(); line++) {
                /*创建对象,封装数据*/
                User user  = new User();
                /*创建行对象,便于逐行操作单元格*/
                Row row = sheet.getRow(line);
                if (null == row) {
                    continue;
                }
                /* 判断单元格类型是否为文本类型 */
                if (1 != row.getCell(0).getCellType()) {
                    System.out.println("单元格类型不是文本类型");
                }
                /*获取第一个单元格的内容并设置对象的属性*/
                String username = row.getCell(0).getStringCellValue();
                user.setUsername(username);
                /* 获取第二个单元格的内容并设置对象的属性*/
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                String password = row.getCell(1).getStringCellValue();
                user.setPassword(password);
                /*将这个对象插入List容器里*/
                userList.add(user);
            }
        }
        return userList;
    }
}

3、设计controller层测试

fileController类

@RestController
public class fileController {
    @RequestMapping("index")
    public ModelAndView index(){
        return new ModelAndView("index");
    }
    @RequestMapping(value = "file",method = RequestMethod.POST)
    @ResponseBody
    public String excelImport(@RequestParam(value = "filename")MultipartFile file, HttpSession httpSession){
        List<User> userList = FileOption.fileOptions(file);
        return userList.toString();
    }
}

测试结果:

spring MailSender发送表格 spring excel导入_List_03

spring MailSender发送表格 spring excel导入_学习_04

 慢慢来,不急。