432.
433. out.close(); 434.
435. 12.读和重写EXCEL文件 436.
437. POIFSFileSystem fs = 438.
439. new POIFSFileSystem(new FileInputStream(\ 440.
441. HSSFWorkbook wb = new HSSFWorkbook(fs); 442.
443. HSSFSheet sheet = wb.getSheetAt(0); 444.
445. HSSFRow row = sheet.getRow(2); 446.
447. HSSFCell cell = row.getCell((short)3); 448.
449. if (cell == null) 450.
451. cell = row.createCell((short)3); 452.
453. cell.setCellType(HSSFCell.CELL_TYPE_STRING); 454.
455. cell.setCellValue(\ 456.
457. // Write the output to a file 458.
459. FileOutputStream fileOut = new FileOutputStream(\ 460.
461. wb.write(fileOut); 462.
463. fileOut.close(); 464.
465. 13.在EXCEL单元格中使用自动换行 466.
467. HSSFWorkbook wb = new HSSFWorkbook(); 468.
469. HSSFSheet s = wb.createSheet(); 470.
471. HSSFRow r = null; 472.
473. HSSFCell c = null; 474.
475. HSSFCellStyle cs = wb.createCellStyle();
476.
477. HSSFFont f = wb.createFont(); 478.
479. HSSFFont f2 = wb.createFont(); 480.
481. cs = wb.createCellStyle(); 482.
483. cs.setFont( f2 ); 484.
485. //Word Wrap MUST be turned on 486.
487. cs.setWrapText( true ); 488.
489. r = s.createRow( (short) 2 ); 490.
491. r.setHeight( (short) 0x349 ); 492.
493. c = r.createCell( (short) 2 ); 494.
495. c.setCellType( HSSFCell.CELL_TYPE_STRING ); 496.
497. c.setCellValue( \ 498.
499. c.setCellStyle( cs ); 500.
501. s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) ); 502.
503. FileOutputStream fileOut = new FileOutputStream( \ 504.
505. wb.write( fileOut ); 506.
507. fileOut.close(); 508.
509. 14.数字格式自定义 510.
511. HSSFWorkbook wb = new HSSFWorkbook(); 512.
513. HSSFSheet sheet = wb.createSheet(\ 514.
515. HSSFCellStyle style; 516.
517. HSSFDataFormat format = wb.createDataFormat(); 518.
519. HSSFRow row;
520.
521. HSSFCell cell; 522.
523. short rowNum = 0; 524.
525. short colNum = 0; 526.
527. row = sheet.createRow(rowNum++); 528.
529. cell = row.createCell(colNum); 530.
531. cell.setCellValue(11111.25); 532.
533. style = wb.createCellStyle(); 534.
535. style.setDataFormat(format.getFormat(\ 536.
537. cell.setCellStyle(style); 538.
539. row = sheet.createRow(rowNum++); 540.
541. cell = row.createCell(colNum); 542.
543. cell.setCellValue(11111.25); 544.
545. style = wb.createCellStyle(); 546.
547. style.setDataFormat(format.getFormat(\ 548.
549. cell.setCellStyle(style); 550.
551. FileOutputStream fileOut = new FileOutputStream(\ 552.
553. wb.write(fileOut); 554.
555. fileOut.close(); 556.
557. 15.调整工作单位置 558.
559. HSSFWorkbook wb = new HSSFWorkbook(); 560.
561. HSSFSheet sheet = wb.createSheet(\ 562.
563. HSSFPrintSetup ps = sheet.getPrintSetup();
564.
565. sheet.setAutobreaks(true); 566.
567. ps.setFitHeight((short)1); 568.
569. ps.setFitWidth((short)1); 570. 571. 572.
573. // Create various cells and rows for spreadsheet. 574.
575. FileOutputStream fileOut = new FileOutputStream(\ 576.
577. wb.write(fileOut); 578.
579. fileOut.close(); 580.
581. 16.设置打印区域 582.
583. HSSFWorkbook wb = new HSSFWorkbook(); 584.
585. HSSFSheet sheet = wb.createSheet(\ 586.
587. wb.setPrintArea(0, \ 588.
589. //sets the print area for the first sheet 590.
591. //Alternatively: 592.
593. //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the Java
Docs for more details) 594.
595. // Create various cells and rows for spreadsheet. 596.
597. FileOutputStream fileOut = new FileOutputStream(\ 598.
599. wb.write(fileOut); 600.
601. fileOut.close(); 602.
603. 17.标注脚注 604.
605. HSSFWorkbook wb = new HSSFWorkbook(); 606.
607. HSSFSheet sheet = wb.createSheet(\ 608.
609. HSSFFooter footer = sheet.getFooter() 610.
611. footer.setRight( \
612. 613. 614.
615. // Create various cells and rows for spreadsheet. 616.
617. FileOutputStream fileOut = new FileOutputStream(\ 618.
619. wb.write(fileOut); 620.
621. fileOut.close(); 622.
623. 18.使用方便的内部提供的函数 624.
625. HSSFWorkbook wb = new HSSFWorkbook(); 626.
627. HSSFSheet sheet1 = wb.createSheet( \ 628.
629. // Create a merged region 630.
631. HSSFRow row = sheet1.createRow( (short) 1 ); 632.
633. HSSFRow row2 = sheet1.createRow( (short) 2 ); 634.
635. HSSFCell cell = row.createCell( (short) 1 ); 636.
637. cell.setCellValue( \ 638.
639. Region region = new Region( 1, (short) 1, 4, (short) 4 ); 640.
641. sheet1.addMergedRegion( region ); 642.
643. // Set the border and border colors. 644.
645. final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
646.
647. HSSFRegionUtil.setBorderBottom( borderMediumDashed, 648.