java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java操作excel

java操作excel的方法

作者:恺哥

这篇文章主要介绍了java操作excel的方法,实例分析了java针对excel文件的读写、打开、保存等操作技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

您可能感兴趣的文章:
阅读全文