一、前提条件
POI组件基础知识,POI操作Excel文档、读取、写入、合并单元格。(点击查看)
SSM框架整合Maven工程整合Spring+Springmvc+Mybatis(详细教程,附代码)(点击查看)
二、POI与SSM框架整合实现Excel导入
1、Maven工程pom.xml文件中添加Excel导入所需依赖包
org.apache.poi
poi
3.16
org.apache.poi
poi-ooxml-schemas
3.16
org.apache.poi
poi-excelant
3.16
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.1
2、修改spring-mvc.xml增加文件上传配置
3、编写Excel上传处理控制类Controller层
@Controller
public class FileUploadController {
@Autowired
NewStudentInfoService newStudentInfoService;
@RequestMapping(value="/importexcel",method=RequestMethod.POST)
public String uploadExcel(@RequestParam("file") MultipartFile file,HttpServletRequest request,Model model){
//获取服务器端路径
String path=request.getServletContext().getRealPath("upload");
//获取到上传文件名称
String fileName=file.getOriginalFilename();
//创建目标File
File targetFile=new File(path+"\\"+fileName);
//创建存储目录
File targetPath=new File(path);
//判断服务器端目录是否存在,如果不存在创建目录
if(!ta