Excel 표 읽 기 (데 이 터 는 트 리)

데이터 의 코드 는 트 리 구조 에 따라 인 코딩 됩 니 다. 각 줄 의 데 이 터 는 세 열 이 있 습 니 다. code, name, description 은 여러 줄 을 차지 할 수 있 습 니 다. 즉, name 과 description 은 여러 줄 을 차지 할 수 있 지만 code 는 한 줄 만 차지 합 니 다.
ResouceClassDao dao = new ResouceClassDaoImpl();

	public RowObj readSheetAndInsert(Sheet st, int typeId)
			throws BiffException, IOException {
		int rows = st.getRows();

		Stack<RowObj> parentObjs = new Stack<RowObj>();
		RowObj curParentObj = new RowObj();

		int count = 0;
		//   excel  
		for (int i = 0; i < rows; i++) {
			Cell cell = st.getCell(0, i);
			String code = cell.getContents().trim();

			if (code != null && code.length() > 0) {
				cell = st.getCell(1, i);
				String name = cell.getContents().trim();

				cell = st.getCell(2, i);
				String description = cell.getContents().trim();

				RowObj obj = new RowObj();
				obj.setTypeId(typeId);
				obj.setCode(code);
				obj.setName(name);
				obj.setDescription(description);
				count++;

				String parentCode = curParentObj.getCode();
				System.out.print(parentCode + "|" + code);
				while (parentCode != null && code.indexOf(parentCode) < 0) {
					curParentObj = parentObjs.pop();
					parentCode = curParentObj.getCode();
				}
				System.out.println("      *" + parentCode + "|" + code);

				curParentObj.getSubObjs().add(obj);
				parentObjs.push(curParentObj);
				curParentObj = obj;
			} else {
				cell = st.getCell(1, i);
				String name = cell.getContents().trim();
				curParentObj.setName(curParentObj.getName() + name);

				cell = st.getCell(2, i);
				String description = cell.getContents().trim();
				curParentObj.setDescription(curParentObj.getDescription()
						+ description);
			}
		}

		System.out.println("count=" + count);

		return parentObjs.firstElement();
	}

	// excel  
	public void importExcel(String fileName) throws BiffException, IOException {
		InputStream is = new FileInputStream(fileName);
		//
		Workbook wb = null;
		wb = Workbook.getWorkbook(is);

		Sheet[] sheets = wb.getSheets();
		TransactionManager transactionManager = new TransactionManager();
		try {
			transactionManager.begin();

			for (int k = 0; k < sheets.length; k++) {
				RowObj rootObj = readSheetAndInsert(sheets[k], k + 1);
				insertData(rootObj, 0);
			}

			transactionManager.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				transactionManager.rollback();
			} catch (RollbackException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			throw new RuntimeException(e);
		}
	}

	public void insertData(RowObj obj, int level) throws Exception {
		ResouceClassBean bean = new ResouceClassBean();

		String space = "";
		for (int i = 0; i < level; i++) {
			space = space + "  ";
		}
		if (obj.getCode() != null) {
			System.out.println(space + obj.getCode() + "|" + obj.getName()
					+ "|" + obj.getDescription());
			bean.setTct_id(obj.getTypeId());
			bean.setTctc_code(obj.getCode());
			bean.setTctc_name(obj.getName());
			bean.setTctc_brief_name(bean.getTctc_name());
			bean.setTctc_remark(obj.getDescription());
			bean.setTctc_parent_id(obj.getParentId());

			dao.insert(bean);
		}

		level++;
		List<RowObj> subObjs = obj.getSubObjs();
		if (subObjs != null && subObjs.size() > 0) {
			for (RowObj rowObj : subObjs) {
				rowObj.setParentId(bean.getTctc_id());
				insertData(rowObj, level);
			}
		}
	}

	private class RowObj {
		private String code;
		private String name;
		private String description;
		private String parentId;

		public String getParentId() {
			return parentId;
		}

		public void setParentId(String parentId) {
			this.parentId = parentId;
		}

		private int typeId;

		public int getTypeId() {
			return typeId;
		}

		public void setTypeId(int typeId) {
			this.typeId = typeId;
		}

		private List<RowObj> subObjs = new ArrayList<RowObj>();

		public List<RowObj> getSubObjs() {
			return subObjs;
		}

		public void setSubObjs(List<RowObj> subObjs) {
			this.subObjs = subObjs;
		}

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}
	}

 
f

좋은 웹페이지 즐겨찾기