Excel 표 읽 기 (데 이 터 는 트 리)
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Spring에서 DAO가 순환 호출될 때 데이터가 실시간으로 업데이트되지 않는 해결 방법문제를 설명하기 전에 몇 가지 전제 사항을 설명하십시오. Spring의 구성 파일에서 다음과 같은 방식으로 데이터베이스 트랜잭션을 구성했다고 가정하십시오. 현재 UserDao 및 Security Service가 있습...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.