上传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;
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值