1、controller类
@PostMapping("/saveExcel")
public Model saveExcelInfo(@RequestPart("file") MultipartFile file, HttpServletRequest request,Model model) throws IOException {
String newname = UUID.randomUUID().toString().replace("-","");
//获取项目真实路径
String rootPath = request.getSession().getServletContext().getRealPath("/");
//获取文件后缀
String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
newname += ext;
//上传后文件相对项目的相对路径
String relativePath = Paths.get("excels",newname).toString();
//上传后文件真实路径
Path path = Paths.get(rootPath,relativePath);
//如果保存文件的目录不存在,则创建
if (!Files.exists(path.getParent())){
Files.createDirectory(path.getParent());
}
//保存文件
file.transferTo(path.toFile());
service.saveExcel(path.toString());
2、service类
@Override
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public boolean saveExcel(String url) {
List<Staff> list = StaffExcel.start(url);
3.Excel操作类
/**
* 描述:
* 导入Excel表数据--staff类
*
* @author mixue
* @create 2018-06-14 8:43
*/
public class StaffExcel {
//记录类的输出信息
Log log = LogFactory.getLog(StaffExcel.class);
/**
* </