java导入Excel

写道
public List<String> importFlowprojectData(String filePath) throws BiffException, IOException{
FlowProjectMgr mgr = (FlowProjectMgr) SpringBeanFactory.getBean("flowprojectMgr_new");
FlowProjectDao dao = (FlowProjectDao) SpringBeanFactory.getBean("flowprojectDao_new");
List<String> errList=new ArrayList<String>();
InputStream is = null;
jxl.Workbook rwb =null;
Integer flowProjectId=null;
List list=null;
try {
//1 从Excel文件读取数据表
//Java Excel API既可以从本地文件系统的一个文件(.xls),也可以从输入流中读取Excel数据表。
//读取Excel数据表的第一步是创建Workbook(术语:工作薄),下面的代码片段举例说明了应该如何操作:
//(完整代码见ExcelReading.java)
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
//一旦创建了Workbook,我们就可以通过它来访问Excel Sheet(术语:工作表)。参考下面的代码片段:
//获取第一张Sheet表
is = new FileInputStream(filePath);
rwb = Workbook.getWorkbook(is);
Sheet rs = (Sheet) rwb.getSheet(0);

//我们既可能通过Sheet的名称来访问它,也可以通过下标来访问它。如果通过下标来访问的话,
//要注意的一点是下标从0开始,就像数组一样。
//一旦得到了Sheet,我们就可以通过它来访问Excel Cell(术语:单元格)。参考下面的代码片段:
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
FlowProjectBaseinfo info=null;
int rowCount=rs.getRows();
for(int i=3;i<rowCount;i++){
try{
Cell c0 = ((jxl.Sheet) rs).getCell(0, i);
String flowProjectIndex = c0.getContents();
Cell c1 = ((jxl.Sheet) rs).getCell(1, i);
String flowProjectYear = c1.getContents();
if(flowProjectIndex!=null&&flowProjectIndex.length()>0&&flowProjectYear!=null&&flowProjectYear.length()>0){

list= mgr.findByHql("from FlowProjectBaseinfo f where f.flowProjectYear='"+flowProjectYear+"' and f.flowProjectIndex='"+flowProjectIndex+"'");
if(list!=null&&list.size()>0){
info=(FlowProjectBaseinfo) list.get(0);
info.setFlowProjectEffectflag(1);
}else{
info=new FlowProjectBaseinfo();
info.setFlowProjectIndex(flowProjectIndex);//项目索引
info.setFlowProjectYear(flowProjectYear);//项目年度
info.setFlowProjectEffectflag(1);
info.setFlowProjectCreatetime(new Timestamp(new Date().getTime()));
SysUserInfo sysUserInfo=(SysUserInfo) DoradoContext.getContext().getAttribute("sysUserInfo");
if(sysUserInfo!=null){
info.setFlowProjectCreater(sysUserInfo.getSysUserId());
}
}
Cell c2 = ((jxl.Sheet) rs).getCell(2, i);
String flowProjectName = c2.getContents();
info.setFlowProjectName(flowProjectName);

Cell c3 = ((jxl.Sheet) rs).getCell(3, i);
String deptIds = c3.getContents();//主办业务部门
if(deptIds!=null&&deptIds.length()>0){
String [] s= deptIds.split(",");
for(int j=0;j<s.length;j++){
List o=dao.getDeptBaseInfo(" and sysDeptUpid = 2194 and sysDeptActiveflag = 1 and sysDepttypeId = 20 and sysDeptdegreeId = 4 and sysDeptId <> 24 and sysDeptOther3 = 1 and sysDeptName='"+s[j]+"'");
if(o!=null&&o.size()>0){
SysDeptInfo dept=(SysDeptInfo)o.get(0);
info.setSysOrgId(dept.getSysDeptId());
}
}
}
Cell c4 = ((jxl.Sheet) rs).getCell(4, i);
String flowProjectManagerName = c4.getContents();
info.setFlowProjectManagerName(flowProjectManagerName);
Cell c7 = ((jxl.Sheet) rs).getCell(7, i);
String flowProjectAcceptName = c7.getContents();//委托开发中心开发
if(flowProjectAcceptName!=null&&flowProjectAcceptName.length()>0){
Object o= mgr.getIdByName("FlowProjectAcceptMap", "flowProjectAcceptdesc", flowProjectAcceptName);
if(o!=null){
FlowProjectAcceptMap a=(FlowProjectAcceptMap)o;
info.setFlowProjectAcceptid(a.getFlowProjectAcceptid());
}
}
Cell c8 = ((jxl.Sheet) rs).getCell(8, i);
String projectStartData = c8.getContents();//项目启动时间
if(projectStartData!=null&&projectStartData.length()>0){
Date start =sdf.parse(projectStartData);
info.setFlowProjectStartdate(new Timestamp(start.getTime()));
}

Cell c9 = ((jxl.Sheet) rs).getCell(9, i);//需求确定时间
String needTime = c9.getContents();
if(needTime!=null&&needTime.length()>0){
Date flowProjectRequirementdate=sdf.parse(needTime);
info.setFlowProjectRequirementdate(new Timestamp(flowProjectRequirementdate.getTime()));
}

Cell c10 = ((jxl.Sheet) rs).getCell(10, i);//预计投产时间
String useTime = c10.getContents();
if(useTime!=null&&useTime.length()>0){
Date userDate=sdf.parse(useTime);
info.setFlowProjectUsedate(new Timestamp(userDate.getTime()));
}
Cell c11 = ((jxl.Sheet) rs).getCell(11, i);
String range = c11.getContents();
if(range!=null&&range.length()>0){//适用范围
Object rangeMap= mgr.getIdByName("FlowProjectRangeMap","rangeName" , range);
if(rangeMap!=null){
FlowProjectRangeMap r=(FlowProjectRangeMap)rangeMap;
info.setFlowProjectRange(r.getRangId().toString());
}

}

Cell c112 = ((jxl.Sheet) rs).getCell(12, i);
String categoryName = c112.getContents();//项目类别
if(categoryName!=null&&categoryName.length()>0){
Object c=mgr.getIdByName("FlowProjectCategoryMap", "categoryName", categoryName);
if(c!=null){
FlowProjectCategoryMap f=(FlowProjectCategoryMap)c;
info.setFlowProjectCategoryId(f.getCategoryId());
}
}
Cell c13 = ((jxl.Sheet) rs).getCell(13, i);
String sequence = c13.getContents();
if(sequence!=null&&sequence.length()>0){
info.setFlowProjectSequence(Integer.parseInt(sequence));//项目优先级
}
Cell c14 = ((jxl.Sheet) rs).getCell(14, i);
String flowProjectDetail = c14.getContents();
info.setFlowProjectDetail(flowProjectDetail);//项目描述

Cell c15 = ((jxl.Sheet) rs).getCell(15, i);
String flowBenefitMemo = c15.getContents();
info.setFlowBenefitMemo(flowBenefitMemo); //经济效益

Cell c16 = ((jxl.Sheet) rs).getCell(16, i);//社会效益
String flowBenefitSociety = c16.getContents();
info.setFlowBenefitSociety(flowBenefitSociety);

Cell c17 = ((jxl.Sheet) rs).getCell(17, i);//后评估方法描述
String flowProjectAppraise = c17.getContents();
info.setFlowProjectAppraise(flowProjectAppraise);
SysUserInfo sysUserInfo= (SysUserInfo) DoradoContext.getContext().getAttribute("sysUserInfo");
//保存相关表中得数据
if(sysUserInfo!=null){
info.setFlowProjectCreater(sysUserInfo.getSysUserId());
}
info.setFlowStateId(Constant.I_PROJECT_STATE_TEMPORARY);//设置项目状态为暂存
flowProjectId= info.getFlowProjectId();
if(flowProjectId==null||flowProjectId==0){
Object o=dao.save(info);
flowProjectId=Integer.valueOf(o.toString());
}else{
dao.update(info);
}
Cell c5 = ((jxl.Sheet) rs).getCell(5, i);
String assIds = c5.getContents();//协办部门
if(assIds!=null&&assIds.length()>0){
String [] s= assIds.split(",");
for(int j=0;j<s.length;j++){
List o=dao.getDeptBaseInfo(" and sysDeptUpid = 2194 and sysDeptActiveflag = 1 and sysDepttypeId = 20 and sysDeptdegreeId = 4 and sysDeptId <> 24 and sysDeptOther3 = 1 and sysDeptName='"+s[j]+"'");
if(o!=null&&o.size()>0){
SysDeptInfo dept=(SysDeptInfo)o.get(0);
FlowProjectBenefitinfo ab=new FlowProjectBenefitinfo();
List lists= dao.findByHql("from FlowProjectBenefitinfo f where f.sysOrgId='"+dept.getSysDeptId()+"' and f.flowProjectId='"+flowProjectId+"'");
if(lists!=null&&lists.size()>0){
ab=(FlowProjectBenefitinfo) lists.get(0);
ab.setFlowProjectEffectflag(1);
dao.update(ab);
}else{
ab.setFlowProjectId(flowProjectId);
ab.setSysOrgId(dept.getSysDeptId());
ab.setFlowProjectEffectflag(1);
dao.save(ab);
}
}
}
}
Cell c6 = ((jxl.Sheet) rs).getCell(6, i);
String benefitIds = c6.getContents();//受益部门
if(benefitIds!=null&&benefitIds.length()>0){
String [] s= benefitIds.split(",");
for(int j=0;j<s.length;j++){
String name= s[j];
List o=dao.getDeptBaseInfo(" and sysDeptUpid = 2194 and sysDeptActiveflag = 1 and sysDepttypeId = 20 and sysDeptdegreeId = 4 and sysDeptId <> 24 and sysDeptOther3 = 1 and sysDeptName='"+name+"'");
if(o!=null&&o.size()>0){
SysDeptInfo dept=(SysDeptInfo)o.get(0);
FlowProjectAssistinfo fa=new FlowProjectAssistinfo();
fa.setFlowProjectId(flowProjectId);
fa.setFlowProjectEffectflag(1);
fa.setSysOrgId(dept.getSysDeptId());
List lists= dao.findByHql("from FlowProjectAssistinfo f where f.sysOrgId='"+dept.getSysDeptId()+"' and f.flowProjectId='"+flowProjectId+"'");
if(lists!=null&&lists.size()>0){
FlowProjectAssistinfo temp=fa;
fa=(FlowProjectAssistinfo) lists.get(0);
fa.setFlowProjectEffectflag(1);
fa.setFlowProjectId(temp.getFlowProjectId());
fa.setSysOrgId(temp.getSysOrgId());
dao.update(fa);
}else{
Integer sysOrgId= fa.getSysOrgId();//主办部门不能为空
if(sysOrgId!=null&&sysOrgId!=0&&sysOrgId!=-1){
dao.save(fa);
}else{
errList.add("第"+i+"行数据出错了");
}

}
}
}
}
}else{
errList.add("第"+i+"行数据出错了");
}
}catch (Exception e) {
errList.add("第"+i+"行数据出错了");
e.printStackTrace();
// TODO: handle exception
}
}
}catch (BiffException e) {
errList.add("导入出错");
e.printStackTrace();
} catch (IOException e) {
errList.add("导入出错");
e.printStackTrace();
}finally{
rwb.close();
is.close();
}
return errList;
}

 

你可能感兴趣的:(Excel)