我是靠谱客的博主 愉快发箍,这篇文章主要介绍Excel导入,前台上传文件,后台获取文件流,并在服务器保存文件,返回服务器文件地址,根据地址将文件导入数据库。1、过程流程:,现在分享给大家,希望可以做个参考。
1、过程流程:
本地excel(要上传的文件)
页面截图:
开始操作!!!
点击选择文件,选取第一张图(本地要上传的excel),选择之后,点击upload上传。
在服务器端生成的文件:
数据库添加的数据:
2、具体代码:
1)需要IDEA Mysql; SpringBoot; Mybatis
2)html代码:(需要引入jquery)
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="uploadForm">
<input id="file" type="file"/>
<button id="upload" type="button" onclick="test()">upload</button>
</div>
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<script type="text/javascript">
function test(){
var formData = new FormData();
formData.append('file', document.getElementById("file").files[0]);
alert(formData)
$.ajax({
url: 'upload',
type: 'POST',
cache: false,
data: formData,
processData: false,
contentType: false
}).done(function(res) {
}).fail(function(res) {});
}
</script>
</body>
</html>
3)Maven引入依赖(pom.xl)
<!--导入导出-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
4)工具类:
https://www.aliyundrive.com/s/TMGVwGf4oeS
提取码: 44mh
5)Controller
@ResponseBody
@RequestMapping("/upload")
public String uploadPicture(@RequestParam(value="file",required=false)MultipartFile file) {
String fileName = ExcelUpload.uploadPicture(file);
// 获取Excel文件
File excelFile = new File(fileName);
//读取单页excel,获取List<HashMap<String, Object>>格式
List<HashMap<String, Object>> list = ExcelUtils.parseSingleExcelToMap(excelFile);
System.out.println(list);
List<Student> st = new ArrayList<>();
//将excel信息变成Student类对象
List<Integer> stu_ids = new ArrayList<>();
for (HashMap<String, Object> m : list) {
Student s =new Student();
for (String k : m.keySet()) {
if(k.contains("学号")){
s.setStu_id(Integer.parseInt((String) m.get(k)));
stu_ids.add(Integer.parseInt((String) m.get(k)));
}
else if(k.contains("身份证")){ s.setPassword(m.get(k).toString().substring(m.get(k).toString().length() - 6));}
else if(k.contains("姓名")){s.setStu_name(m.get(k).toString()); }
else if(k.contains("学院")){s.setCollege(m.get(k).toString());}
else if(k.contains("班级")){s.setClasses(Integer.parseInt((String) m.get(k)));}
}
st.add(s);
}
for(Student s : st){
System.out.println(s.toString());
}
System.out.println(st);
System.out.println(stu_ids);
int i = studentService.addstudent1_test(st); //调用服务层代码 插入信息到数据库
System.out.println("成功插入========"+i+"行!");
/*//读取多页excel,获取List<HashMap<String, Object>>格式
List<List<HashMap<String, Object>>> lists =ExcelUtils.parseComplexExcel(excelFile);*/
return "";
}
4)Sql语句:
<insert id="addstudent1_test" parameterType="java.util.List">
insert ignore student1 (stu_id,password,stu_name,college,classes)
values
<foreach collection="lists" item="list" index="index" separator=",">
(#{list.stu_id} , #{list.password} , #{list.stu_name} , #{list.college} , #{list.classes})
</foreach>
</insert>
最后
以上就是愉快发箍最近收集整理的关于Excel导入,前台上传文件,后台获取文件流,并在服务器保存文件,返回服务器文件地址,根据地址将文件导入数据库。1、过程流程:的全部内容,更多相关Excel导入内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复