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. Navicat Premium 12.1.16.0安装与激活

    声明:本文所提供的所有软件均来自于互联网,仅供个人研究和学习使用,请勿用于商业用途,下载后请于24小时内删除,请支持正版! 本文介绍Navicat Premium 12的安装.激活与基本使用.已于20 ...

  2. pyecharts使用

    安装 pyecharts 兼容 Python2 和 Python3.目前版本为 0.1.2 pip install pyecharts 入门 首先开始来绘制你的第一个图表 from pyecharts ...

  3. Ubuntu下找不到ttyUSB*问题解决

    一.硬件连接 确认Ubuntu对USB转串口设备的支持. 1.# lsmod | grep usbserial如果有usbserial,说明系统支持USB转串口. 如果没有,先安装ch340驱动或者c ...

  4. 使用栈实现队列(2)(Java)

    class MyQueue { private Stack s1; private Stack s2; public MyQueue(int size) { this.s1 = new Stack(s ...

  5. 【zabbix教程系列】五、邮件报警设置(脚本方式)

    本方式是使用外部邮箱账号发送报警邮件到指定邮箱. 好处是:此邮箱账号既能发送邮件,也能接收邮件,而且避免被当做垃圾邮件. 一.zabbix-server端安装mailx服务 [root@ltt01 ~ ...

  6. Excel vba中访问ASP.NET MVC项目,记录访问时间,文件名称

    每30秒连接一次服务器,连接成功单元格变绿色,连接失败变红色,状态单元格为17行,2列 1,打开excel文件,进入vba编辑器,新建一个modules模块,在里面先写一个每30秒执行一次ConnSe ...

  7. Kafka学习笔记-如何保证高可用

    一.术语 1.1 Broker Kafka 集群包含一个或多个服务器,服务器节点称为broker. broker存储topic的数据. 如果某topic有N个partition,集群有N个broker ...

  8. 树莓派3B+(二)

    一.安装SSH工具 这里用的是putty,下载下来是一个exe文件,点开就能用. 下载地址:https://www.chiark.greenend.org.uk/~sgtatham/putty/lat ...

  9. adb push 中文路径文件名丢失后缀

    adb 的一个BUG. 今天刷机的时候,用以下命令多次 push 安装包到手机: adb push F:\刷机\Nexus5\lineage-14.1-20170314-nightly-hammerh ...

  10. [CQOI2009] 中位数

    不错的思维题 传送门:$>here<$ 题意:给出一个N的排列,求出其中有多少个连续子段的中位数是b 数据范围:$N \leq 100000$ $Solution$ 先考虑中位数的意义:一 ...