上傳excel文件

javascript:
	// 上傳xls文件
	var button = $('#importExcel');
	log(button);
	new AjaxUpload(button, {
		action : 'phoneNoMarket/xlsupload',
		name : 'file',
		onSubmit : function(file, ext) {
			if (!(ext && /^(xls)$/.test(ext))) {
				alert('文件格式不正確,請選擇 xls 格式的文件!', '系統提示');
				return false;
			}

			this.disable();
		},
		onComplete : function(file, response) {
			this.enable();
			var k = response.replace("<pre style=\"word-wrap: break-word; white-space: pre-wrap;\">", "").replace("</pre>", "");
			k=eval('('+k+')');
			importlist = k.importData;
			convtohtml2add(k.importData);

			return false;
		}
	});

java:

@RequestMapping(value = "/xlsupload", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	public @ResponseBody String xlsupload(HttpServletRequest request
			, HttpServletResponse response,HttpSession session
			, <span style="color:#ff0000;">@RequestParam("file") MultipartFile file</span>) {
		String strResult = "";
		CondQueryRepHmglView repview = new CondQueryRepHmglView();
		List<HmglAddGroupNumRepView> pagedata = new ArrayList<HmglAddGroupNumRepView>();
		<span style="color:#ff0000;">Workbook workbook;</span>
		if (!file.isEmpty()) {
			try {
				// 文件保存路徑
				<span style="color:#ff0000;">String filePath = request.getSession().getServletContext()
						.getRealPath("/")
						+ "upload/" + file.getOriginalFilename();</span>
				File newfile = new File(filePath);
				if (!newfile.exists()) {
					// 轉存文件
					file.transferTo(new File(filePath));
				} else {
					newfile.delete();
					file.transferTo(new File(filePath));
				}


				workbook = Workbook.getWorkbook(new File(filePath));
				// 得到excel第一頁的內容
				Sheet sheet = workbook.getSheet(0);
				String delimiter = ",";
				List<String> dataList = new ArrayList<String>();
				for (int i = 1; i < sheet.getRows(); i++) {
					String string = "";
					for (int j = 0; j < sheet.getColumns(); j++) {
						// <span style="color:#ff0000;">sheet.getCell(j,i).getContents();得到指定單元格的內容</span>
						string += sheet.getCell(j, i).getContents().equals("") ? " "
								+ delimiter
								: sheet.getCell(j, i).getContents() + delimiter;
					}
					dataList.add(string);
				}
				for (int i = 0; i < dataList.size(); i++) {
					String data = dataList.get(i);
					String dataArray[] = data.split(",");
					HmglAddGroupNumRepView repRecord = new HmglAddGroupNumRepView();
					// 手機號碼
					repRecord.setPhonenumber(dataArray[0]);
					// 省份 TODO:


					// 城市
					repRecord.setAreaname(dataArray[2]);
					// 運營商名稱
					repRecord.setOperatorsname(dataArray[3]);
					// 新增時間
					repRecord.setCreatedatetime("");
					// 選定時間
					repRecord.setSelectdatetime(dataArray[4]);
					// 推薦人號碼
					repRecord.setAngentno(dataArray[5]);
					// 狀態 :默認未選中 0
					repRecord.setIsselect("0");


					pagedata.add(repRecord);
				}


			} catch (BiffException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		repview.setImportData(pagedata);


		strResult = JsonUtil.toJson(repview);
		return strResult;
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章