Java解析Excel 实现批量上传

@RequestMapping(value = "/importWorkRecordsExcel")
@ResponseBody
public String upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
   SerParameter serParameter = new SerParameter();
   Map<String, Object> resultMap = new HashMap<String, Object>();
   try {
      Map<String, Object> parseResult = parseExcel(request);
         //文件名
      String fileName = (String) parseResult.get("FILE_NAME");
        //excel文件解析后的ListMap
      List<Map<String, String>> excelData = (List<Map<String, String>>) parseResult.get("EXCEL_DATA");
        // ......保存数据的方法
}

private Map<String, Object> parseExcel(HttpServletRequest request) throws Exception {
   Map<String, Object> parseResult = new HashMap<String, Object>();
   List<Map<String, Object>> excelData = null;
   boolean isMultipart = ServletFileUpload.isMultipartContent(request);
   CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
   if(multipartResolver.isMultipart(request)){
      MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
      Iterator<String> iterator = multiRequest.getFileNames();
      while (iterator.hasNext()) {
         CommonsMultipartFile multipartFile =(CommonsMultipartFile)multiRequest.getFile(iterator.next());
         if(multipartFile !=null){
            FileItem item=multipartFile.getFileItem();
            if (!item.isFormField()) {
               String fileName = item.getName();
               String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
               if (".xls".equals(fileType)) {
                  excelData = parseWorkRecordsXls(item);
               } else if (".xlsx".equals(fileType)) {
                  excelData = parseWorkRecordsXlsx(item);
               }
               parseResult.put("FILE_NAME", fileName);
               parseResult.put("EXCEL_DATA", excelData);
            }
         }
      }
      if (null == excelData) {
         throw new Exception("无法解析的Excel文件!");
      } else if (excelData.size() == 0) {
         throw new Exception("解析Excel无数据!");
      }
   }
   return parseResult;
}

private List<Map<String, Object>> parseWorkRecordsXls(FileItem fileItem) throws Exception {
   List<Map<String, Object>> excelData = new ArrayList<Map<String, Object>>();
   if (null != fileItem.getInputStream()) {
      HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileItem.getInputStream());
      if (hssfWorkbook.getNumberOfSheets() > 0) {
         HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
         if (hssfSheet.getLastRowNum() > 0) {
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
               HSSFRow hssfRow = hssfSheet.getRow(rowNum);
               if (hssfRow != null) {
                  Map<String, Object> recordMap = new HashMap<String, Object>();
                  recordMap.put("visitorName", null == hssfRow.getCell(0) ? "":FileUtils.getCellValue(hssfRow.getCell(0)));
                  recordMap.put("visitorType", null == hssfRow.getCell(1) ? "":FileUtils.getCellValue(hssfRow.getCell(1)));
                  recordMap.put("staffCode", null == hssfRow.getCell(2) ? "":FileUtils.getCellValue(hssfRow.getCell(2)));
                  recordMap.put("staffName", null == hssfRow.getCell(3) ? "":FileUtils.getCellValue(hssfRow.getCell(3)));
                  recordMap.put("mobile", null == hssfRow.getCell(4) ? "" :FileUtils.getCellValue(hssfRow.getCell(4)));
                  recordMap.put("gender", null == hssfRow.getCell(5) ? "":FileUtils.getCellValue(hssfRow.getCell(5)));
                  recordMap.put("cardType", null == hssfRow.getCell(6) ? "" :FileUtils.getCellValue(hssfRow.getCell(6)));
                  recordMap.put("cardNum", null == hssfRow.getCell(7) ? "" :FileUtils.getCellValue(hssfRow.getCell(7)));
                  recordMap.put("effectDate", null == hssfRow.getCell(8) ? "" :FileUtils.getCellValue(hssfRow.getCell(8)));
                  recordMap.put("expireDate", null == hssfRow.getCell(9) ? "" : FileUtils.getCellValue(hssfRow.getCell(9)));
                  excelData.add(recordMap);
               }
            }
         } else {
            throw new Exception("无有效数据行!");
         }
      } else {
         throw new Exception("文件sheet页数量为空!");
      }
   } else {
      throw new Exception("File文件为空!");
   }
   return excelData;
}