springboot 框架学习 Excel导入数据到springboot项目

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

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

Excel导入测试:

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

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

3、设计controller层测试

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

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>
<!-- http://t.csdn.cn/G5Q5H这篇文章有这个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();
    }
}

测试结果:

 慢慢来,不急。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ForestSpringH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值