其他分享
首页 > 其他分享> > Excel导入操作,poi

Excel导入操作,poi

作者:互联网

导入操作,仅供参考,具体情况具体而论

  @Override
  public ReturnObject inforImport(LogySbjsJdsbqxxxParts entity, HttpServletRequest req, MultipartFile file)
  		throws IOException {
  	ReturnObject returnObject = new ReturnObject();
  	List<LogySbjsJdsbqxxxAccount> lsjList = new ArrayList<LogySbjsJdsbqxxxAccount>();
  	JwtUserObject jwtUserObject = JwtUtil.getJwtUserObj(req.getHeader("Authorization"));
  	// 创建一个excel文件
  	HSSFWorkbook workbook = null;
  	workbook = new HSSFWorkbook(file.getInputStream()); // 获取文件
  	HSSFSheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
  	int lastRowNum = sheet.getLastRowNum();
  	// 获取数据
  	for (int i = 2; i <= lastRowNum; i++) {
  		HSSFRow row = sheet.getRow(i);
  		if (row != null) {
  			List<String> liststr = new ArrayList<>();
  			String value = null;
  			for (Cell cell : row) {
  				if (cell != null && cell.getCellType() != CellType.BLANK) {
  					cell.setCellType(CellType.STRING);
  					value = cell.getStringCellValue(); // 读取数据
  					liststr.add(value);
  				} else {
  					value = "";
  					liststr.add(value);
  				}
  			}
  			if (liststr.size() > 0) {
  				lsjList = extracted(entity, lsjList, jwtUserObject, i, liststr);
  			}
  		}
  	}
  	logySbjsJdsbqxxxAccountDao.saveAll(lsjList);
  	returnObject.setRemsg("上传成功");
  	return returnObject;
  }
	/**
	 * 构造数据 @Title: extracted @Description: TODO(这里用一句话描述这个方法的作用) @param: @param
	 * entity @param: @param lsjList 定义一个List @param: @param
	 * jwtUserObject @param: @param i 行号 @param: @param liststr 解析出来的一行数据 @return:
	 * void @throws
	 */
	private List<LogySbjsJdsbqxxxAccount> extracted(LogySbjsJdsbqxxxParts entity, List<LogySbjsJdsbqxxxAccount> lsjList,
			JwtUserObject jwtUserObject, int i, List<String> liststr) {

		entity.setITEMCODE(jwtUserObject.getITEMCODE());
		entity.setQxxxModeId(entity.getQxxxModeId());
		Example<LogySbjsJdsbqxxxParts> example = Example.of(entity);
		// 查询所有并组装
		List<LogySbjsJdsbqxxxParts> list = logySbjsJdsbqxxxPartsDao.findAll(example);
		// 排序输出
		List<LogySbjsJdsbqxxxParts> collectList = list.stream()
		.sorted(Comparator.comparing(LogySbjsJdsbqxxxParts::getPartsCode)
		.thenComparing(LogySbjsJdsbqxxxParts::getSort)).collect(Collectors.toList());
		for (int j = 0; j < liststr.size(); j++) {
			String id = UUID.randomUUID().toString().replaceAll("-", ""); // id
			String str = liststr.get(j);
			if(collectList.get(j).getPartsName().contains("日期")) {  //处理日期为数字
				Calendar calendar = new GregorianCalendar(1900, 0, -1);
				Date d = calendar.getTime();
				Date datastr = DateUtils.addDays(d, Integer.valueOf(str));
				SimpleDateFormat dataformatter  = new SimpleDateFormat( "yyyy-MM-dd");
				str = dataformatter.format(datastr);
			}
			LogySbjsJdsbqxxxAccount lsj = new LogySbjsJdsbqxxxAccount(id, entity.getITEMCODE(),
					jwtUserObject.getItemName(), entity.getQxxxModeId(), collectList.get(j).getQxxxPartsId(),
					collectList.get(j).getPartsCode(), collectList.get(j).getPartsName(), str, new Date(),
					jwtUserObject.getUSERID(), jwtUserObject.getUSERNAME(), 0, i - 1);
			// 添加到集合中
			lsjList.add(lsj);
		}
		return lsjList;

	}

标签:liststr,Excel,param,entity,导入,lsjList,jwtUserObject,poi,new
来源: https://www.cnblogs.com/hhs-5120/p/16656840.html