欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java操作excel的方法

程序员文章站 2024-03-05 16:36:31
本文实例讲述了java操作excel的方法。分享给大家供大家参考。具体如下: writableworkbook workbook = workbook.creat...

本文实例讲述了java操作excel的方法。分享给大家供大家参考。具体如下:

writableworkbook workbook = workbook.createworkbook(new file("d:\\output.xls")); 
 writablesheet sheet = workbook.createsheet("项目简报", 0); 
 //样式 
 writablefont sonti18font = new writablefont(writablefont.createfont("宋体"), 18,writablefont.bold); 
 writablefont sonti12font = new writablefont(writablefont.createfont("宋体"), 12,writablefont.bold); 
 writablefont sonti12fontnobold = new writablefont(writablefont.createfont("宋体"), 12,writablefont.no_bold); 
 writablecellformat font18subject = new writablecellformat (sonti18font); 
 writablecellformat font12subject = new writablecellformat (sonti12font); 
 writablecellformat font12subjectnobold = new writablecellformat (sonti12fontnobold); 
 font18subject.setalignment(jxl.format.alignment.centre); 
 font18subject.setverticalalignment(jxl.format.verticalalignment.centre);  
 font18subject.setborder(jxl.format.border.all,jxl.format.borderlinestyle.thin);  
 font12subject.setalignment(jxl.format.alignment.left); 
 font12subject.setverticalalignment(jxl.format.verticalalignment.centre);  
 font12subject.setborder(jxl.format.border.all,jxl.format.borderlinestyle.thin); 
 font12subjectnobold.setalignment(jxl.format.alignment.left); 
 font12subjectnobold.setverticalalignment(jxl.format.verticalalignment.centre);  
 font12subjectnobold.setborder(jxl.format.border.all,jxl.format.borderlinestyle.thin); 
 //样式 end 
 //项目简介信息 
 sheet.addcell(new label(0, 0, projreportparam.getproj_name() + "项目简报",font18subject)); 
 sheet.mergecells(0, 0, 5, 0); 
 sheet.addcell(new label(0,1,"项目编号",font12subject)); 
 sheet.addcell(new label(1,1,projreportparam.getproj_code(),font12subjectnobold)); 
 sheet.addcell(new label(2,1,"项目经理",font12subject)); 
 sheet.addcell(new label(3,1,projreportparam.getemp_name(),font12subjectnobold)); 
 sheet.addcell(new label(4,1,"立项部门",font12subject)); 
 sheet.addcell(new label(5,1,projreportparam.getdept_name(),font12subjectnobold)); 
 sheet.addcell(new label(0,2,"计划发生成本",font12subject)); 
 sheet.addcell(new label(1,2,projreportparam.getplan_cost(),font12subjectnobold)); 
 sheet.addcell(new label(2,2,"实际发生成本",font12subject)); 
 sheet.addcell(new label(3,2,projreportparam.getactual_cost(),font12subjectnobold)); 
 sheet.addcell(new label(0,3,"项目简介",font12subject)); 
 sheet.addcell(new label(1,3,projreportparam.getproj_desc(),font12subjectnobold)); 
 sheet.mergecells(1, 3, 5, 3); 
 //项目简介信息 end 
 //里程碑 
 sheet.addcell(new label(0,5,"里程碑",font18subject)); 
 sheet.mergecells(0, 5, 5, 5); 
 sheet.addcell(new label(0,6,"阶段名称",font12subject)); 
 sheet.addcell(new label(1,6,"任务名称",font12subject)); 
 sheet.addcell(new label(2,6,"实施人",font12subject)); 
 sheet.addcell(new label(3,6,"计划起止时间",font12subject)); 
 sheet.addcell(new label(4,6,"实际起止时间",font12subject)); 
 sheet.addcell(new label(5,6,"状态",font12subject)); 
 int i = 7;//行 
 for(projreportparamminfo projreportparamminfo : projreportparam.getprojreportparamminfolist()){ 
  sheet.addcell(new label(0,i,projreportparamminfo.getpharse_name(),font12subjectnobold)); 
  sheet.addcell(new label(1,i,projreportparamminfo.gettask_item(),font12subjectnobold)); 
  sheet.addcell(new label(2,i,projreportparamminfo.getemp_name(),font12subjectnobold)); 
  sheet.addcell(new label(3,i,utils.formatdate(projreportparamminfo.gettask_plan_sd()) + " / " + utils.formatdate(projreportparamminfo.gettask_plan_fd()),font12subjectnobold)); 
  sheet.addcell(new label(4,i,utils.formatdate(projreportparamminfo.gettask_actual_sd()) + " / " + utils.formatdate(projreportparamminfo.gettask_actual_fd()),font12subjectnobold)); 
  sheet.addcell(new label(5,i,projreportparamminfo.gettask_state(),font12subjectnobold)); 
  i++; 
 } 
 //里程碑 end 
 //项目组成员及干系人 
 int j = 1+i;//行 
 sheet.addcell(new label(0,j,"项目组成员及干系人",font18subject)); 
 sheet.mergecells(0, j, 5, j); 
 sheet.addcell(new label(0,j+1,"序号",font12subject)); 
 sheet.addcell(new label(1,j+1,"姓名",font12subject)); 
 sheet.addcell(new label(2,j+1,"角色",font12subject)); 
 sheet.addcell(new label(3,j+1,"进入项目组时间",font12subject)); 
 sheet.addcell(new label(4,j+1,"离开项目组时间",font12subject)); 
 sheet.addcell(new label(5,j+1,"累计工时",font12subject)); 
 int m = 1;//序号 
 int n = j+2;//行 
 for(projreportparamhrinfo projreportparamhrinfo : projreportparam.getprojreportparamhrinfolist()){ 
  sheet.addcell(new label(0,n,m+"",font12subjectnobold)); 
  sheet.addcell(new label(1,n,projreportparamhrinfo.getemp_name(),font12subjectnobold)); 
  sheet.addcell(new label(2,n,projreportparamhrinfo.getrole_name(),font12subjectnobold)); 
  sheet.addcell(new label(3,n,utils.formatdate(projreportparamhrinfo.gethr_start_date()),font12subjectnobold)); 
  sheet.addcell(new label(4,n,utils.formatdate(projreportparamhrinfo.gethr_release_date()),font12subjectnobold)); 
  sheet.addcell(new label(5,n,projreportparamhrinfo.gettotal_manhour(),font12subjectnobold)); 
  m++; 
  n++; 
 } 
 //项目组成员及干系人 end 
 //本期工作内容 
 int k = n + 1;//行 
 sheet.addcell(new label(0,k,"本期工作内容("+startdate+"至"+enddate+")",font18subject)); 
 sheet.mergecells(0, k, 6, k); 
 sheet.addcell(new label(0,k+1,"任务编号",font12subject)); 
 sheet.addcell(new label(1,k+1,"任务名称",font12subject)); 
 sheet.addcell(new label(2,k+1,"计划起止时间",font12subject)); 
 sheet.addcell(new label(3,k+1,"实际起止时间",font12subject)); 
 sheet.addcell(new label(4,k+1,"计划工时",font12subject)); 
 sheet.addcell(new label(5,k+1,"实际工时",font12subject)); 
 sheet.addcell(new label(6,k+1,"实施人员",font12subject)); 
 int p = k + 2;//行 
 for(projreportparamtaskinfo projreportparamtaskinfo : projreportparam.getthisprojreportparamtaskinfolist()){ 
  sheet.addcell(new label(0,p,projreportparamtaskinfo.gettask_code(),font12subjectnobold)); 
  sheet.addcell(new label(1,p,projreportparamtaskinfo.gettask_item(),font12subjectnobold)); 
  sheet.addcell(new label(2,p,utils.formatdate(projreportparamtaskinfo.gettask_plan_sd()) + " / " + utils.formatdate(projreportparamtaskinfo.gettask_plan_fd()),font12subjectnobold)); 
  sheet.addcell(new label(3,p,utils.formatdate(projreportparamtaskinfo.gettask_actual_sd()) + " / " + utils.formatdate(projreportparamtaskinfo.gettask_actual_fd()),font12subjectnobold)); 
  sheet.addcell(new label(4,p,projreportparamtaskinfo.gettask_plan_manhour(),font12subjectnobold)); 
  sheet.addcell(new label(5,p,projreportparamtaskinfo.gettask_actual_manhour(),font12subjectnobold)); 
  sheet.addcell(new label(6,p,projreportparamtaskinfo.getplan_emp_name(),font12subjectnobold)); 
  p++; 
 } 
 //本期工作内容 end 
 //下周工作内容 
 int q = p + 1;//行 
 sheet.addcell(new label(0,q,"下周工作内容",font18subject)); 
 sheet.mergecells(0, q, 5, q); 
 sheet.addcell(new label(0,q+1,"任务编号",font12subject)); 
 sheet.addcell(new label(1,q+1,"任务名称",font12subject)); 
 sheet.addcell(new label(2,q+1,"计划开始时间",font12subject)); 
 sheet.addcell(new label(3,q+1,"计划结束时间",font12subject)); 
 sheet.addcell(new label(4,q+1,"计划工时",font12subject)); 
 sheet.addcell(new label(5,q+1,"实施人员",font12subject)); 
 int r = q + 2; 
 for(projreportparamtaskinfo projreportparamtaskinfo : projreportparam.getnextprojreportparamtaskinfolist()){ 
  sheet.addcell(new label(0,r,projreportparamtaskinfo.gettask_code(),font12subjectnobold)); 
  sheet.addcell(new label(1,r,projreportparamtaskinfo.gettask_item(),font12subjectnobold)); 
  sheet.addcell(new label(2,r,utils.formatdate(projreportparamtaskinfo.gettask_plan_sd()),font12subjectnobold)); 
  sheet.addcell(new label(3,r,utils.formatdate(projreportparamtaskinfo.gettask_plan_fd()),font12subjectnobold)); 
  sheet.addcell(new label(4,r,projreportparamtaskinfo.gettask_plan_manhour(),font12subjectnobold)); 
  sheet.addcell(new label(5,r,projreportparamtaskinfo.getplan_emp_name(),font12subjectnobold)); 
  r++; 
 } 
 //下周工作内容 end 
 //项目存在的问题及解决 
 int s = r + 1;//行 
 sheet.addcell(new label(0,s,"项目存在的问题及解决",font18subject)); 
 sheet.mergecells(0, s, 8, s); 
 sheet.addcell(new label(0,s+1,"识别日期",font12subject)); 
 sheet.addcell(new label(1,s+1,"问题描述",font12subject)); 
 sheet.addcell(new label(2,s+1,"所属风险",font12subject)); 
 sheet.addcell(new label(3,s+1,"解决方法",font12subject)); 
 sheet.addcell(new label(4,s+1,"解决人",font12subject)); 
 sheet.addcell(new label(5,s+1,"计划开始时间",font12subject)); 
 sheet.addcell(new label(6,s+1,"计划完成时间",font12subject)); 
 sheet.addcell(new label(7,s+1,"问题状态",font12subject)); 
 sheet.addcell(new label(8,s+1,"总结",font12subject)); 
 int t = s + 2;//行 
 for(projreportparamriskinfo projreportparamriskinfo : projreportparam.getprojreportparamriskinfolist()){ 
  sheet.addcell(new label(0,t,utils.formatdate(projreportparamriskinfo.getsts_date()),font12subjectnobold)); 
  sheet.addcell(new label(1,t,projreportparamriskinfo.getproj_issue_item(),font12subjectnobold)); 
  sheet.addcell(new label(2,t,projreportparamriskinfo.getproj_risk_item(),font12subjectnobold)); 
  sheet.addcell(new label(3,t,projreportparamriskinfo.getissue_resolve_method(),font12subjectnobold)); 
  sheet.addcell(new label(4,t,projreportparamriskinfo.getissue_resolve_emp(),font12subjectnobold)); 
  sheet.addcell(new label(5,t,utils.formatdate(projreportparamriskinfo.getissue_plan_date()),font12subjectnobold)); 
  sheet.addcell(new label(6,t,utils.formatdate(projreportparamriskinfo.getissue_actual_date()),font12subjectnobold)); 
  sheet.addcell(new label(7,t,projreportparamriskinfo.getissue_state(),font12subjectnobold)); 
  sheet.addcell(new label(8,t,projreportparamriskinfo.getissue_summarize(),font12subjectnobold)); 
  t++; 
 } 
 //项目存在的问题及解决 end 
 workbook.write(); 
 workbook.close(); 
//生成项目简报的程序

希望本文所述对大家的java程序设计有所帮助。