public Map<String, Object> saveExcelUserCard(
MultipartFile multipartFile
) {
InputStream inputStream = null;
Workbook wb0 = null;
try {
inputStream = multipartFile.getInputStream();
//根据指定的文件输入流导入Excel从而产生Workbook对象
wb0 = new XSSFWorkbook(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (wb0 != null) {
//获取Excel文档中的第一个表单
Sheet sht0 = wb0.getSheetAt(0);
//对Sheet中的每一行进行迭代
// StringBuilder errorBuilder = new StringBuilder();
try {
for (Row r : sht0) {
//如果当前行的行号(从0开始)未达到1(第二行)则从新循环
//r就是表格的一行
int rowNum = r.getRowNum();
if (rowNum < 1) {
continue;
}
if (r.getCell(0)!= null){
//只有从数字表格中取出 String类型 才需要设置类型
r.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
}
// coolegeId 是我的字段名
String coolegeId = r.getCell(0).getStringCellValue();
String majorName = r.getCell(1).getStringCellValue();
/*String endTime = r.getCell(3).getStringCellValue();
Date endDate = simpleDateFormat.parse(endTime);*/
SchoolCollegeMajor major = new SchoolCollegeMajor();
major.setCollegeId(Integer.parseInt(coolegeId));
major.setMajorName(majorName);
major.setCreateAt(new Date());
major.setInvalid(true);
//调用数据库保存
schoolCollegeMajorService.save(major);
}
} catch (Exception e) {
e.printStackTrace();
}
return "添加成功";
}
return "文件无内容";
}
maven 依赖
<!-- excel导入 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
这是我的exec格式