首页 > 其他分享 >(13)SpringBoot使用poi上传excel文件

(13)SpringBoot使用poi上传excel文件

时间:2023-01-14 21:34:36浏览次数:54  
标签:13 return SpringBoot excel private user org import String




(13)SpringBoot使用poi上传excel文件_spring

   摘要:本文主要讲解,在SpringBoot项目中如何使用poi上传excel文件。


SpringBoot整合poi和springmvc整合poi差别不大,springmvc整合poi也可以借鉴此文;由于后面打算写springboot异步处理任务,和多线程处理任务,所以先写此篇,可以为后面准备数据场景。


目录:

  • 1.pom.xml引入依赖
  • 2.application.properties配置属性
  • 3.写上传页面
  • 4.ExcelParserUtil工具类
  • 5.ExcelController写接口
  • 6.ExcelServiceImpl写Excel处理逻辑

1.pom.xml引入依赖


<!--poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>

2.application.properties配置属性


这个属性不是必须的,如果不配置此属性,当文件大于1Mb时,会报错,因为springboot默认的上传单个文件大小为1Mb;

报错如下:

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.

配置如下,这里配置为10Mb:

# 设置上传文件大小 默认是1Mb
spring.servlet.multipart.max-file-size=10Mb
spring.servlet.multipart.max-request-size=10Mb

3.写上传页面


我们复用之前​​(8)SpringBoot整合JSP的页面​​,只是把请求接口修改为我们解析excel文件的接口。

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<div class="sm">
<form method="POST" enctype="multipart/form-data" action="/excel/parseExcel">
<p>文件:<input type="file" name="file"/></p>
<p><input type="submit" value="上传" /></p>
</form>
</div>

4.ExcelParserUtil工具类


我们写个简单的工具类,方便后面调用。

package com.java4all.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Author: momo
* Date: 2018/4/11
* Description:Excel解析工具类
*/
public class ExcelParserUtil {
private static Logger logger = LoggerFactory.getLogger("");
/**
* 是否是2003的excel,返回true 是2003
* @param filePath
* @return
*/
public static boolean isExcel2003(String filePath) {
return filePath.matches("^.+\\.(?i)(xls)$");
}
/**
* 是否是2007的excel,返回true 是2007
* @param filePath
* @return
*/
public static boolean isExcel2007(String filePath) {
return filePath.matches("^.+\\.(?i)(xlsx)$");
}
/**
* 验证是否为EXCEL文件
* @param filePath
* @return
*/
public static boolean validateExcel(String filePath){
if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))){
return false;
}
return true;
}
}

5.ExcelController写接口


写个接口,页面访问此接口上传文件。

package com.java4all.controller;
import com.java4all.service.ExcelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* Author: momo
* Date: 2018/4/11
* Description:表格处理
*/
@RestController
@RequestMapping("excel")
public class ExcelController {
private static Logger logger = LoggerFactory.getLogger("");
@Autowired
private ExcelService excelService;
@RequestMapping(value = "parseExcel",method = RequestMethod.POST)
public String parseExcel(MultipartFile file){
try {
String ss = excelService.parseExcel(file);
return ss;
}catch (Exception ex){
logger.info("文件解析出错",ex);
return "文件解析出错";
}
}
}

6.ExcelServiceImpl写Excel处理逻辑


文件上传后,excel的解析代码很简单;我这里的逻辑是:接收文件-->对文件做判断-->解析文件,将数据转为User对象列表-->对列表数据做处理:将列表中省份为陕西省的user挑选出来。

文末附上User实体,也可以参考​​(5)SpringBoot使用JPA访问数据库​​一文,excel表格读者自行创建。

此过程无特殊之处,如果只是想学习springboot如何整合poi,此步骤可以省略;筛选陕西省只是为了后面改造为异步或者多线程提供基础。

ExcelService.java
package com.java4all.service;
import org.springframework.web.multipart.MultipartFile;
/**
* Author: momo
* Date: 2018/4/11
* Description:表格处理
*/
public interface ExcelService {
/**
* 解析Excel文件
* @param file
* @return
* @throws Exception
*/
String parseExcel(MultipartFile file) throws Exception;
}
ExcelServiceImpl.java
package com.java4all.serviceImpl;
import com.java4all.entity.User;
import com.java4all.service.ExcelService;
import com.java4all.utils.ExcelParserUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Author: momo
* Date: 2018/4/11
* Description:
*/
@Service
public class ExcelServiceImpl implements ExcelService{
private static Logger logger = LoggerFactory.getLogger("");
/**
* 分析数据
* @param list
* @return
*/
public List getUserByProvince(List<User> list) {
List<User> list1 = new ArrayList();
for (int i = 0,length = list.size();i < length;i++){
User user = list.get(i);
if("陕西省".equals(user.getProvince())){
list1.add(user);
logger.info("==========>符合条件:"+user.getRealName());
}
}
return list1;
}
@Override
public String parseExcel(MultipartFile file) throws Exception{
String fileName = file.getOriginalFilename();
logger.info("====>文件名:"+fileName);
//判断是否为null
if(null == file){
return "文件为空,请重新上传";
}
//判断是否为Excel格式
if(!ExcelParserUtil.validateExcel(fileName)){
return "文件必须是Excel格式,请重新上传";
}
//判断内容是否为空
long size = file.getSize();
if(0 == size){
return "文件内容为空,请重新上传";
}
//将文件备份到服务器/本地
File fileDir = new File("E:\\fileDir");
if(!fileDir.exists()){
fileDir.mkdirs();
}
//String filePath = fileDir+"\\";
File fileNew = new File(fileDir,System.currentTimeMillis()+ "-"+ fileName);
file.transferTo(fileNew);
//创建输入流
InputStream is = new FileInputStream(fileNew);
Workbook wb = null;
//根据文件名判断文件是2003还是2007
if(ExcelParserUtil.isExcel2003(fileName)){
wb = new HSSFWorkbook(is);
}else {
wb = new XSSFWorkbook(is);
}
List<User> users = readExcel(wb);
List listPS = this.getUserByProvince(users);
logger.info("=======>处理完毕,符合条件数据有:"+listPS.size()+"条");
return "处理完毕";
}
/**
* 解析Excel表格数据
* @param wb
* @return
*/
public List<User> readExcel(Workbook wb){
//错误信息
String errorMessage = "";
//获取第一个sheet
Sheet sheet0 = wb.getSheetAt(0);
//行数
int totalRows = sheet0.getPhysicalNumberOfRows();
//列数
int totalCells = 0;
//第二行算起且不为空
if(totalRows >= 2 && null != sheet0.getRow(1)){
totalCells = sheet0.getRow(1).getPhysicalNumberOfCells();
}
//解析数据转为user对象
List<User> list = new ArrayList<>();
for(int i = 1,length = totalRows;i < totalRows;i ++){
Row row = sheet0.getRow(i);
if(null == row){
logger.info("===========>第"+i+"行数据有问题,请核查数据!");
}
User user = new User();
user.setUserName(row.getCell(1).getStringCellValue());
user.setRealName(row.getCell(2).getStringCellValue());
user.setPassword(row.getCell(3).getStringCellValue());
user.setCountry(row.getCell(4).getStringCellValue());
user.setProvince(row.getCell(5).getStringCellValue());
user.setCity(row.getCell(6).getStringCellValue());
user.setPhone(row.getCell(7).getNumericCellValue()+"");
user.setDevLanguage(row.getCell(9).getStringCellValue());
user.setSchoolName(row.getCell(10).getStringCellValue());
user.setSchoolMajor(row.getCell(11).getStringCellValue());
list.add(user);
}
return list;
}
}

附:user实体类

package com.java4all.entity;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* Author: momo
* Date: 2018/3/26
* Description:
*/
@Entity
@Table(name="user")
public class User extends PageEntity implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id",nullable = false)
private Integer id ;
@Column(name = "userName",nullable = true)
private String userName;
@Column(name = "real_name",nullable = true)
private String real_name;
@Column(name = "password",nullable = true)
private String password;
@Column(name = "country",nullable = true)
private String country;
@Column(name = "province",nullable = true)
private String province;
@Column(name = "city",nullable = true)
private String city;
@Column(name = "phone",nullable = true)
private String phone;
@Column(name = "createTime",nullable = true)
private Date createTime;
@Column(name = "devLanguage",nullable = true)
private String devLanguage;
@Column(name = "schoolName",nullable = true)
private String schoolName;
@Column(name = "schoolMajor",nullable = true)
private String schoolMajor;
}

下一篇,我们会将此处理数据的过程改为异步处理。





标签:13,return,SpringBoot,excel,private,user,org,import,String
From: https://blog.51cto.com/u_15936016/6007810

相关文章

  • (11)SpringBoot整合EhCache做缓存
      摘要:本文介绍在SpringBoot项目中,如何使用EhCache做缓存。EhCache简介:EhCache是一个纯Java的进程内缓存框架,是Hibernate中默认的CacheProvider;其缓存的数据可以存放......
  • (8)SpringBoot整合JSP
    SpringBoot官方不推荐使用JSP来开发web页面,而是推荐如Thymeleaf,FreeMarker等模板引擎来开发,但是绝大部分做java的程序员还是习惯jsp;所以,本文先讲解SpringBoot如何支持jsp开......
  • 230114_50_SpringBoot入门
    主启动类详解packagecom.bill;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@Spr......
  • SpringBoot中使用SpringSecurity
    1.maven配置<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>2.新建html页面去......
  • springboot中的一些常用的知识
    1.lomboklombok就是为了简化代码的@Data注释@Data@AllArgsConstructor@NoArgsConstructorpublicclassPerson{privateStringname;privateStringadd......
  • Redis 6 学习笔记 3 —— 用SpringBoot整合Redis的踩坑,了解事务、乐观锁、悲观锁
    SpringBoot整合Redis时踩到的坑jdk1.8环境,用idea的SpringInitializr创建springboot项目,版本我选的2.7.6。pom文件添加的依赖如下,仅供参考。注意commons-pool2选错版本......
  • DataGear 制作Excel动态数据可视化图表
    DataGear4.1.0版本增强了Excel数据集功能,新增了【工作表名称】项,并且支持填写参数化语法内容,使得可基于Excel多工作表,构建动态数据可视化图表。本文以某商品三个地区的......
  • springboot @Autowried报错
     原因:没啥,就是idea偶尔有薄毛病 解决:作用是屏蔽一些无关紧要的警告。使开发者能看到一些他们真正关心的警告。从而提高开发者的效率@SuppressWarnings("all")......
  • SpringBoot——核心原理入门
    SpringBoot概述BuildAnythingwithSpringBoot:**SpringBootisthestartingpointforbuildingallSpring-basedapplications.SpringBootisdesignedtoget......
  • SpringBoot——初始化器解析
    初始化器Spring是一个扩展性很强的容器框架,为开发者提供了丰富的扩展入口,其中一个扩展点便是ApplicationContextInitializer(应用上下文初始化器)。 ApplicationConte......