001 003 import java.io.FileNotFoundException; 002 003 004 import java.io.FileOutputStream; 004 005 005 import java.io.IOException; 006 007 006 008 009 007 import org.apache.log4j.Logger; 010 011 008 import org.apache.poi.hssf.usermodel.HSSFCell; 012 013 009 import org.apache.poi.hssf.usermodel.HSSFRow; 014 015 010 import org.apache.poi.hssf.usermodel.HSSFSheet; 016 017 011 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 018 019 012 import org.apache.poi.hssf.util.HSSFCellUtil; 020 021 013 import org.apache.poi.ss.usermodel.CellStyle; 022 023 014 import org.apache.poi.ss.usermodel.Font; 024 025 015 import org.apache.poi.ss.util.CellRangeAddress; 026 027 016 028 029 017 /** 030 031 018 * 描述:Excel寫操做幫助類 032 033 019 * @author ALEX 034 035 020 * @since 2010-11-24 036 037 021 * @version 1.0v 038 039 022 */ 040 041 023 public class ExcelUtil { 042 043 024 private static final Logger log=Logger.getLogger(ExcelUtil.class); 044 045 025 /** 046 047 026 * 功能:將HSSFWorkbook寫入Excel文件 048 049 027 * @param wb HSSFWorkbook 050 051 028 * @param absPath 寫入文件的相對路徑 052 053 029 * @param wbName 文件名 054 055 030 */ 056 057 031 public static void writeWorkbook(HSSFWorkbook wb,String fileName){ 058 059 032 FileOutputStream fos=null; 060 061 033 try { 062 063 034 fos=new FileOutputStream(fileName); 064 065 035 wb.write(fos); 066 067 036 } catch (FileNotFoundException e) { 068 069 037 log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause())); 070 071 038 } catch (IOException e) { 072 073 039 log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause())); 074 075 040 } finally{ 076 077 041 try { 078 079 042 if(fos!=null){ 080 081 043 fos.close(); 082 083 044 } 084 085 045 } catch (IOException e) { 086 087 046 log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause())); 088 089 047 } 090 091 048 } 092 093 049 } 094 095 050 /** 096 097 051 * 功能:建立HSSFSheet工做簿 098 099 052 * @param wb HSSFWorkbook 100 101 053 * @param sheetName String 102 103 054 * @return HSSFSheet 104 105 055 */ 106 107 056 public static HSSFSheet createSheet(HSSFWorkbook wb,String sheetName){ 108 109 057 HSSFSheet sheet=wb.createSheet(sheetName); 110 111 058 sheet.setDefaultColumnWidth(12); 112 113 059 sheet.setGridsPrinted(false); 114 115 060 sheet.setDisplayGridlines(false); 116 117 061 return sheet; 118 119 062 } 120 121 063 /** 122 123 064 * 功能:建立HSSFRow 124 125 065 * @param sheet HSSFSheet 126 127 066 * @param rowNum int 128 129 067 * @param height int 130 131 068 * @return HSSFRow 132 133 069 */ 134 135 070 public static HSSFRow createRow(HSSFSheet sheet,int rowNum,int height){ 136 137 071 HSSFRow row=sheet.createRow(rowNum); 138 139 072 row.setHeight((short)height); 140 141 073 return row; 142 143 074 } 144 145 075 /** 146 147 076 * 功能:建立CellStyle樣式 148 149 077 * @param wb HSSFWorkbook 150 151 078 * @param backgroundColor 背景色 152 153 079 * @param foregroundColor 前置色 154 155 080 * @param font 字體 156 157 081 * @return CellStyle 158 159 082 */ 160 161 083 public static CellStyle createCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){ 162 163 084 CellStyle cs=wb.createCellStyle(); 164 165 085 cs.setAlignment(halign); 166 167 086 cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 168 169 087 cs.setFillBackgroundColor(backgroundColor); 170 171 088 cs.setFillForegroundColor(foregroundColor); 172 173 089 cs.setFillPattern(CellStyle.SOLID_FOREGROUND); 174 175 090 cs.setFont(font); 176 177 091 return cs; 178 179 092 } 180 181 093 /** 182 183 094 * 功能:建立帶邊框的CellStyle樣式 184 185 095 * @param wb HSSFWorkbook 186 187 096 * @param backgroundColor 背景色 188 189 097 * @param foregroundColor 前置色 190 191 098 * @param font 字體 192 193 099 * @return CellStyle 194 195 100 */ 196 197 101 public static CellStyle createBorderCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){ 198 199 102 CellStyle cs=wb.createCellStyle(); 200 201 103 cs.setAlignment(halign); 202 203 104 cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 204 205 105 cs.setFillBackgroundColor(backgroundColor); 206 207 106 cs.setFillForegroundColor(foregroundColor); 208 209 107 cs.setFillPattern(CellStyle.SOLID_FOREGROUND); 210 211 108 cs.setFont(font); 212 213 109 cs.setBorderLeft(CellStyle.BORDER_DASHED); 214 215 110 cs.setBorderRight(CellStyle.BORDER_DASHED); 216 217 111 cs.setBorderTop(CellStyle.BORDER_DASHED); 218 219 112 cs.setBorderBottom(CellStyle.BORDER_DASHED); 220 221 113 return cs; 222 223 114 } 224 225 115 /** 226 227 116 * 功能:建立CELL 228 229 117 * @param row HSSFRow 230 231 118 * @param cellNum int 232 233 119 * @param style HSSFStyle 234 235 120 * @return HSSFCell 236 237 121 */ 238 239 122 public static HSSFCell createCell(HSSFRow row,int cellNum,CellStyle style){ 240 241 123 HSSFCell cell=row.createCell(cellNum); 242 243 124 cell.setCellStyle(style); 244 245 125 return cell; 246 247 126 } 248 249 127 /** 250 251 128 * 功能:合併單元格 252 253 129 * @param sheet HSSFSheet 254 255 130 * @param firstRow int 256 257 131 * @param lastRow int 258 259 132 * @param firstColumn int 260 261 133 * @param lastColumn int 262 263 134 * @return int 合併區域號碼 264 265 135 */ 266 267 136 public static int mergeCell(HSSFSheet sheet,int firstRow,int lastRow,int firstColumn,int lastColumn){ 268 269 137 return sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,firstColumn,lastColumn)); 270 271 138 } 272 273 139 /** 274 275 140 * 功能:建立字體 276 277 141 * @param wb HSSFWorkbook 278 279 142 * @param boldweight short 280 281 143 * @param color short 282 283 144 * @return Font 284 285 145 */ 286 287 146 public static Font createFont(HSSFWorkbook wb,short boldweight,short color,short size){ 288 289 147 Font font=wb.createFont(); 290 291 148 font.setBoldweight(boldweight); 292 293 149 font.setColor(color); 294 295 150 font.setFontHeightInPoints(size); 296 297 151 return font; 298 299 152 } 300 301 153 /** 302 303 154 * 設置合併單元格的邊框樣式 304 305 155 * @param sheet HSSFSheet 306 307 156 * @param ca CellRangAddress 308 309 157 * @param style CellStyle 310 311 158 */ 312 313 159 public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress ca,CellStyle style) { 314 315 160 for (int i = ca.getFirstRow(); i <= ca.getLastRow(); i++) { 316 317 161 HSSFRow row = HSSFCellUtil.getRow(i, sheet); 318 319 162 for (int j = ca.getFirstColumn(); j <= ca.getLastColumn(); j++) { 320 321 163 HSSFCell cell = HSSFCellUtil.getCell(row, j); 322 323 164 cell.setCellStyle(style); 324 325 165 } 326 327 166 } 328 329 167 } 330 331 168 } 332 333 [文件] ExcelUtil.java ~ 5KB 下載(553) 334 view sourceprint?001 package com.tkqd.util.poi; 335 336 002 337 338 003 import java.io.FileNotFoundException; 339 340 004 import java.io.FileOutputStream; 341 342 005 import java.io.IOException; 343 344 006 345 346 007 import org.apache.log4j.Logger; 347 348 008 import org.apache.poi.hssf.usermodel.HSSFCell; 349 350 009 import org.apache.poi.hssf.usermodel.HSSFRow; 351 352 010 import org.apache.poi.hssf.usermodel.HSSFSheet; 353 354 011 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 355 356 012 import org.apache.poi.hssf.util.HSSFCellUtil; 357 358 013 import org.apache.poi.ss.usermodel.CellStyle; 359 360 014 import org.apache.poi.ss.usermodel.Font; 361 362 015 import org.apache.poi.ss.util.CellRangeAddress; 363 364 016 365 366 017 /** 367 368 018 * 描述:Excel寫操做幫助類 369 370 019 * @author ALEX 371 372 020 * @since 2010-11-24 373 374 021 * @version 1.0v 375 376 022 */ 377 378 023 public class ExcelUtil { 379 380 024 private static final Logger log=Logger.getLogger(ExcelUtil.class); 381 382 025 /** 383 384 026 * 功能:將HSSFWorkbook寫入Excel文件 385 386 027 * @param wb HSSFWorkbook 387 388 028 * @param absPath 寫入文件的相對路徑 389 390 029 * @param wbName 文件名 391 392 030 */ 393 394 031 public static void writeWorkbook(HSSFWorkbook wb,String fileName){ 395 396 032 FileOutputStream fos=null; 397 398 033 try { 399 400 034 fos=new FileOutputStream(fileName); 401 402 035 wb.write(fos); 403 404 036 } catch (FileNotFoundException e) { 405 406 037 log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause())); 407 408 038 } catch (IOException e) { 409 410 039 log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause())); 411 412 040 } finally{ 413 414 041 try { 415 416 042 if(fos!=null){ 417 418 043 fos.close(); 419 420 044 } 421 422 045 } catch (IOException e) { 423 424 046 log.error(new StringBuffer("[").append(e.getMessage()).append("]").append(e.getCause())); 425 426 047 } 427 428 048 } 429 430 049 } 431 432 050 /** 433 434 051 * 功能:建立HSSFSheet工做簿 435 436 052 * @param wb HSSFWorkbook 437 438 053 * @param sheetName String 439 440 054 * @return HSSFSheet 441 442 055 */ 443 444 056 public static HSSFSheet createSheet(HSSFWorkbook wb,String sheetName){ 445 446 057 HSSFSheet sheet=wb.createSheet(sheetName); 447 448 058 sheet.setDefaultColumnWidth(12); 449 450 059 sheet.setGridsPrinted(false); 451 452 060 sheet.setDisplayGridlines(false); 453 454 061 return sheet; 455 456 062 } 457 458 063 /** 459 460 064 * 功能:建立HSSFRow 461 462 065 * @param sheet HSSFSheet 463 464 066 * @param rowNum int 465 466 067 * @param height int 467 468 068 * @return HSSFRow 469 470 069 */ 471 472 070 public static HSSFRow createRow(HSSFSheet sheet,int rowNum,int height){ 473 474 071 HSSFRow row=sheet.createRow(rowNum); 475 476 072 row.setHeight((short)height); 477 478 073 return row; 479 480 074 } 481 482 075 /** 483 484 076 * 功能:建立CellStyle樣式 485 486 077 * @param wb HSSFWorkbook 487 488 078 * @param backgroundColor 背景色 489 490 079 * @param foregroundColor 前置色 491 492 080 * @param font 字體 493 494 081 * @return CellStyle 495 496 082 */ 497 498 083 public static CellStyle createCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){ 499 500 084 CellStyle cs=wb.createCellStyle(); 501 502 085 cs.setAlignment(halign); 503 504 086 cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 505 506 087 cs.setFillBackgroundColor(backgroundColor); 507 508 088 cs.setFillForegroundColor(foregroundColor); 509 510 089 cs.setFillPattern(CellStyle.SOLID_FOREGROUND); 511 512 090 cs.setFont(font); 513 514 091 return cs; 515 516 092 } 517 518 093 /** 519 520 094 * 功能:建立帶邊框的CellStyle樣式 521 522 095 * @param wb HSSFWorkbook 523 524 096 * @param backgroundColor 背景色 525 526 097 * @param foregroundColor 前置色 527 528 098 * @param font 字體 529 530 099 * @return CellStyle 531 532 100 */ 533 534 101 public static CellStyle createBorderCellStyle(HSSFWorkbook wb,short backgroundColor,short foregroundColor,short halign,Font font){ 535 536 102 CellStyle cs=wb.createCellStyle(); 537 538 103 cs.setAlignment(halign); 539 540 104 cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 541 542 105 cs.setFillBackgroundColor(backgroundColor); 543 544 106 cs.setFillForegroundColor(foregroundColor); 545 546 107 cs.setFillPattern(CellStyle.SOLID_FOREGROUND); 547 548 108 cs.setFont(font); 549 550 109 cs.setBorderLeft(CellStyle.BORDER_DASHED); 551 552 110 cs.setBorderRight(CellStyle.BORDER_DASHED); 553 554 111 cs.setBorderTop(CellStyle.BORDER_DASHED); 555 556 112 cs.setBorderBottom(CellStyle.BORDER_DASHED); 557 558 113 return cs; 559 560 114 } 561 562 115 /** 563 564 116 * 功能:建立CELL 565 566 117 * @param row HSSFRow 567 568 118 * @param cellNum int 569 570 119 * @param style HSSFStyle 571 572 120 * @return HSSFCell 573 574 121 */ 575 576 122 public static HSSFCell createCell(HSSFRow row,int cellNum,CellStyle style){ 577 578 123 HSSFCell cell=row.createCell(cellNum); 579 580 124 cell.setCellStyle(style); 581 582 125 return cell; 583 584 126 } 585 586 127 /** 587 588 128 * 功能:合併單元格 589 590 129 * @param sheet HSSFSheet 591 592 130 * @param firstRow int 593 594 131 * @param lastRow int 595 596 132 * @param firstColumn int 597 598 133 * @param lastColumn int 599 600 134 * @return int 合併區域號碼 601 602 135 */ 603 604 136 public static int mergeCell(HSSFSheet sheet,int firstRow,int lastRow,int firstColumn,int lastColumn){ 605 606 137 return sheet.addMergedRegion(new CellRangeAddress(firstRow,lastRow,firstColumn,lastColumn)); 607 608 138 } 609 610 139 /** 611 612 140 * 功能:建立字體 613 614 141 * @param wb HSSFWorkbook 615 616 142 * @param boldweight short 617 618 143 * @param color short 619 620 144 * @return Font 621 622 145 */ 623 624 146 public static Font createFont(HSSFWorkbook wb,short boldweight,short color,short size){ 625 626 147 Font font=wb.createFont(); 627 628 148 font.setBoldweight(boldweight); 629 630 149 font.setColor(color); 631 632 150 font.setFontHeightInPoints(size); 633 634 151 return font; 635 636 152 } 637 638 153 /** 639 640 154 * 設置合併單元格的邊框樣式 641 642 155 * @param sheet HSSFSheet 643 644 156 * @param ca CellRangAddress 645 646 157 * @param style CellStyle 647 648 158 */ 649 650 159 public static void setRegionStyle(HSSFSheet sheet, CellRangeAddress ca,CellStyle style) { 651 652 160 for (int i = ca.getFirstRow(); i <= ca.getLastRow(); i++) { 653 654 161 HSSFRow row = HSSFCellUtil.getRow(i, sheet); 655 656 162 for (int j = ca.getFirstColumn(); j <= ca.getLastColumn(); j++) { 657 658 163 HSSFCell cell = HSSFCellUtil.getCell(row, j); 659 660 164 cell.setCellStyle(style); 661 662 165 } 663 664 166 } 665 666 167 } 667 668 168 }