导入excel

/**
	 * 导入
	 * 
	 * @return
	 */
	public String importEmployee() {
		EmployeeResult result = new EmployeeResult();
		if (null == cvsFile) {
			return maintain();
		}
		try {
			HSSFWorkbook workbook = null;
			// 通过字符流获取HSSFWorkbook对象
			workbook = new HSSFWorkbook(new FileInputStream(cvsFile));
			// 获取第一张表格
			org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);

			if (sheet == null) {
				LOG.info("没有找到csv文件");
				employeeList = new ArrayList<BofEmployeeDto>();
                BofEmployeeDto b = new BofEmployeeDto();
                b.setErrMsg("没有找到csv文件!");
                employeeList.add(b);
				return showImportEmployeePage();
			}
			// 获取表格最大行数
			int lastRowNum = sheet.getLastRowNum();

			if (lastRowNum > MOBILE_LASTROWNUM) {
				LOG.info("csv文件数据大于1000条");
				employeeList = new ArrayList<BofEmployeeDto>();
                BofEmployeeDto b = new BofEmployeeDto();
                b.setErrMsg("csv文件数据大于1000条");
                employeeList.add(b);
				return showImportEmployeePage();
			}

			Row row = sheet.getRow(1);

			if (row == null) {
				getRequest().setAttribute("importMobile", 0);
				return showImportEmployeePage();
			}
			// 获取第一行的列数
			int lastCellNum = row.getLastCellNum();
			if (lastCellNum != MOBILE_LASTCELLNUM) {
				LOG.info("csv文件数据列数不为9");
				employeeList = new ArrayList<BofEmployeeDto>();
                BofEmployeeDto b = new BofEmployeeDto();
                b.setErrMsg("请使用正确模版!");
                employeeList.add(b);
				return showImportEmployeePage();
			}
			// 解析CVS封装成LIST
			result = this.convertToList(sheet);
			Set<String> set = new HashSet<String>(); 
			for( BofEmployeeDto e:result.getList()){
			    set.add(e.getEmployeeNo());
			}
			if(set.size()<result.getList().size()){
			    //存在相同员工工号
			    employeeList = new ArrayList<BofEmployeeDto>();
			    BofEmployeeDto b = new BofEmployeeDto();
			    b.setErrMsg("存在相同员工工号");
			    employeeList.add(b);
			    return showImportEmployeePage();
			}
			if (result.isErrMsg()) {
				employeeList = new ArrayList<BofEmployeeDto>();
				for(BofEmployeeDto r:result.getList()){
					if(StringUtils.isNotEmpty(r.getErrMsg())){
						employeeList.add(r);
					}
				}
				return showImportEmployeePage();
			}
		} catch (Exception e) {
			LOG.error("csv文件导入失败");
			throw new RuntimeException(e);
		}
		try {
			// 插入表
			employeeBiz.batchInsertEmployee(result.getList());
		} catch (Exception e) {
			LOG.debug("向表中导入数据失败");
			result.setSuccess(false);
		}
		setShowContent(true);
		LOG.info("向表中导入数据成功");
		return search();
	}



/**
	 * 根据输入流转换成列表
	 * 
	 * @param sheet
	 * @return
	 */
	public EmployeeResult convertToList(org.apache.poi.ss.usermodel.Sheet sheet) {
		EmployeeResult result = new EmployeeResult();
		List<BofEmployeeDto> returnEmployeeImportTempDtoList = new ArrayList<BofEmployeeDto>();
		int i = 0;
		int j = 0;
		BofEmployeeDto returnEmployeeImportTempDto;
		for (org.apache.poi.ss.usermodel.Row row : sheet) {
			j=i+1;
			// 跳过第一第二行标题
			if (i == 0 || i == 1) {
				i++;
				continue;
			}

			returnEmployeeImportTempDto = new BofEmployeeDto();
			int cell = 1;
			if (row != null) {
				String area = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(area)) {
					returnEmployeeImportTempDto.setArea(area);
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				String company = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(company)) {
					returnEmployeeImportTempDto.setCompany(company);
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				String doorNo = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(doorNo)) {
					returnEmployeeImportTempDto.setDoorNo(doorNo);
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				String doorName = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(doorName)) {
					returnEmployeeImportTempDto.setDoorName(doorName);
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				String employeeNo = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(employeeNo)) {
					returnEmployeeImportTempDto.setEmployeeNo(employeeNo);
					// 如果已经存在该员工号,则提示 第XX行,已存在此员工,请核实!
					if (employeeBiz.isExistEmployeeNo(employeeNo)) {
						returnEmployeeImportTempDto.setErrMsg("第" + j
								+ "行,已存在此员工,请核实!");
						result.setErrMsg(true);
					}
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				String name = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(name)) {
					returnEmployeeImportTempDto.setName(name);
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				String job = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(job)) {
					returnEmployeeImportTempDto.setJob(job);
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				String level = getCellValueString(row.getCell(cell++));
				if (!StringUtils.isEmpty(level)) {
					returnEmployeeImportTempDto.setLevel(level);
				} else {
					returnEmployeeImportTempDto.setErrMsg("第" + j
							+ "行,格式错误,请核实!");
					result.setErrMsg(true);
				}
				returnEmployeeImportTempDtoList
						.add(returnEmployeeImportTempDto);
			}
			i++;
		}
		result.setList(returnEmployeeImportTempDtoList);
		return result;
	}

	/**
	 * 获取Cell类型
	 * 
	 * @param cell
	 * @return
	 */
	public String getCellValueString(Cell cell) {
		if (cell == null) {
			return null;
		}
		switch (cell.getCellType()) {
		case Cell.CELL_TYPE_STRING:
			return cell.getRichStringCellValue().getString();
		case Cell.CELL_TYPE_NUMERIC:
			return String.valueOf(cell.getNumericCellValue());
		case Cell.CELL_TYPE_BOOLEAN:
			return BooleanUtils.toStringTrueFalse(cell.getBooleanCellValue());
		case Cell.CELL_TYPE_FORMULA:
			return cell.getCellFormula();
		default:
			return null;
		}
	}



你可能感兴趣的:(poi,Excel,导入)