导入导出--jxl.jar,关于模板可以用也可以不用,看具体需求,如果有较多的固定内容可以用,固定内容少可以不用
1 public static void excelOutput(final String templePath,final List dataList, final List staffCdList,final String path,final String fileNam) {
2 try {
3 // テンポレトの位置
4 String url = templePath;
5 // テンポレトを取得
6 POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(url));//url为excel模板放的地址
7 // 新規Workbook
8 HSSFWorkbook wb = new HSSFWorkbook(fs);
9 // スタッフCd数による シートを新規
10 for (int i = 0; i < staffCdList.size() - 1; i++) {
11 // コピ第一シート
12 wb.cloneSheet(0);
13 }
14 HSSFRichTextString hs = new HSSFRichTextString();
15 // 時間格式
16 SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
17 Date date = new Date();
18 // スタッフデータvo
19 HakenDaityouVO staffVo = new HakenDaityouVO();
20 // excelへデータを出力する
21 for (int i = 0; i < staffCdList.size(); i++) {
22 staffVo = (HakenDaityouVO) staffCdList.get(i);
23 // スタッフCd
24 String staffCd = staffVo.getStaffCd().trim();
25 // シート名を設定
26 wb.setSheetName(i, staffCd);//sheet的名字
27 // シートを取得
28 HSSFSheet sheet = wb.getSheetAt(i);
29 if(i==0){
30 sheet.setSelected(true);
31 } else{
32 sheet.setSelected(false);
33 }
34 // シートの第3行を取得
35 HSSFRow row = sheet.getRow(3);
36 // 第3行の第2列を取得
37 HSSFCell cell = row.getCell((short) 2);
38 // 派遣元事業所名
39 String hmjigyoNam = staffVo.getHmjigyoNam();
40 hmjigyoNam = setString(hmjigyoNam);
41 hs = new HSSFRichTextString(hmjigyoNam);
42 // 設定第3行第2列の内容は派遣元事業所名
43 cell.setCellValue(hs);
44
45 // 第3行の第4列を取得
46 cell = row.getCell((short) 4);
47 // 派遣元事業所住所
48 String hmjigyouAddr = staffVo.getHmjigyouAddr();
49 hmjigyouAddr = setString(hmjigyouAddr);
50 hs = new HSSFRichTextString(hmjigyouAddr);
51 // 設定第4行第3列の内容は派遣元事業所住所
52 cell.setCellValue(hs);
53
54 // 第3行の第5列を取得
55 cell = row.getCell((short) 5);
56 String hmjigyoTel = staffVo.getHmjigyoTel();
57 hmjigyoTel = setString(hmjigyoTel);
58 if (hmjigyoTel.equals(" ")) {
59 hmjigyoTel = " ";
60 }
61 // 派遣元事業所FAX
62 String hmjigyoFax = staffVo.getHmjigyoFax();
63 hmjigyoFax = setString(hmjigyoFax);
64 StringBuffer buffer = new StringBuffer();
65 buffer.append(" TEL: " + hmjigyoTel + " ");
66 buffer.append("FAX: " + hmjigyoFax);
67 hs = new HSSFRichTextString(buffer.toString());
68 // 設定第3行第5列の内容は派遣元事業所TEL FAX
69 cell.setCellValue(hs);
70
71 // シートの第4行を取得
72 row = sheet.getRow(4);
73 // 第4行の第2列を取得
74 cell = row.getCell((short) 2);
75 // スタッフ登録店名
76 String staffTenNam = staffVo.getStaffTenNam();
77 staffTenNam = setString(staffTenNam);
78 hs = new HSSFRichTextString(staffTenNam);
79 // 設定第4行第2列の内容はスタッフ登録店名
80 cell.setCellValue(hs);
81
82 // 第4行の第4列を取得
83 cell = row.getCell((short) 4);
84 // スタッフCD,派遣社員名
85 String staffNam = staffVo.getStaffNam();
86 staffNam = setString(staffNam);
87 String staff = staffCd + " " + staffNam;
88 hs = new HSSFRichTextString(staff);
89 // 設定第4行第4列の内容はスタッフCD,派遣社員名
90 cell.setCellValue(hs);
91 // 同じなスタッフの記録数
92 int cnt = 0;
93 for (int j = 0; j < dataList.size(); j++) {
94 // Create a cell format for Arial 10 point font
95 staffVo = (HakenDaityouVO) dataList.get(j);
96 String staffCd1 = staffVo.getStaffCd();
97 // 同じなスタッフ、記録数が不足30の場合、データを出力する
98 if (staffCd1.equals(staffCd) && cnt < 30) {
99 // 日付(”-” ⇒ ”/”に変更)
100 String ymd = staffVo.getYmd();
101 ymd = ymd.replace("-", "/");
102 ymd = setString(ymd);
103 // シートの第6 + 2 * cnt行を取得
104 row = sheet.getRow(6 + 2 * cnt);
105 // 第6 + 2 * cnt行の第0列を取得
106 cell = row.getCell((short) 0);
107 date = ft.parse(ymd);
108 // 設定第6 + 2 * cnt行第0列の内容は日付
109 cell.setCellValue(date);
110
111 // 作業開始時刻(秒単位は表示しない)
112 String startTime = staffVo.getStartTime();
113 startTime = setString(startTime);
114 if (startTime.length() >= 5) {
115 startTime = startTime.trim().substring(0, 5);
116 }
117 // 作業開始時刻(秒単位は表示しない)
118 String endTime = staffVo.getEndTime();
119 endTime = setString(endTime);
120 if (endTime.length() >= 5) {
121 endTime = endTime.trim().substring(0, 5);
122 }
123 String time = " ";
124 // 作業開始時刻と作業開始時刻がnull の場合
125 if (!(startTime.equals(" ") && endTime.equals(" "))) {
126 time = startTime + "/" + endTime;
127 }
128 hs = new HSSFRichTextString(time);
129 // 第6 + 2 * cnt行の第1列を取得
130 cell = row.getCell((short) 1);
131 // 設定第6 + 2 * cnt行第1列の内容は作業開始時刻/作業開始時刻
132 cell.setCellValue(hs);
133
134 // 休憩時間
135 String restTime = staffVo.getRestTime();
136 restTime = setString(restTime);
137 // 残業時間
138 String overTime = staffVo.getOverTime();
139 overTime = setString(overTime);
140 if(restTime.equals(" ")&& overTime.equals(" ")){
141 time = " ";
142 } else{
143 time = restTime + "/" + overTime;
144 }
145 hs = new HSSFRichTextString(time);
146 // 第6 + 2 * cnt行の第2列を取得
147 cell = row.getCell((short) 2);
148 // 設定第6 + 2 * cnt行第1列の内容は休憩時間/残業時間
149 cell.setCellValue(hs);
150
151 // 事業所(業務)名称
152 String gyoumNam = staffVo.getGyoumNam();
153 gyoumNam = setString(gyoumNam);
154 hs = new HSSFRichTextString(gyoumNam);
155 // 第6 + 2 * cnt行の第3列を取得
156 cell = row.getCell((short) 3);
157 // -------------修正 2009-1-19 start--------------
158 // 新規字体格式
159 HSSFFont font = wb.createFont();
160 // 字体タイプ
161 font.setFontName("MS Pゴシック");
162 // 字体大小
163 font.setFontHeightInPoints((short) 9);
164 // 新規風格
165 HSSFCellStyle style1 = wb.createCellStyle();
166 // セルの風格を取得
167 style1 = cell.getCellStyle();
168 // 内容の長さ>新規セルの長さの場合 内容の字体が小さくなる
169 if(hs.length()> 50){
170 font.setFontHeightInPoints((short)8);
171 }
172 // 風格に字体格式を設定
173 style1.setFont(font);
174 // セルに風格を設定
175 cell.setCellStyle(style1);
176 // -------------修正 2009-1-19 end--------------
177 // 設定第6 + 2 * cnt行第3列の内容は事業所(業務)名称
178 cell.setCellValue(hs);
179
180 // 事業所住所
181 String hakenBasyoNam = staffVo.getHakenBasyoNam();
182 hakenBasyoNam = setString(hakenBasyoNam);
183 String hakenAddr = staffVo.getHakenAddr();
184 hakenAddr = setString(hakenAddr);
185 String jimuBasyo = hakenBasyoNam + " " + hakenAddr;
186 hs = new HSSFRichTextString(jimuBasyo);
187 // 第6 + 2 * cnt行の第4列を取得
188 HSSFCell hakencell = row.getCell((short) 4);
189 // -------------修正 2009-1-19 start--------------
190 // 新規字体格式
191 HSSFFont font1 = wb.createFont();
192 // 字体タイプ
193 font1.setFontName("MS Pゴシック");
194 // 字体大小
195 font1.setFontHeightInPoints((short) 9);
196 // 新規風格
197 HSSFCellStyle style2 = wb.createCellStyle();
198 setStyle(style1,style2);
199 // 内容の長さ>新規セルの長さの場合 内容の字体が小さくなる
200 if (hs.length() > 50) {
201 font1.setFontHeightInPoints((short) 8);
202 }
203 // 風格に字体格式を設定
204 style2.setFont(font1);
205 // セルに風格を設定
206 hakencell.setCellStyle(style2);
207 // 設定第6 + 2 * cnt行第4列の内容は事業所(業務)名称
208 hakencell.setCellValue(hs);
209 // -------------修正 2009-1-19 end--------------
210
211 // 業務内容
212 String gyoumNaiyou = staffVo.getGyoumNaiyou();
213 gyoumNaiyou = setString(gyoumNaiyou);
214 hs = new HSSFRichTextString(gyoumNaiyou);
215 // 第6 + 2 * cnt行の第5列を取得
216 cell = row.getCell((short) 5);
217 // -------------修正 2009-1-19 start--------------
218 // 新規字体格式
219 HSSFFont font2 = wb.createFont();
220 // 字体タイプ
221 font2.setFontName("MS Pゴシック");
222 // 字体大小
223 font2.setFontHeightInPoints((short) 9);
224 // 新規風格
225 HSSFCellStyle style3 = wb.createCellStyle();
226 setStyle(style1,style3);
227 // 内容の長さ>新規セルの長さの場合 内容の字体が小さくなる
228 if (hs.length() > 70) {
229 font2.setFontHeightInPoints((short) 8);
230 }
231 // 風格に字体格式を設定
232 style3.setFont(font2);
233 // セルに風格を設定
234 cell.setCellStyle(style3);
235 // -------------修正 2009-1-19 end--------------
236 // 設定第6 + 2 * cnt行第5列の内容は業務内容
237 cell.setCellValue(hs);
238
239
240
241 // 派遣先責任者名
242 String hakenNam = staffVo.getHakenNam();
243 hakenNam = setString(hakenNam);
244 hs = new HSSFRichTextString(hakenNam);
245 // 第6 + 2 * cnt行の第6列を取得
246 cell = row.getCell((short) 6);
247 // 設定第6 + 2 * cnt行第6列の内容は派遣先責任者名
248 cell.setCellValue(hs);
249
250 // 派遣元責任者名
251 String hmempNam = staffVo.getHmempNam();
252 hmempNam = setString(hmempNam);
253 hs = new HSSFRichTextString(hmempNam);
254 // 第6 + 2 * cnt行の第7列を取得
255 cell = row.getCell((short) 7);
256 // 設定第6 + 2 * cnt行第7列の内容は派遣元責任者名
257 cell.setCellValue(hs);
258
259 // 健康保険,厚生年金保険,雇用保険
260 String str1 = "未加入";
261 for (int k = 0; k < 3; k++) {
262 hs = new HSSFRichTextString(str1);
263 // 第6 + 2 * cnt行の第8+k列を取得
264 cell = row.getCell((short) (8 + k));
265 // 設定第6 + 2 * cnt行第8+k列の内容は"未加入"
266 cell.setCellValue(hs);
267 }
268 // 社会保険手続状況
269 String str2 = "1日ごとの雇用契約で1ヶ月を越える継続的雇用が見込めない為未手続";
270 hs = new HSSFRichTextString(str2);
271 // 第6 + 2 * cnt行の第11列を取得
272 cell = row.getCell((short) 11);
273 // 設定第6 + 2 * cnt行第11列の内容は"1日ごとの雇用契約で2ヶ月を越える継続的雇用が見込めない為未手続"
274 cell.setCellValue(hs);
275
276 // シートの第7 + 2 * cnt行を取得
277 row = sheet.getRow(7 + 2 * cnt);
278 // 派遣先責任者連絡先
279 String hakenTel = staffVo.getHakenTel();
280 hakenTel = setString(hakenTel);
281 hs = new HSSFRichTextString(hakenTel);
282 // 第7 + 2 * cnt行の第6列を取得
283 cell = row.getCell((short) 6);
284 // 設定第7 + 2 * cnt行の第6列の内容は派遣先責任者連絡先
285 cell.setCellValue(hs);
286
287 String hmjigyoTele = staffVo.getHmjigyoTel();
288 hmjigyoTele = setString(hmjigyoTele);
289 hs = new HSSFRichTextString(hmjigyoTele);
290 // 第7 + 2 * cnt行の第7列を取得
291 cell = row.getCell((short) 7);
292 // 設定第7 + 2 * cnt行の第7列の内容は派遣元責任者連絡先
293 cell.setCellValue(hs);
294 // 同じスタッフデータ数
295 cnt++;
296 }
297 }
298 }
299
300 File f = new File(path);
301 // 指定パースがない場合 生成パス
302 if(!f.exists()){
303 f.mkdirs();
304 }
305 // 生成するファイルパス
306 FileOutputStream fos = new FileOutputStream(path+"/"+fileNam+".xls");
307 wb.write(fos);
308 // 指定パースに新excelファイルを生成
309 fos.close();
310
311 } catch (Exception e) {
312 e.printStackTrace();
313 System.out.println(e);
314 }
315 }