Excel文件导入web页面

最近期在负责的一个项目中,用到很多Excel文件导入web页面这个方法,现在总结归纳一下。

**html===============**

<!-- 导入Excel -->
<form style="color:white;border:none;cursor:pointer" class="impor" id="form1"  method="POST"  enctype="multipart/form-data">
        导入excel
<input style="display:none" class="import" type="file" name="upfile" value="" >
</form>

<script src="../plugins/jquery/jquery.form.min.js"></script>
  <script type="text/javascript">
	  var impor = document.querySelector('.impor');
	  var imports = document.querySelector('.import');
	  impor.onclick = function(){
	    imports.click();
	  };
	  imports.onchange = function(){
	    console.log(this.files[0]);
	    // people.innerHTML = this.file;
	  }

=controller.js================

//文件上传
  $("input[type=file]").change(function (e) {

    if (window.confirm('你确定要上传文件吗?')) {
      $('#form1').ajaxSubmit({
        url: '/PostalBank/user/uploadexcel.do',
        dataType: 'text',
        success: resutlMsg,
        error: errorMsg
      });

      function resutlMsg(msg) {
        alert(msg);
        $("#upfile").val("");
        //调用方法
        $scope.reloadList();//刷新列表
      }

      function errorMsg() {
        alert("导入excel出错!请选择正确的文件!");
      }

controller.java==============
``

/**
	 * 完成度设置后上传excel
	 * @return
	 */
	@RequestMapping(value="uploadTemplet.do") 
	public Result uploadTemplet(Integer tindex,HttpServletRequest request, HttpServletResponse response) throws Exception {

	
	MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
	InputStream in = null;
	List<List<Object>> listob = null;
	MultipartFile file = multipartRequest.getFile("upfile");

	if (file.isEmpty()) {
		throw new CustomerException("上传文件不能为空");
	}
	in = file.getInputStream();
	String fileName = file.getOriginalFilename();
	listob = new ImportExcelUtil().readExcelWithTitle(in, fileName);
/*		log.info("输入文件名为: " + fileName + " 数据为:" + listob);
*/	
	//获取当前的季度
	Integer quarter = DateUtils.currentQuarter();
	 //录入库,得到相关录入岗位信息(新)
	for (int i = 0; i < listob.size(); i++) {
		// 得到每页中有多少个map集合数据
		for (int j = 0; j < listob.get(i).size(); j++) {
			 Map dataMap = new HashMap<>();
			
			 //岗位ID 通过输入获取
			 TCompanyAssess tCompanyAssess = new TCompanyAssess();
			 TTime tTime  = new TTime();
			 TIndex item=new TIndex();

			
			dataMap = (Map) listob.get(i).get(j);
			//录入指标与库
			if (dataMap.containsKey("考核指标")) {

				//根据获取到的id查找findCategory
				 TUserCategory category = userCategoryService.findCategory(tindex);
				 item.setCreatetime(new Date());
				 item.setPermissionid(tindex);
				 item.setPermissionname(category.getCategoryname());

			
				if(dataMap.containsKey("指标类别")) {
					String indexNum = (String) dataMap.get("指标类别");
					item.setType(indexNum);
					if(indexNum.equals("基本指标")) {
				
						item.setTypeid(0);
					}
					if(indexNum.equals("分类指标")) {
				
						item.setTypeid(1);
					}
					dataMap.remove("指标类别");
				}
				if(dataMap.containsKey("考核指标")) {
					String accname = (String) dataMap.get("考核指标");
					item.setTitle(accnam                                             e);
				
					dataMap.remove("考核指标");
				}
				if(dataMap.containsKey("考核岗位")) {
					String khgw = (String) dataMap.get("考核岗位");
					TPost tPost = tPostService.selectPostByPostName(khgw);
				
					 item.setPostid(tPost.getId());
					
					dataMap.remove("指标名称");
				}

				TIndex tcoma=tIndexService.selectBytitle(item.getTitle());
			
				if(tcoma!=null)
				{
					tIndexService.update(item);
				
				}else
				{
					tIndexService.add(item);

				}
				
			}
				

			}else
			{
				return new Result(false, "数据不正确");
			}
		}
	}
	return new Result(true, "文件上传成功");
}

`

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值