本文實例講述了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程序設計有所幫助。