读取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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript可以使用一些库和工具来读取Excel表格数据。其中最常用的是使用`SheetJS`库,它可以帮助我们读取和处理Excel文件。 首先,我们需要引入`SheetJS`库。可以通过在HTML文件中添加以下代码来引入这个库: ``` <script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script> ``` 接下来,我们可以编写JavaScript代码来读取Excel表格数据。首先,我们需要选择并上传一个Excel文件: ```javascript var fileInput = document.getElementById('fileInput'); var workbook; fileInput.addEventListener('change', function(e) { var file = e.target.files[0]; var reader = new FileReader(); reader.onload = function(e) { var data = new Uint8Array(e.target.result); workbook = XLSX.read(data, { type: 'array' }); }; reader.readAsArrayBuffer(file); }); ``` 上述代码将会在文件选择框的`change`事件发生时触发。一旦读取完成,我们将获取到一个`workbook`对象,它包含了Excel文件的所有信息。 然后,我们可以使用`SheetJS`库提供的API来读取具体的单元格数据。可以通过以下代码来读取第一个Sheet的A1单元格的数据: ```javascript var worksheet = workbook.Sheets[workbook.SheetNames[0]]; var cellValue = worksheet.A1.v; ``` 通过上述代码,我们可以读取Excel表格中各个单元格的数据。可以根据需要进行循环遍历和处理。 需要注意的是,由于安全性的限制,JavaScript无法直接访问本地计算机文件系统上的文件,因此需要用户手动选择并上传文件。同时,由于使用了第三方库,需要保证可以访问到这些库的链接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值