1、pom导入依赖文件

        <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

2、采用Multipart接收,设置接收大小

spring.http.multipart.maxFileSize=10Mb
spring.http.multipart.maxRequestSize=10Mb

3、controller层,用@RequestParam接收

@RequestMapping(value = "/resultImport",method = RequestMethod.POST)
@ResponseBody
public ResultBean importResult(@RequestParam("file") MultipartFile file) throws Exception{
ResultBean resultBean =new ResultBean();
String fileName = file.getOriginalFilename();
if(askService.batchImport(fileName,file) ==0){
resultBean.setMsg("上传文件格式不正确");
resultBean.setCode(1);
}else{
resultBean.setMsg("导入成功");
}
return resultBean;
}

4、service层

//客户批量导入
public Integer batchImport(String fileName, MultipartFile file) throws Exception{
boolean notNull = false;
Integer status = 1;
List<ResultInfo> resultList = new ArrayList<>(); if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
String error = "上传文件格式不正确";
status = 0;
return status;
}
boolean isExcel2003 = true;
if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
isExcel2003 = false;
}
InputStream is = file.getInputStream();
Workbook wb = null;
if (isExcel2003) {
wb = new HSSFWorkbook(is);
} else {
wb = new XSSFWorkbook(is);
}
Sheet sheet = wb.getSheetAt(0);
if(sheet!=null){
notNull = true;
}
System.out.println(sheet.getLastRowNum());
for (int r = 1; r < sheet.getLastRowNum()-1; r++) {
Row row = sheet.getRow(r);
if (row == null){
continue;
}
ResultInfo resultInfo = new ResultInfo();
AskUserInfo askUserInfo = new AskUserInfo(); row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);//设置读取转String类型
row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
row.getCell(7).setCellType(Cell.CELL_TYPE_STRING); String testId = row.getCell(1).getStringCellValue();
String name = row.getCell(3).getStringCellValue();
String record = row.getCell(4).getStringCellValue();
String sex = row.getCell(5).getStringCellValue();
String age = row.getCell(6).getStringCellValue(); String idCard = row.getCell(7).getStringCellValue(); if(testId ==null || name ==null || sex==null || age==null){
continue;
} askUserInfo.setName(name);
askUserInfo.setRecord(record);
if(sex.equals("1")){
askUserInfo.setSex(1);
}else{
askUserInfo.setSex(0);
}
askUserInfo.setIdcard(idCard);
askUserInfo.setAge(Integer.parseInt(age));
resultInfo.setTestId(testId);
resultInfo.setCreateTime(new Date()); System.out.println(r + name); AskUserInfo askUserInfo1 =askUserInfoRepository.save(askUserInfo);
resultInfo.setAskUserInfo(askUserInfo1);
resultInfoRepository.save(resultInfo); } return status;
}

5、前端js提交

impData:function(){
var vm = this;
var inputDOM = this.$refs.inputer;
var formdata = new FormData();
formdata.append('file',inputDOM.files[0]);
vm.$http.post('/admin/resultImport',formdata).then(function (res) {
var data = res.data;
if(data.code==0) {
alert("导入成功");
window.history.back(-1);
}
console.log(data);
})
}

6、前端html

<input type="file"  placeholder="请选择文件" name="file" ref="inputer">

7、测试

springboot-文件上传xls及POI操作Excel的更多相关文章

  1. 补习系列(11)-springboot 文件上传原理

    目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...

  2. 【SpringBoot】07.SpringBoot文件上传

    SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...

  3. SpringBoot文件上传与POI的使用

    1.使用springboot上传文件 本文所要源码在一个项目中,源码:https://github.com/zhongyushi-git/springboot-upload-download.git. ...

  4. springboot文件上传下载简单使用

    springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...

  5. SpringBoot 文件上传临时文件路径问题

    年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...

  6. SpringBoot文件上传(MVC情况和webFlux情况)

    MVC情况 引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

  7. springboot 文件上传大小配置

    转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...

  8. Jmeter之模拟文件上传、下载接口操作

    上周群里有位同学,问我用jmeter怎么上传文件?因好久没用jmeter了,顺便自己也复习下,现整理出来和大家分享 一.准备工作: 上传接口一个(自行开发解决了) 下载接口 ps:没有困难创造困难也要 ...

  9. SpringBoot文件上传下载

    项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...

随机推荐

  1. pytest生成测试报告-4种方法

    1.生成resultlog文件 2.生成JunitXML文件 3.生成html测试报告 > pip install pytest-html     # 通过pip安装pytest-html 4. ...

  2. nginx正向代理和反正代理区别

    1)正向代理:客户端 <一> 代理 一>服务端 客户端访问不到服务端,所以找了代理,由代理帮忙访问到了服务端 2)反向代理:客户端 一>代理 <一> 服务端 客户端 ...

  3. Win7删除右键菜单中“图形属性”和“图形选项”

    完win7操作系统后,打完驱动在桌面右键会出现如下两个选项,平时没啥用又占用空间,那么如何删掉这两个选项呢? 操作步骤: 1.在运行中输入 regedit 确定打开注册表: 2.依次单击展开HKEY_ ...

  4. [转帖]5G网速那么快,基站辐射会很大吗?

    5G网速那么快,基站辐射会很大吗? 鲜枣课堂 2019-04-20 21:19收藏55评论6社交通讯     题图来自东方IC,本文来自微信公众号:鲜枣课堂(ID:xzclasscom),作者:小枣君 ...

  5. 360大牛:全面解读PHP面试

    让大家了解基本面试流程和面试的核心要求以及意义是什么并理解PHP面试考点主要以基础为核心,说明PHP面试考察范围. 有需要联系:QQ:1844912514

  6. [CF 1043F] Make It One

    Description 给定 \(n\) 个正整数 \(a_i\),最少选出多少个 \(a_i\) 使得他们 \(gcd\) 为 \(1\)?\(n,a_i\le 3\times 10^5\). So ...

  7. SWIG 扩展Opencv python调用C++

    osx:10.12 g++ 7.1 swig 3.0.12 opencv 3.2.0 SWIG是Simplified Wrapper and Interface Generator的缩写.是Pytho ...

  8. MT【324】增量代换

    实数$a,b,c$满足$a^2+b^2+c^2=1$求$f=\min\{(a-b)^2,(b-c)^2,(c-a)^2\}$的最大值 分析:由对称性不妨设$c\ge b\ge a$,令$b-a=s,c ...

  9. webp图片实践之路(转载)

    最近,我们在项目中实践了webp图片,并且抽离出了工具模块,整合到了项目的基础模板中.传闻IOS10也将要支持webp,那么使用webp带来的性能提升将更加明显.估计在不久的将来,webp会成为标配. ...

  10. 【洛谷P2756】飞行员配对方案问题

    题目大意:二分图匹配裸题. 题解:用网络流进行处理. 找配对方案的时候,采用遍历二分图左边的每个节点,找到不与源点相连,且正向边权值为 0,反向边权值为 1 的边,输出即可. 代码如下 #includ ...