服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java操作excel的方法

java操作excel的方法

2019-12-26 13:33恺哥 JAVA教程

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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程序设计有所帮助。

延伸 · 阅读

精彩推荐