spring boot整合POI实现excel上传、解析并存贮

spring boot整合POI实现excel上传、解析并存贮

1、项目目录结构如下

2019-05-09_140126_stitch.jpg

2、spring boot pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.excel.poi</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Spring boot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring boot 测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 数据库连接 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mybaties 映射 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!-- Mysql连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--解析Excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <!--导入excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.14</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3、spring boot 上传文件的配置项

package com.excel.poi.demo.config;


import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;

/**
 * @author hushixian
 * @date 2019-05-09 9:54
 */
@Configuration
public class UploadFileConfig extends WebMvcConfigurerAdapter {

    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置文件大小限制 ,超出设置页面会抛出异常信息,
        // 这样在文件上传的地方就需要进行异常信息的处理了;
        factory.setMaxFileSize("128MB"); // KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("256MB");
        //设置文件路径
        //factory.setLocation("");
        return factory.createMultipartConfig();
    }
}

4、我们要建立一个实体类,用于接收从excel中解析出来列的数据(根据自己实际的业务功能来创建这个实体类,可自行修改)

package com.excel.poi.demo.entity;

import java.io.Serializable;

/**
 * @author hushixian
 * @date 2019-05-09 10:01
 */
public class ReqImportClient implements Serializable {

    private String id;

    private String userName;

    private String loginName;

    private String passWord;

    public ReqImportClient() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "id='" + id + '\'' +
                ", loginName='" + loginName + '\'' +
                ", userName='" + userName + '\'' +
                ", passWord='" + passWord;
    }
}

5、三个工具类,用来辅助我们的返回值和自定义异常信息

  • 1、ApiResponse类,用来封装返回值
package com.excel.poi.demo.response;


/**
 * Created by guocai.zhang on 16/5/28.
 */
public class ApiResponse {

    public static final ApiResponse SUC = new ApiResponse(ReturnCode.CODE_SUCCESS, "Success", null);
    public static final ApiResponse FAIL = new ApiResponse();

    private int status;
    private String info;
    private Object resultObject;

    public ApiResponse() {
        this.status = ReturnCode.CODE_FAIL;
    }

    public ApiResponse(int status, String info, Object resultObject) {
        this.status = status;
        this.info = info;
        this.resultObject = resultObject;
    }


    public static ApiResponse immediateOf(int status) {
        return new ApiResponse(status, "", null);
    }

    public static ApiResponse immediateOf(int status, String info) {
        return new ApiResponse(status, info, null);
    }

    public static ApiResponse failOf(Integer status, String info) {
        if (status == null) {
            status = ReturnCode.CODE_FAIL;
        }
        return new ApiResponse(status, info, null);
    }

    public static ApiResponse immediateOf(int status, String info, Object data) {
        return new ApiResponse(status, info, data);
    }

    public static ApiResponse successOf(Object data) {
        return immediateOf(200, "success", data);
    }

    public Object getResultObject() {
        return resultObject;
    }

    public void setResultObject(Object resultObject) {
        this.resultObject = resultObject;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public boolean hasError() {
        return getStatus() != ReturnCode.CODE_SUCCESS;
    }

}

  • 2、BusinessException自定义异常信息
package com.excel.poi.demo.response;

/**
 * Created by guocai.zhang on 16/5/29.
 */
public class BusinessException extends Exception {

    private int errCode;
    private String errMsg;

    public BusinessException() {
    }

    public BusinessException(int errCode, String errMsg) {
        super(errMsg);
        this.errCode = errCode;
        this.errMsg = errMsg;
    }

    public int getErrCode() {
        return errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }
}

  • 3、ReturnCode返回值编码类型
package com.excel.poi.demo.response;

public class ReturnCode {
    /**
     * 失败
     */
    public final static int CODE_FAIL = -1;
    /**
     * 成功
     */
    public final static int CODE_SUCCESS = 0;
}

6、mapper

package com.excel.poi.demo.mapper;

import com.excel.poi.demo.entity.ReqImportClient;

/**
 * @author hushixian
 * @date 2019-05-09 10:23
 */
public interface ReqImportClientMapper {

    /**
     * 添加方法
     * @param reqImportClient 实体类
     * @return int 返回值
     */
    int addReq(ReqImportClient reqImportClient);

}

7 、mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.excel.poi.demo.mapper.ReqImportClientMapper">
    <resultMap id="BaseResultMap" type="com.excel.poi.demo.entity.ReqImportClient">
        <id column="Id" property="id" jdbcType="VARCHAR"></id>
        <result column="User_Name" property="userName" jdbcType="VARCHAR"></result>
        <result column="Login_Name" property="loginName" jdbcType="VARCHAR"></result>
        <result column="Pass_Word" property="passWord" jdbcType="VARCHAR"></result>
    </resultMap>
    <insert id="addReq" parameterType="com.excel.poi.demo.entity.ReqImportClient">
        insert into  ReqImportClient (Id,User_Name,Login_Name,Pass_Word)
        values ( #{id,jdbcType=VARCHAR},#{userName,jdbcType=VARCHAR},
        #{loginName,jdbcType=VARCHAR},#{passWord,jdbcType=VARCHAR}
        )
    </insert>
</mapper>

8、service

package com.excel.poi.demo.service;

import com.excel.poi.demo.entity.ReqImportClient;
import com.excel.poi.demo.response.BusinessException;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author hushixian
 * @date 2019-05-09 10:05
 */
public interface ResolveExcelService {

    public List<ReqImportClient> resolveExcel(MultipartFile file) throws BusinessException;
}

9、serviceImpl

package com.excel.poi.demo.service.impl;

import com.excel.poi.demo.entity.ReqImportClient;
import com.excel.poi.demo.mapper.ReqImportClientMapper;
import com.excel.poi.demo.response.BusinessException;
import com.excel.poi.demo.response.ReturnCode;
import com.excel.poi.demo.service.ResolveExcelService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author hushixian
 * @date 2019-05-09 10:07
 */
@Service("resolveExcelServiceImpl")
public class ResolveExcelServiceImpl implements ResolveExcelService {

    /**
     * 打印日志
     */
   private static final Logger logger = LoggerFactory.getLogger(ResolveExcelServiceImpl.class);

    /**
     * 上传文件后缀的地址
     */
   private static final String SUFFIX_2003 = ".xls";
   private static final String SUFFIX_2007 = ".xlsx";
    /**
     * 电话的正则
     */
    public static final String PHONE_NUMBER_REG = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[01356789]|18[0-9]|19[89])\\d{8}$";

    /**
     * 密码长度
     */
    public static final int passWardLength = 6;

    @Autowired
    private ReqImportClientMapper mapper;

    @Override
    public List<ReqImportClient> resolveExcel(MultipartFile file) throws BusinessException {

        List<ReqImportClient> list = new ArrayList<>();
        if(file==null){
            throw  new BusinessException(ReturnCode.CODE_FAIL,"对象不能为空");
        }
        // 获取文件的名字
        String originalFilename = file.getOriginalFilename();
        Workbook workbook = null;
        try {
            if (originalFilename.endsWith(SUFFIX_2003)) {
                workbook = new HSSFWorkbook(file.getInputStream());
            } else if (originalFilename.endsWith(SUFFIX_2007)) {
                workbook = new XSSFWorkbook(file.getInputStream());
            }
        } catch (Exception e) {
            logger.info(originalFilename);
            e.printStackTrace();
            throw new BusinessException(ReturnCode.CODE_FAIL, "格式错误");
        }
        if(workbook==null){
            logger.info(originalFilename);
            throw new BusinessException(ReturnCode.CODE_FAIL, "格式错误");
        }else{
            //获取所有的工作表的的数量
            int numOfSheet = workbook.getNumberOfSheets();
            //遍历这个这些表
            for (int i = 0; i < numOfSheet ; i++) {
                //获取一个sheet也就是一个工作簿
                Sheet sheet = workbook.getSheetAt(i);
                int lastRowNum = sheet.getLastRowNum();
                // 从第一行开始 第一行一般是标题
                for (int j = 1; j <= lastRowNum; j++) {
                    Row row = sheet.getRow(j);
                    ReqImportClient reqImportClient = new ReqImportClient();
                    // 获取第一行id的值
                    if(row.getCell(0) !=null){
                        row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                        String id = row.getCell(0).getStringCellValue();
                        reqImportClient.setId(id);
                    }
                    // 姓名
                    if(row.getCell(1) !=null){
                        row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                        String userName = row.getCell(1).getStringCellValue();
                        reqImportClient.setUserName(userName);
                    }
                    // 手机号
                    if (row.getCell(2) !=null){
                        row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                        String loginName = row.getCell(2).getStringCellValue();
                        // todo 正则对比
                        boolean matche = Pattern.matches(PHONE_NUMBER_REG,loginName);
                        if(!matche){
                            throw new BusinessException(ReturnCode.CODE_FAIL, "电话格式错误");
                        }
                        reqImportClient.setLoginName(loginName);
                    }
                    // 密码
                    if(row.getCell(3) !=null){
                        row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                        String passWord = row.getCell(3).getStringCellValue();
                        if (passWord.replace("", "").length() < passWardLength) {
                            //校验密码长度
                            throw new BusinessException(ReturnCode.CODE_FAIL, "密码的格式有误");
                        }
                        reqImportClient.setPassWord(passWord);
                    }
                    // 添加方法
                    mapper.addReq(reqImportClient);
                    list.add(reqImportClient);
                }
            }
        }
        return list;
    }
}

10、controller

package com.excel.poi.demo.controller;

import com.excel.poi.demo.response.ApiResponse;
import com.excel.poi.demo.response.BusinessException;
import com.excel.poi.demo.service.ResolveExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author hushixian
 * @date 2019-05-09 11:17
 */
@RestController
@RequestMapping("/resolve")
public class ResolveExcelController {

    @Autowired
    private ResolveExcelService resolveExcelService;

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public ApiResponse uploadExcel(@RequestParam("file") MultipartFile file){
        Object result;
        try {
            result = resolveExcelService.resolveExcel(file);
        }catch (BusinessException e){
            e.printStackTrace();
            return ApiResponse.failOf(-1, e.getErrMsg());
        }
        return ApiResponse.successOf(result);
    }

}

11、 yml文件的配置

server:
  port: 9008
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.mysql.cj.jdbc.MysqlConnectionPoolDataSource

mybatis:
    mapper-locations: classpath:mappers/*.xml
    # 虽然可以配置这项来进行pojo包扫描,但其实我更倾向于在mapper.xml写全类名
#    type-aliases-package: com.spring.shiro.demo.entity

12、spring boot 启动类

package com.excel.poi.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.excel.poi.demo.mapper")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

简单的html页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传示例</title>
</head>
<body>
    <h2>文件上传示例</h2>
    <hr/>
    <form method="post" enctype="multipart/form-data" action="/resolve/upload">
        <p>
            文件:<input type="file" name="file" />
        </p>
        <p>
            <input type="submit" value="上传" />
        </p>
    </form>
</body>
</html>
希望对大家有所帮助,谢谢大家的观看
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342