java入门019~springboot批量导入excel数据到mysql

我们在前面的章节已经讲了如何用jpa或者mybatis来操作mysql数据库。这一节我们就来结合具体案例,来讲解下excel表格的上传,与excel表里数据的识别。并把识别后的数据批量导入到mysql数据库

所用知识点

  • springboot 2.1.9
  • excel文件上传
  • excel数据批量识别
  • excel数据上传到mysql数据库
  • jpa的使用

jpa的使用我们在上一节已经给大家讲过了,不知道如何创建的亲,记得去翻看上一节的文章:《java入门018~springboot2使用JPA操作mysql数据库》

一,创建一个springboot项目

1,使用idea创建springboot项目


java入门019~springboot批量导入excel数据到mysql_第1张图片

java入门019~springboot批量导入excel数据到mysql_第2张图片

java入门019~springboot批量导入excel数据到mysql_第3张图片

点击finish即可


java入门019~springboot批量导入excel数据到mysql_第4张图片

二,引入识别excel的poi 和poi-ooxml类库

java入门019~springboot批量导入excel数据到mysql_第5张图片

完整的pom.xml贴出来给大家



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.9.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.apache.poi
            poi
            3.16
        

        
            org.apache.poi
            poi-ooxml
            3.16
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

记得重新Reimport


java入门019~springboot批量导入excel数据到mysql_第6张图片

三,创建一个controller用于接收上传的excel文件

java入门019~springboot批量导入excel数据到mysql_第7张图片

完整代码如下

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 2019-10-07 18:35
 * author: 编程小石头
 * wechat:2501902696
 * desc: 把excel里的数据保存到mysql数据库里
 */
@Controller
public class ExcelController {

    @GetMapping("/index")
    public String index() {
        return "index";
    }


    @RequestMapping("/uploadExcel")
    @ResponseBody
    public String uploadExcel(@RequestParam("file") MultipartFile file,
                              Map map) {
        String name = file.getOriginalFilename();
        if (name.length() < 6 || !name.substring(name.length() - 5).equals(".xlsx")) {
            return "文件格式错误";
        }
        List list = null;
        try {
            list = ExcelUtils.excelToShopIdList(file.getInputStream());
            if (list == null || list.size() <= 0) {
                return "导入的数据为空";
            }
            //excel的数据保存到数据库
            try {
                for (ExcelBean excel : list) {
                    System.out.println(excel.toString());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return e.getMessage();
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            return e.getMessage();
        }
        return "保存成功";
    }
}

简单讲解下上面代码的步骤

  • 1,获取用户上传的excel文件
  • 2,获取file流
  • 3,把excel文件流传入ExcelUtils.excelToShopIdList来识别excel里的数据
  • ExcelUtils很重要,是我们识别excel的重要步骤

四,ExcelUtils类如下

package com.example.demo;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/*
 * 操作excel
 * */
public class ExcelUtils {

    public static List excelToShopIdList(InputStream inputStream) {
        List list = new ArrayList<>();
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
            //工作表对象
            Sheet sheet = workbook.getSheetAt(0);
            //总行数
            int rowLength = sheet.getLastRowNum();
            //            System.out.println("总行数有多少行" + rowLength);
            //工作表的列
            Row row = sheet.getRow(0);

            //总列数
            int colLength = row.getLastCellNum();
            //            System.out.println("总列数有多少列" + colLength);
            //得到指定的单元格
            Cell cell = row.getCell(0);
            for (int i = 1; i <= rowLength; i++) {
                ExcelBean jiFenExcel = new ExcelBean();
                row = sheet.getRow(i);
                for (int j = 0; j < colLength; j++) {
                    //列: 0姓名    1人员编号   2餐补 3部门
                    cell = row.getCell(j);
                    //                    System.out.print(cell + ",");
                    if (cell != null) {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String data = cell.getStringCellValue();
                        data = data.trim();
                        //                        System.out.print(data);
                        //                        if (StringUtils.isNumeric(data)) {
                        if (j == 0) {
                            jiFenExcel.setName(data);
                        } else if (j == 1) {
                            jiFenExcel.setJobNum(data);
                        } else if (j == 2) {
                            jiFenExcel.setCanBu(Integer.parseInt(data));
                        } else if (j == 3) {
                            jiFenExcel.setBumen(data);
                        }
                        //                        }
                    }
                }
                list.add(jiFenExcel);
                //                System.out.println("====");
            }
        } catch (Exception e) {
        }
        return list;
    }
}

五,定义一个用于上传excel文件的html静态网页

我们的index.html位于resources下的static里


java入门019~springboot批量导入excel数据到mysql_第8张图片

代码如下




    
    上传excel


上传excel文件并存入到mysql数据库

文件上传

六,编写配置文件

java入门019~springboot批量导入excel数据到mysql_第9张图片

这一步是让springboot可以直接访问我们上面第五步定义的静态html网页。

七,运行项目

java入门019~springboot批量导入excel数据到mysql_第10张图片

运行起来后,我们通过index.html网页,来上传我们桌面的excel文件。


java入门019~springboot批量导入excel数据到mysql_第11张图片

八,识别excel表格内容。

我们excel表格内容如下


java入门019~springboot批量导入excel数据到mysql_第12张图片

我们通过上面第七步,上传excel到服务器后,识别出来的数据如下


java入门019~springboot批量导入excel数据到mysql_第13张图片

通过上图可以看出,我们成功的识别出了excel里的数据。

既然数据已经识别出来了,接下来就是通过一个for循环,把我们识别出来的5行数据,批量的存到数据里就可以了。

今天就先到这里,下一节来讲如何把这些数据存到mysql数据库里。

往期回顾

  • java入门001--IntelliJ IDEA 配置阿里云Maven国内仓库(含idea下载及破解教程)

  • java入门002~jdk8 window版32位 64位 Mac版64位安装包

  • java入门003~手把手教你开发自己的第一个java项目(基于springboot2.1.5)

  • java入门004~五分钟教你用java开发一个小程序后台服务器~看完你也会

  • java入门005~springboot实现单个文件上传(图片 文档 视频 音频都可以上传)

  • java入门006~springboot实现多文件的上传(java多文件的上传)

  • java入门013~java异常的捕获~springboot通过@ControllerAdvice和@ExceptionHandler来捕获异常

  • java入门014~springboot自定义错误页面 并重定向到首页

  • java入门015~springboot2整合mybatis,轻松实现mysql数据的增删改查

  • java入门016~springboot2结合mybatis,免xml配置

  • java入门017~springboot创建多Moudle多模块的java项目

你可能感兴趣的:(java入门019~springboot批量导入excel数据到mysql)