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. 突击战 (uva 11729)贪心

    思路:就是把J大的放在前面.为什么这样贪心呢? 看看这个图 #include<iostream> #include<algorithm> #include<vector& ...

  2. 免费高端出辕营,横空出世惊鬼神 Excel插件:ExcelPower_Helper 0.41初出茅庐

    免费高端出辕营,横空出世惊鬼神 Excel插件:ExcelPower_Helper 0.41初出茅庐        隐鹤 倾心开发 2019.4.1 1.   引言 经过前后大约零零散散的一年的开发, ...

  3. Object.clone()方法与对象的深浅拷贝

    转载:[https://www.cnblogs.com/nickhan/p/8569329.html] 引言 在某些场景中,我们需要获取到一个对象的拷贝用于某些处理.这时候就可以用到Java中的Obj ...

  4. DAY23、面向对象特性

    一.复习1.类: 对象属性的查找顺序:先找自身再找类 类的名称空间:直接写在类中 对象的名称空间:写在__init__方法中,通过self.属性形成名称空间中的名字 类的方法:在类中用@classme ...

  5. CF 1119C Ramesses and Corner Inversion

    https://codeforces.com/problemset/problem/1119/C 题目 给两个矩阵,只能选宽和高大于等于2的子矩阵左上.左下.右上.右下四点翻转(1->0,0-& ...

  6. Educational Codeforces Round 63 Div. 2

    A:找到两个相邻字符使后者小于前者即可. #include<bits/stdc++.h> using namespace std; #define ll long long #define ...

  7. 机器学习---感知机(Machine Learning Perceptron)

    感知机(perceptron)是一种线性分类模型,通常用于二分类问题.感知机由Rosenblatt在1957年提出,是神经网络和支持向量机的基础.通过修改损失函数,它可以发展成支持向量机:通过多层堆叠 ...

  8. Xilinx FPGA 移位寄存器IP延时问题

    软件版本:Vivado2016.1 在使用移位寄存器IP时,对于不同延时拍数的使能延时可能会有问题. (1)32深度的可变长度移位寄存器,IP生成界面如下图所示. (2)128深度的可变长度移位寄存器 ...

  9. 执行Git命令时出现各种 SSL certificate problem 的解决办法

    执行Git命令时出现各种 SSL certificate problem 的解决办法 来源  https://www.cnblogs.com/chenzc/p/5842932.html 比如我在win ...

  10. docker报错:Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

    在github上开到这样一条 于是 这两个选项换着来 具体怎么回事,咱也不知道,咱也不敢问 改完后蹭蹭的