spring boot读取Excel
首先引入相关依赖
<!--解析office相关文件-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!--解析office相关文件-->
工具类
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.web.multipart.MultipartFile; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map; public class OfficeUtils { protected static final Logger logger = LoggerFactory.getLogger(OfficeUtils.class); public static Map<Integer, Map<Integer, Object>> readExcelContentz(MultipartFile file) throws Exception {
Map<Integer, Map<Integer, Object>> content = new HashMap<Integer, Map<Integer, Object>>();
// 上传文件名
Workbook wb = getWb(file);
if (wb == null) {
throw new BusinessException(ErrorType.WORK_BOOK_EMPTY);
}
Sheet sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
Row row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
Map<Integer, Object> cellValue = new HashMap<Integer, Object>();
while (j < colNum) {
Object obj = getCellFormatValue(row.getCell(j));
cellValue.put(j, obj);
j++;
}
content.put(i, cellValue); }
return content;
} //根据Cell类型设置数据
private static Object getCellFormatValue(Cell cell) {
Object cellvalue = "";
if (cell != null) {
switch (cell.getCellTypeEnum()) {
case NUMERIC:
cellvalue = String.valueOf(cell.getNumericCellValue());
break;
case FORMULA: {
cellvalue = cell.getDateCellValue();
break;
}
case STRING:
cellvalue = cell.getRichStringCellValue().getString();
break;
default:
cellvalue = "";
}
} else {
cellvalue = "";
}
return cellvalue;
} private static Workbook getWb(MultipartFile mf) {
String filepath = mf.getOriginalFilename();
String ext = filepath.substring(filepath.lastIndexOf("."));
Workbook wb = null;
try {
InputStream is = mf.getInputStream();
if (".xls".equals(ext)) {
wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(ext)) {
wb = new XSSFWorkbook(is);
} else {
wb = null;
}
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException", e);
} catch (IOException e) {
logger.error("IOException", e);
}
return wb;
}
}
service层
public Map<Integer, Map<Integer,Object>> addCustomerInfo(MultipartFile file) {
Map<Integer, Map<Integer,Object>> map = new HashMap<>();
try {
map = ReadExcelUtil.readExcelContentz(file);
} catch (Exception e) {
e.printStackTrace();
}
//excel数据存在map里,map.get(0).get(0)为excel第1行第1列的值,此处可对数据进行处理
}
controller层
@PostMapping
public String add(@RequestParam("file")MultipartFile file){
Map<Integer, Map<Integer,Object>> map = customerService.addCustomerInfo(file);
return "success";
}
至此,基本完成Excel的解析。
spring boot读取Excel的更多相关文章
- Spring Boot 导出Excel表格
Spring Boot 导出Excel表格 添加支持 <!--添加导入/出表格依赖--> <dependency> <groupId>org.apache.poi& ...
- Spring boot 读取resource目录下的文件
背景:最近做项目重构将以前的ssh + angular js架构,重构为spring boot + vue.项目是一个数据管理平台,后台涉及到多表关联查询,数据导入导出等. 问题:读取resource ...
- Spring Boot 操作 Excel
Excel 在日常操作中经常使用到,Spring Boot 中使用 POI 操作 Excel 本项目源码 github 下载 1 新建 Spring Boot Maven 示例工程项目 注意:本示例是 ...
- Spring Boot读取配置的几种方式
读取application文件 在application.yml或者properties文件中添加: info.address=USAinfo.company=Springinfo.degree=hi ...
- Spring Boot读取配置的 5 种方式
读取application文件 在application.yml或者properties文件中添加: info.address=USA info.company=Spring info.degree= ...
- spring boot读取配置文件
一.springboot配置文件 核心配置文件和自定义配置文件.核心配置文件是指在resources根目录下的application.properties或application.yml配置文 ...
- spring boot 读取配置文件(application.yml)中的属性值
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注 ...
- Spring Boot读取配置文件的几种方式
Spring Boot获取文件总的来说有三种方式,分别是@Value注解,@ConfigurationProperties注解和Environment接口.这三种注解可以配合着@PropertySou ...
- Spring Mvc和Spring Boot读取Profile方式
spring boot java代码中获取spring.profiles.active - u013042707的专栏 - CSDN博客https://blog.csdn.net/u013042707 ...
随机推荐
- 3.用Python画五角星
import turtleimport timeimport os #def draw_square(org_x, org_y, x, y): turtle.setpos(org_x, org_y) ...
- JavaWeb 之 Filter:过滤器
一.Filter 概述 1.概念 web 中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能. 2.作用 一般用于完成通用的操作.如:登录验证.统一编码处理.敏感字符等功能 ...
- Java 之 Response 对象
Response 对象 一.Response 原理和继承结构 原理和继承结构参考 request. 二.Response 对象 Response 就是用来设置响应消息. 1.设置响应行 响应头格式 ...
- mac杂记
brew 安装.更新 https://blog.csdn.net/fxp850899969/article/details/53284193 vmware work 15 pro https://ww ...
- testNG 注释实例
1. 单个测试用例文件 新建TestDBConnection.java文件 import org.testng.annotations.*; public class TestDBConnection ...
- SQL常见的一些面试题(太有用啦)
SQL常见面试题 1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name kecheng fenshu张三 语文 81张三 数学 75李四 ...
- 自定义View(二),强大的Canvas
本文转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android中使用图形处理引擎,2D部分是 ...
- Docker03-安装Docker运行环境
目录 Ubuntu 18 中安装Docker 查看Docker安装信息 查看Docker版本,命令:docker version 查看Docker运行信息,命令: docker info 检查安装是否 ...
- Python并发编程-多进程
Python并发编程-多进程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.多进程相关概念 由于Python的GIL全局解释器锁存在,多线程未必是CPU密集型程序的好的选择. ...
- HTML&CSS基础-标签的属性
HTML&CSS基础-标签的属性 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!-- html 根标签,一个页面中有且只有一个根标签,网页中的 ...