`
cleaneyes
  • 浏览: 336385 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

    java把excel表的数据导入到mysql数据库

    用java把excel表的数据导入到数据库(可能有些类型还没考虑完全,抽空写的) 简单的思路 ... 二、读取excel文件的数据(注意:excel文件的数据表字段的顺序与该表的顺序一致,有空再做导出) 三、拼接sql

    Visual C#.NET 2008程序设计案例集锦 (源码)

    案例1.12 以树形显示的窗体 案例1.13 图案动画窗体 本章小结 第2章 图形图像处理技术 案例2.1 波形图特效 案例2.2 在图像中写入文字 案例2.3 图像缩放与翻转 .案例2.4 图像的涂沫 案例2.5 百叶窗图像效果 ...

    Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3

    实例045 用数组设置JTable表格的列名与列宽 55 3.2 数组操作 57 实例046 数组的下标界限 57 实例047 按钮控件数组实现计数器界面 58 实例048 复选框控件数组 59 实例049 用数组反转字符串 60 3.3 数组排序与查询 61 ...

    易语言程序免安装版下载

    修改扩展界面支持库一,为“树形框”增加多态检查框功能,相应地添加了多个与检查框相关的属性、方法和事件。 17. 修改高级表格支持库,允许“复制选定文本()”“剪切选定文本()”在“允许选择块”属性为假时复制...

    C#程序开发范例宝典(第2版).part08

    实例020 以树形显示的程序界面 20 实例021 动态按钮的窗体界面 21 1.6 特殊形状的窗体 23 实例022 非矩形窗体 23 实例023 建立字体形状窗体 24 实例024 控件随窗体自动调整 25 实例025 带分隔栏的窗体 25 ...

    C#程序开发范例宝典(第2版).part13

    实例020 以树形显示的程序界面 20 实例021 动态按钮的窗体界面 21 1.6 特殊形状的窗体 23 实例022 非矩形窗体 23 实例023 建立字体形状窗体 24 实例024 控件随窗体自动调整 25 实例025 带分隔栏的窗体 25 ...

    C#程序开发范例宝典(第2版).part02

    实例020 以树形显示的程序界面 20 实例021 动态按钮的窗体界面 21 1.6 特殊形状的窗体 23 实例022 非矩形窗体 23 实例023 建立字体形状窗体 24 实例024 控件随窗体自动调整 25 实例025 带分隔栏的窗体 25 ...

    C#程序开发范例宝典(第2版).part12

    实例020 以树形显示的程序界面 20 实例021 动态按钮的窗体界面 21 1.6 特殊形状的窗体 23 实例022 非矩形窗体 23 实例023 建立字体形状窗体 24 实例024 控件随窗体自动调整 25 实例025 带分隔栏的窗体 25 ...

Global site tag (gtag.js) - Google Analytics