栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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文件(只负责一个文件上传)




    
    主页


测试导入EXCEL文件

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

FileOption类

public class FileOption {

    public static List fileOptions( MultipartFile file) {

        List 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("文件版本识别异常");
        }

        
        Sheet sheet = wb.getSheetAt(0);

        
        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);
                
                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 userList = FileOption.fileOptions(file);
        return userList.toString();
    }
}

测试结果:

 慢慢来,不急。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/913922.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号