java Excel导入数据库

/**
	 * 资产批量入库excel导入
	 * @return
	 */
	public String addBatch() {
		Transaction t=null;
		try {
			t=this.getStockinService().getTransaction();
		} catch (Exception e1) {
			if(t==null){
				this.outJsonError( "事物没有正确启动!");
				return SUCCESS;
			}
		}
		if(t==null){
			this.outJsonError( "事物没有正确启动!");
			return SUCCESS;
		}
		t.begin();
		List list = new ArrayList();
		int count=0,errcount=0,existcount=0;//总记录数,格式错误数,已经导入存在的数
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(stockinFile));
			HSSFSheet aSheet = workbook.getSheetAt(0);
			if (workbook.getSheetAt(0)!= null) {//get the first sheet
				for (int rowNumOfSheet = 2; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
					if (aSheet.getRow(rowNumOfSheet)!=null) {
						HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
						Object[] o = new Object[9];
						count++;
						String value="";
						String msg="";
						for (short cellNumOfRow = 0; cellNumOfRow < 9; cellNumOfRow++) {//aRow.getLastCellNum()
							if (aRow.getCell(cellNumOfRow)!=null) {
								HSSFCell aCell = aRow.getCell(cellNumOfRow);
								value = getCellValue(aCell);
								if(cellNumOfRow == 7){
									try{
										aCell.getDateCellValue();
									}catch(Exception e){
										this.outJsonError("第"+(rowNumOfSheet+1)+"行,第"+(cellNumOfRow+1)+"列巡检时间格式不是有效的日期格式");
									}
									value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(aCell.getDateCellValue());
								}
								o[cellNumOfRow] = Tools.processNull(value.trim()).replaceAll("\\s*", "");
								String relust = CheckImport(String.valueOf(o[cellNumOfRow]),cellNumOfRow,rowNumOfSheet);
								if(!"".equals(relust)){
									this.outJsonError(relust);
									return SUCCESS;
								}
								if (cellNumOfRow == 0) {
									Stockin stk = stockinService.getStockin(String.valueOf(o[cellNumOfRow]));
									if (stk != null) {
										this.outJsonError("第"+ (rowNumOfSheet+1) +"行,第"+(cellNumOfRow+1)+"列资产信息已入库!");
										return SUCCESS;
									}
								}
								if (cellNumOfRow == 1) {
									Register r = stockinService.getRegister(String.valueOf(o[cellNumOfRow]));
									if (r != null) {
										if (r.getMarknum().intValue()<=0) {
											msg = "第"+ (rowNumOfSheet+1) +"行,第"+(cellNumOfRow+1)+"列资产已经全部入库!";
											this.outJsonError(msg);
											return SUCCESS;
										}
										if ("未确认".equals(r.getSstatus())) {
											msg = "第"+ (rowNumOfSheet+1) +"行,第"+(cellNumOfRow+1)+"列资产未审批,请先审批!";
											this.outJsonError(msg);
											return SUCCESS;
										}
									} else {
										msg = "第"+ (rowNumOfSheet+1) +"行,第"+(cellNumOfRow+1)+"列资产未登记,请先登记!";
										this.outJsonError(msg);
										return SUCCESS;
									}
								}
							}
						}
						list.add(rowNumOfSheet-2,o);
					}
				}
				stockinService.saveBatchStockin(list);
				t.commit();
				this.outJsonMessage( "成功入库"+count+"条数据!");
			}
		} catch (Exception e) {
			t.rollback();
			logger.error("添加资产批量入库信息失败!",e);
			this.outJsonError( "添加资产批量入库信息失败!");
		}
		return SUCCESS;
	}
	
	/**  
     * 得到Excel表中的值  
     *  
     * @param hssfCell  
     *            Excel中的每一个格子  
     * @return Excel中每一个格子中的值  
     */ 
     public static String getCellValue(HSSFCell cell) {  
        if ((cell == null) || (HSSFCell.CELL_TYPE_BLANK == cell.getCellType())) {  
            return "";  
        } 
//        else if (HSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {  
//            return String.valueOf(cell.getBooleanCellValue());  
//        } else if (HSSFCell.CELL_TYPE_FORMULA == cell.getCellType()) {  
//            return cell.getCellFormula();  
//        } 
        else if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {  //只适用于整数
	        	int cellValue_int = (int)cell.getNumericCellValue();
	        	String cellValue=String.valueOf(cellValue_int);
                return cellValue;  
        } else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType()) {  
            return String.valueOf(cell.getStringCellValue());  
        } else {  
            return String.valueOf(cell.getStringCellValue());  
        }  
  
    }
	
	/**
	 * 校验导入数据的有效性
	 * @param str
	 * @param colnum
	 */
	public String CheckImport(String str,int colnum,int rowNumOfSheet){
		String msg = "";
		if(colnum==0){
			if (!lessThan(str,30)) {
				msg = "第"+ (rowNumOfSheet+1) +"行资产编号的最大长度不应超过30个字符!";
				return msg;
			}
			if(str.equals("")){
				msg = "第"+ (rowNumOfSheet+1) +"行,第"+(colnum+1)+"列不能为空!";
				return msg;
			}
		}
		if(colnum==1){
			if(str.equals(""))
				return "第"+ (rowNumOfSheet+1) +"行,第"+(colnum+1)+"列不能为空!";
		}
		return "";
	}

你可能感兴趣的:(源码)