读取Excel表格(数据为树形)

Excel数据导入与树形结构处理
本文介绍了一种从Excel文件中读取数据并将其转换为树形结构的方法。该方法通过递归地处理每一行数据来构建层级关系,并最终将数据插入数据库。文章详细解释了如何使用Java实现这一过程。

数据的代码按树形结构编码,每行数据有三列,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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值