1. 程式人生 > >jquery.form.js 利用ajaxSubmit ajax上傳Excel,

jquery.form.js 利用ajaxSubmit ajax上傳Excel,

@RequestMapping(value="importClubPersonInfoFromExcel.json", method={RequestMethod.POST})
	@ResponseBody
	public Map<String,Object> importClubPersonInfoFromExcel(@RequestParam("file") MultipartFile file,@RequestParam Map<String, Object> params) throws IOException, Exception {
		Map<String,Object> result = new HashMap<String, Object>();
		String opr_msg = "匯入成功!";
		
		if(file == null || file.isEmpty()) {
			opr_msg = "未獲取檔案物件!";
			result.put("opr_msg", opr_msg);
			result.put("flag", false);
			return result;
		}
		//>10M
		long maxFileSize = 10<<20;
		if(file.getSize() > maxFileSize) {
			opr_msg = "檔案大小不能超過10M!";
			result.put("opr_msg", opr_msg);
			result.put("flag", false);
			return result;
		}
		String fileName = file.getOriginalFilename();
		if(fileName.lastIndexOf(".xls") == -1 && fileName.lastIndexOf(".xlsx") == -1) {
			opr_msg = "檔案格式不正確!";
			result.put("opr_msg", opr_msg);
			result.put("flag", false);
			return result;
		}
	
		opr_msg = clubPersoninfoService.importClubPersoninfoExcel(file.getInputStream(),params);
		if(!"success".equals(opr_msg)){
			result.put("opr_msg", opr_msg);
			result.put("flag", false);
			return result;
		}
		result.put("flag", true);
		return result;
	}

	@Override
	public String importClubPersoninfoExcel(InputStream is,Map<String,Object> params) throws Exception {
		/*
		 * Excel讀取
		 */
		//預設sheet名稱
		String DEFAULT_SHEETNAME = "俱樂部人員資訊匯入";
		Workbook wb = WorkbookFactory.create(is);
		Sheet sheet = wb.getSheet(DEFAULT_SHEETNAME);
		//如果不是預設命名,則獲取第一項
		if(sheet == null) {
			sheet = wb.getSheetAt(0);
		}
		if(sheet == null) {
			return "sheet頁不存在!";
		}
		Iterator<Row> row_iter = sheet.iterator();
		Set<ClubPersoninfoBean> set = new HashSet<ClubPersoninfoBean>();
		if(row_iter.hasNext()) {
			//Skip first row
			row_iter.next();
			while(row_iter.hasNext()) {
				Row r = row_iter.next();
				//Check values
				String no_cell_title = "序號";
				Cell no_cell = r.getCell(0);
				if(no_cell == null) {
					return "序號為"+no_cell+"的"+no_cell_title+"不可為空!";
				}
				no_cell.setCellType(Cell.CELL_TYPE_STRING);
				if(no_cell.getCellType() != Cell.CELL_TYPE_STRING) {
					return "序號為"+no_cell+"的"+no_cell_title+"格式不正確!";
				}
				
				String saleCode_cell_title = "銷售人員工號";
				Cell saleCode_cell = r.getCell(1);
				if(saleCode_cell == null) {
					return "序號為"+no_cell+"的"+saleCode_cell_title+"不可為空!";
				}
				saleCode_cell.setCellType(Cell.CELL_TYPE_STRING);
				if(saleCode_cell.getCellType() != Cell.CELL_TYPE_STRING) {
					return "序號為"+no_cell+"的"+saleCode_cell_title+"格式不正確!";
				}
				
				String saleName_cell_title = "銷售員姓名";
				Cell saleName_cell = r.getCell(2);
				if(saleName_cell == null) {
					return "序號為"+no_cell+"的"+saleName_cell_title+"不可為空!";
				}
				saleName_cell.setCellType(Cell.CELL_TYPE_STRING);
				if(saleCode_cell.getCellType() != Cell.CELL_TYPE_STRING) {
					return "序號為"+no_cell+"的"+saleName_cell_title+"格式不正確!";
				}
				
				String remarks_cell_title = "備註";
				Cell remarks_cell = r.getCell(3);
				String remarks_cell_str = "";
				if(remarks_cell == null || "".equals(remarks_cell)) {
					remarks_cell_str = null;
				}else{
					remarks_cell_str = remarks_cell.toString().trim();	
					remarks_cell.setCellType(Cell.CELL_TYPE_STRING);
					if(remarks_cell.getCellType() != Cell.CELL_TYPE_STRING) {
						return "序號為"+no_cell+"的"+remarks_cell_title+"格式不正確!";
					}
				}
				
				
				ClubPersoninfoBean clubPersoninfoBean = new ClubPersoninfoBean();
																
				clubPersoninfoBean.setSales_no(saleCode_str);
				clubPersoninfoBean.setSales_name(saleName_str);
				clubPersoninfoBean.setRemarks(remarks_cell_str);
				set.add(clubPersoninfoBean);
			}
		}
		List<ClubPersoninfoBean> list = new ArrayList<ClubPersoninfoBean>(set);
		return insertBatchClubPersoninfo(list);
	}

程式碼只做參考。