以下是步骤:
1.导入所需依赖坐标或者相对应的jar包
commons-fileupload
commons-fileupload
1.3.2
org.apache.poi
poi-ooxml
3.17
org.apache.poi
poi
3.17
commons-fileupload
commons-fileupload
1.3.2
commons-io
commons-io
2.4
2.在springmvc.xml中配置文件解析器对象
3.在html或jsp页面中写页面,注意其中的name的值我的为attach
4.创建一个User对象;自行补全Getter和Setter以及toString
private String username;
private String password;
private Integer age;
private Date birthday;
private Boolean bool;
5.编写UserController中的方法
@RequestMapping(value = "fileUploadExcel", method = RequestMethod.POST)
@ResponseBody
public void informationBatchAdd(MultipartHttpServletRequest request) {
try {
//得到上传的文件此处的attach与上面提到的保持一致!
MultipartFile fileFile = request.getFile("attach");
//转换成输入流
InputStream in = fileFile.getInputStream();
XSSFWorkbook readWb = new XSSFWorkbook(in);
List list =new ArrayList();
/*HSSFWorkbook readWb = new HSSFWorkbook(in);*/
//遍历该表格中所有的工作表,i表示工作表的数量 getNumberOfSheets表示工作表的总数
for (int i = 0; i < readWb.getNumberOfSheets(); i++) {
XSSFSheet sheet = readWb.getSheetAt(i);
// 循环行Row
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
XSSFRow hssfRow = sheet.getRow(rowNum);
if (hssfRow != null) {
//因为我没有找到更好的方法,所以只能一个个去set
User user=new User();
for (int colNum = 0; colNum < hssfRow.getPhysicalNumberOfCells(); colNum++) {
if (colNum==0){
user.setUsername(hssfRow.getCell(0).toString());
}if (colNum==1){
user.setPassword(hssfRow.getCell(1).toString());
}if (colNum==2){
user.setAge((int) Double.parseDouble(hssfRow.getCell(2).toString()));
}if (colNum==3){
//此处通过一个cell对象的一个方法来获取excel中的date日期类型
XSSFCell cell = hssfRow.getCell(3);
Date date = cell.getDateCellValue();
user.setBirthday(date);
}if (colNum==4){
String stringCellValue = hssfRow.getCell(4).toString();
if ("已充气".equals(stringCellValue)){
user.setBool(true);
}else if ("未充气".equals(stringCellValue)){
user.setBool(false);
}
}
//观察一下获取到的数据
System.out.println(hssfRow.getCell(colNum));
}
//将User对象存入List集合
list.add(user);
//赋值实例对象 做插入数据库操作
}
}
}
System.out.println(list.size());
list.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
6.编写excel表格
(关于EXCEL的注意事项,假如你删除了5行数据,那么你最后提交的EXCEL表数据必须多于5行,或者重新创建excel表!否则空指针)
注意图片中excel表格的日期的一列只要是如上图中的日期类型均可
7.部署到tomcat运行程序,选择文件后点击上传
8.集合遍历之后的结果(我用的foreac来遍历的可以用iter),可以看到日期格式正常封装进去
9.补充全部输出结果
在这里插入代码片张三
zhangsan
18.0
20-四月-2020
已充气
张三
zhangsan
18.0
21-四月-2020
未充气
张三
zhangsan
18.0
22-四月-2020
已充气
李四
lisi
20.0
23-四月-2020
未充气
张三
zhangsan
22.0
24-四月-2020
已充气
李四
lisi
24.0
25-四月-2020
已充气
张三
zhangsan
26.0
26-四月-2020
已充气
李四
lisi
28.0
27-四月-2020
已充气
张三
zhangsan
30.0
28-四月-2020
已充气
李四
lisi
32.0
29-四月-2020
已充气
张三
zhangsan
34.0
30-四月-2020
已充气
李四
lisi
36.0
01-五月-2020
已充气
张三
zhangsan
38.0
02-五月-2020
未充气
李四
lisi
40.0
03-五月-2020
未充气
张三
zhangsan
42.0
04-五月-2020
未充气
李四
lisi
44.0
05-五月-2020
未充气
张三
zhangsan
46.0
06-五月-2020
未充气
李四
lisi
48.0
07-五月-2020
未充气
张三
zhangsan
50.0
08-五月-2020
未充气
李四
lisi
52.0
09-五月-2020
未充气
20
User{username='张三', password='zhangsan', age=18, birthday=Mon Apr 20 14:15:50 CST 2020, bool=true}
User{username='张三', password='zhangsan', age=18, birthday=Tue Apr 21 14:15:50 CST 2020, bool=false}
User{username='张三', password='zhangsan', age=18, birthday=Wed Apr 22 14:15:50 CST 2020, bool=true}
User{username='李四', password='lisi', age=20, birthday=Thu Apr 23 14:15:50 CST 2020, bool=false}
User{username='张三', password='zhangsan', age=22, birthday=Fri Apr 24 14:15:50 CST 2020, bool=true}
User{username='李四', password='lisi', age=24, birthday=Sat Apr 25 14:15:50 CST 2020, bool=true}
User{username='张三', password='zhangsan', age=26, birthday=Sun Apr 26 14:15:50 CST 2020, bool=true}
User{username='李四', password='lisi', age=28, birthday=Mon Apr 27 14:15:50 CST 2020, bool=true}
User{username='张三', password='zhangsan', age=30, birthday=Tue Apr 28 14:15:50 CST 2020, bool=true}
User{username='李四', password='lisi', age=32, birthday=Wed Apr 29 14:15:50 CST 2020, bool=true}
User{username='张三', password='zhangsan', age=34, birthday=Thu Apr 30 14:15:50 CST 2020, bool=true}
User{username='李四', password='lisi', age=36, birthday=Fri May 01 14:15:50 CST 2020, bool=true}
User{username='张三', password='zhangsan', age=38, birthday=Sat May 02 14:15:50 CST 2020, bool=false}
User{username='李四', password='lisi', age=40, birthday=Sun May 03 14:15:50 CST 2020, bool=false}
User{username='张三', password='zhangsan', age=42, birthday=Mon May 04 14:15:50 CST 2020, bool=false}
User{username='李四', password='lisi', age=44, birthday=Tue May 05 14:15:50 CST 2020, bool=false}
User{username='张三', password='zhangsan', age=46, birthday=Wed May 06 14:15:50 CST 2020, bool=false}
User{username='李四', password='lisi', age=48, birthday=Thu May 07 14:15:50 CST 2020, bool=false}
User{username='张三', password='zhangsan', age=50, birthday=Fri May 08 14:15:50 CST 2020, bool=false}
User{username='李四', password='lisi', age=52, birthday=Sat May 09 14:15:50 CST 2020, bool=false}