Excel写入数据

Excel数据入库,是通过存储过程判断字段是否为空,为空时给出提示,符合条件时修改数据字典数据入库。
Controller类:

@SuppressWarnings("unchecked")
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		String vposition = request.getParameter("vposition");
		AjaxJson j = new AjaxJson();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			CommonsMultipartFile cf = (CommonsMultipartFile) file;
			DiskFileItem fi = (DiskFileItem) cf.getFileItem();//用来转换file文件格式
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(2);
			params.setNeedSave(true);
			try {
				String flag = teaProductentityService.fromExcelToList(request, fi.getName(), vposition);
				if (flag.equals("-1")) {
					j.setMsg("文件导入失败!");
				} else {
					j.setMsg("文件导入成功!");
				}

			} catch (Exception e) {
				j.setMsg("文件导入失败!");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			} finally {
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}

ServiceImpl实现类:

@Override
	public String fromExcelToList(HttpServletRequest request, String fileName, String vposition) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		MultipartHttpServletRequest mpt = (MultipartHttpServletRequest) request;
		MultipartFile file = mpt.getFile("file");
		String userId = ResourceUtil.getSessionUser().getId();
		String result = "";
		String depart = ResourceUtil.getSessionUser().getDepartid();
		
		String tempTablePrefix = "tmp_imp_" + sdf.format(new Date()) + Math.round(Math.random() * 1000);
		try {

			String[] paramsStr = new String[] { "物料编码,vproduct_id,0", "设备编号,vserial_no,1",
					"错误信息,remark,24", "错误提示,flag,25", "id,id,26" };

			String[] inputValue = new String[] { tempTablePrefix };
			int[] outValue = new int[] { java.sql.Types.FLOAT, java.sql.Types.VARCHAR };
			// 存储过程名
			// String procName = "product_entity";
			String procName = "product_real";
			result = new HssfHelper().importExcelToDB2(request, file, tempTablePrefix, paramsStr, procName, 1, true,
					true, 3, inputValue, outValue, "upload/monitor");

			if (result.equals("-1")) {
				String sql = "select * from " + tempTablePrefix;
				List<TeaProductEntityPo> productStructureList = namedParameterJdbcTemplate.query(sql, new HashMap(),
						new BeanPropertyRowMapper<>(TeaProductEntityPo.class));
				// 计算出需要多少个sheet
				int sheetCount = 1;
				if (productStructureList != null && productStructureList.size() > 0) {
					sheetCount = productStructureList.size() / 65000 == 0 ? 1
							: (productStructureList.size() % 65000 >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值