bootstrap 文件上传fileinput

页面中引入引入:fileinput.js

js初始化:

<div class="modal-body">
  <input type="file" id="customerexcel" name="file"  accept=".xls,.xls" multiple/>
</div>

后台js进行初始化;


	$("#customerexcel").fileinput({
		language : 'zh', // 设置语言
		uploadUrl : getProjectURL() + 'customer/drcustomer', // 上传的地址
		allowedFileExtensions : [ 'xls' ],// 接收的文件后缀
		showUpload : true, // 是否显示上传按钮
		showCaption : false,
		maxFileCount : 1
	// 是否显示标题
	}).on('fileuploaded', function(event, data) {
		alert(data.response.msg);
	})

最终的实现结果:

因为我这个是导入的excel,后台:

	@RequestMapping("/drcustomer")
	@ResponseBody
	public JSONObject drCustomer(HttpServletResponse response, HttpServletRequest request) {
		try {
			int count = 0;
			if (ServletFileUpload.isMultipartContent(request)) {
				MultipartHttpServletRequest fileUpload = (MultipartHttpServletRequest) request;
				MultipartFile file = fileUpload.getFile("file");
				Workbook workbook = Workbook.getWorkbook(file.getInputStream());
				Sheet sheet = workbook.getSheet(0);
				int rows = sheet.getRows();
				for (int i = 1; i < rows; i++) {
					Cell[] cells = sheet.getRow(i);
					Customer customer = new Customer();
					customer.setCode(cells[0].getContents());

					// 判断是否存在相同的代码
					if (customerService.isExistSameCode(customer.getCode())) {
						count++;
						continue;
					}
					customer.setCusname(cells[1].getContents());
					customer.setComtaxno(cells[2].getContents());
					customer.setAddresstel(cells[3].getContents());
					customer.setSj(cells[4].getContents());
					customer.setEmail(cells[5].getContents());
					customer.setAccount(cells[6].getContents());
					customer.setAddtime(new Date());
					customerService.save(customer);
				}
				String msg="";
				if(count>0) {
					msg="客户导入成功:有" + count + "在相同的客户代码,没有导入";
				}else {
					msg="客户导入成功";
				}
				return BuildJsonOfObject.getJsonString("1",msg);
			} else {
				return BuildJsonOfObject.getErrorJSONString("0", "文件上传失败");
			}
		}catch(BiffException e) {
			throw new RuntimeException("内容解析失败,请将当前excel另存为Microsoft Excel 97-2003 文件的*.xls");
		} catch (Exception e) {
			e.printStackTrace();
			return BuildJsonOfObject.getErrorJSONString("0", e.getMessage());
		}

	}

希望对有所帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值