1 16 17 18 19 24 package org.apache.poi.hssf.usermodel; 25 26 import org.apache.poi.hssf.model.FormulaParser; 27 import org.apache.poi.hssf.model.Sheet; 28 import org.apache.poi.hssf.model.Workbook; 29 import org.apache.poi.hssf.record.*; 30 import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; 31 import org.apache.poi.hssf.record.formula.Ptg; 32 33 import java.util.Calendar ; 34 import java.util.Date ; 35 36 54 55 public class HSSFCell 56 { 57 58 63 64 public final static int CELL_TYPE_NUMERIC = 0; 65 66 71 72 public final static int CELL_TYPE_STRING = 1; 73 74 79 80 public final static int CELL_TYPE_FORMULA = 2; 81 82 87 88 public final static int CELL_TYPE_BLANK = 3; 89 90 95 96 public final static int CELL_TYPE_BOOLEAN = 4; 97 98 103 104 public final static int CELL_TYPE_ERROR = 5; 105 public final static short ENCODING_COMPRESSED_UNICODE = 0; 106 public final static short ENCODING_UTF_16 = 1; 107 private short cellNum; 108 private int cellType; 109 private HSSFCellStyle cellStyle; 110 private double cellValue; 111 private String stringValue; 112 private boolean booleanValue; 113 private byte errorValue; 114 private short encoding = ENCODING_COMPRESSED_UNICODE; 115 private Workbook book; 116 private Sheet sheet; 117 private int row; 119 private CellValueRecordInterface record; 120 121 137 138 protected HSSFCell(Workbook book, Sheet sheet, int row, short col) 140 { 141 checkBounds(col); 142 cellNum = col; 143 this.row = row; 144 cellStyle = null; 145 cellValue = 0; 146 stringValue = null; 147 booleanValue = false; 148 errorValue = ( byte ) 0; 149 this.book = book; 150 this.sheet = sheet; 151 152 setCellType(CELL_TYPE_BLANK, false); 156 ExtendedFormatRecord xf = book.getExFormatAt(0xf); 157 158 setCellStyle(new HSSFCellStyle(( short ) 0xf, xf)); 159 } 160 161 176 177 protected HSSFCell(Workbook book, Sheet sheet, int row, short col, 179 int type) 180 { 181 checkBounds(col); 182 cellNum = col; 183 this.row = row; 184 cellType = type; 185 cellStyle = null; 186 cellValue = 0; 187 stringValue = null; 188 booleanValue = false; 189 errorValue = ( byte ) 0; 190 this.book = book; 191 this.sheet = sheet; 192 switch (type) 193 { 194 195 case CELL_TYPE_NUMERIC : 196 record = new NumberRecord(); 197 (( NumberRecord ) record).setColumn(col); 198 (( NumberRecord ) record).setRow(row); 199 (( NumberRecord ) record).setValue(( short ) 0); 200 (( NumberRecord ) record).setXFIndex(( short ) 0); 201 break; 202 203 case CELL_TYPE_STRING : 204 record = new LabelSSTRecord(); 205 (( LabelSSTRecord ) record).setColumn(col); 206 (( LabelSSTRecord ) record).setRow(row); 207 (( LabelSSTRecord ) record).setXFIndex(( short ) 0); 208 break; 209 210 case CELL_TYPE_BLANK : 211 record = new BlankRecord(); 212 (( BlankRecord ) record).setColumn(col); 213 (( BlankRecord ) record).setRow(row); 214 (( BlankRecord ) record).setXFIndex(( short ) 0); 215 break; 216 217 case CELL_TYPE_FORMULA : 218 FormulaRecord formulaRecord = new FormulaRecord(); 219 record = new FormulaRecordAggregate(formulaRecord,null); 220 formulaRecord.setColumn(col); 221 formulaRecord.setRow(row); 222 formulaRecord.setXFIndex(( short ) 0); 223 case CELL_TYPE_BOOLEAN : 224 record = new BoolErrRecord(); 225 (( BoolErrRecord ) record).setColumn(col); 226 (( BoolErrRecord ) record).setRow(row); 227 (( BoolErrRecord ) record).setXFIndex(( short ) 0); 228 (( BoolErrRecord ) record).setValue(false); 229 break; 230 231 case CELL_TYPE_ERROR : 232 record = new BoolErrRecord(); 233 (( BoolErrRecord ) record).setColumn(col); 234 (( BoolErrRecord ) record).setRow(row); 235 (( BoolErrRecord ) record).setXFIndex(( short ) 0); 236 (( BoolErrRecord ) record).setValue(( byte ) 0); 237 break; 238 } 239 ExtendedFormatRecord xf = book.getExFormatAt(0xf); 240 241 setCellStyle(new HSSFCellStyle(( short ) 0xf, xf)); 242 } 243 244 252 253 protected HSSFCell(Workbook book, Sheet sheet, int row, 255 CellValueRecordInterface cval) 256 { 257 cellNum = cval.getColumn(); 258 record = cval; 259 this.row = row; 260 cellType = determineType(cval); 261 cellStyle = null; 262 stringValue = null; 263 this.book = book; 264 this.sheet = sheet; 265 switch (cellType) 266 { 267 268 case CELL_TYPE_NUMERIC : 269 cellValue = (( NumberRecord ) cval).getValue(); 270 break; 271 272 case CELL_TYPE_STRING : 273 stringValue = 274 book.getSSTString( ( (LabelSSTRecord ) cval).getSSTIndex()); 275 break; 276 277 case CELL_TYPE_BLANK : 278 break; 279 280 case CELL_TYPE_FORMULA : 281 cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue(); 282 stringValue=((FormulaRecordAggregate) cval).getStringValue(); 283 break; 284 285 case CELL_TYPE_BOOLEAN : 286 booleanValue = (( BoolErrRecord ) cval).getBooleanValue(); 287 break; 288 289 case CELL_TYPE_ERROR : 290 errorValue = (( BoolErrRecord ) cval).getErrorValue(); 291 break; 292 } 293 ExtendedFormatRecord xf = book.getExFormatAt(cval.getXFIndex()); 294 295 setCellStyle(new HSSFCellStyle(( short ) cval.getXFIndex(), xf)); 296 } 297 298 301 private HSSFCell() 302 { 303 } 304 305 308 private int determineType(CellValueRecordInterface cval) 309 { 310 Record record = ( Record ) cval; 311 int sid = record.getSid(); 312 int retval = 0; 313 314 switch (sid) 315 { 316 317 case NumberRecord.sid : 318 retval = HSSFCell.CELL_TYPE_NUMERIC; 319 break; 320 321 case BlankRecord.sid : 322 retval = HSSFCell.CELL_TYPE_BLANK; 323 break; 324 325 case LabelSSTRecord.sid : 326 retval = HSSFCell.CELL_TYPE_STRING; 327 break; 328 329 case FormulaRecordAggregate.sid : 330 retval = HSSFCell.CELL_TYPE_FORMULA; 331 break; 332 333 case BoolErrRecord.sid : 334 BoolErrRecord boolErrRecord = ( BoolErrRecord ) record; 335 336 retval = (boolErrRecord.isBoolean()) 337 ? HSSFCell.CELL_TYPE_BOOLEAN 338 : HSSFCell.CELL_TYPE_ERROR; 339 break; 340 } 341 return retval; 342 } 343 344 348 349 public void setCellNum(short num) 350 { 351 cellNum = num; 352 record.setColumn(num); 353 } 354 355 359 360 public short getCellNum() 361 { 362 return cellNum; 363 } 364 365 374 375 public void setCellType(int cellType) 376 { 377 setCellType(cellType, true); 378 } 379 380 388 389 private void setCellType(int cellType, boolean setValue) 390 { 391 392 if (cellType > CELL_TYPE_ERROR) 398 { 399 throw new RuntimeException ("I have no idea what type that is!"); 400 } 401 switch (cellType) 402 { 403 404 case CELL_TYPE_FORMULA : 405 FormulaRecordAggregate frec = null; 406 407 if (cellType != this.cellType) 408 { 409 frec = new FormulaRecordAggregate(new FormulaRecord(),null); 410 } 411 else 412 { 413 frec = ( FormulaRecordAggregate ) record; 414 } 415 frec.setColumn(getCellNum()); 416 if (setValue) 417 { 418 frec.getFormulaRecord().setValue(getNumericCellValue()); 419 } 420 frec.setXFIndex(( short ) cellStyle.getIndex()); 421 frec.setRow(row); 422 record = frec; 423 break; 424 425 case CELL_TYPE_NUMERIC : 426 NumberRecord nrec = null; 427 428 if (cellType != this.cellType) 429 { 430 nrec = new NumberRecord(); 431 } 432 else 433 { 434 nrec = ( NumberRecord ) record; 435 } 436 nrec.setColumn(getCellNum()); 437 if (setValue) 438 { 439 nrec.setValue(getNumericCellValue()); 440 } 441 nrec.setXFIndex(( short ) cellStyle.getIndex()); 442 nrec.setRow(row); 443 record = nrec; 444 break; 445 446 case CELL_TYPE_STRING : 447 LabelSSTRecord lrec = null; 448 449 if (cellType != this.cellType) 450 { 451 lrec = new LabelSSTRecord(); 452 } 453 else 454 { 455 lrec = ( LabelSSTRecord ) record; 456 } 457 lrec.setColumn(getCellNum()); 458 lrec.setRow(row); 459 lrec.setXFIndex(( short ) cellStyle.getIndex()); 460 if (setValue) 461 { 462 if ((getStringCellValue() != null) 463 && (!getStringCellValue().equals(""))) 464 { 465 int sst = 0; 466 467 if (encoding == ENCODING_COMPRESSED_UNICODE) 468 { 469 sst = book.addSSTString(getStringCellValue()); 470 } 471 if (encoding == ENCODING_UTF_16) 472 { 473 sst = book.addSSTString(getStringCellValue(), 474 true); 475 } 476 lrec.setSSTIndex(sst); 477 } 478 } 479 record = lrec; 480 break; 481 482 case CELL_TYPE_BLANK : 483 BlankRecord brec = null; 484 485 if (cellType != this.cellType) 486 { 487 brec = new BlankRecord(); 488 } 489 else 490 { 491 brec = ( BlankRecord ) record; 492 } 493 brec.setColumn(getCellNum()); 494 495 if (cellStyle != null) 497 { 498 brec.setXFIndex(( short ) cellStyle.getIndex()); 499 } 500 else 501 { 502 brec.setXFIndex(( short ) 0); 503 } 504 brec.setRow(row); 505 record = brec; 506 break; 507 508 case CELL_TYPE_BOOLEAN : 509 BoolErrRecord boolRec = null; 510 511 if (cellType != this.cellType) 512 { 513 boolRec = new BoolErrRecord(); 514 } 515 else 516 { 517 boolRec = ( BoolErrRecord ) record; 518 } 519 boolRec.setColumn(getCellNum()); 520 if (setValue) 521 { 522 boolRec.setValue(getBooleanCellValue()); 523 } 524 boolRec.setXFIndex(( short ) cellStyle.getIndex()); 525 boolRec.setRow(row); 526 record = boolRec; 527 break; 528 529 case CELL_TYPE_ERROR : 530 BoolErrRecord errRec = null; 531 532 if (cellType != this.cellType) 533 { 534 errRec = new BoolErrRecord(); 535 } 536 else 537 { 538 errRec = ( BoolErrRecord ) record; 539 } 540 errRec.setColumn(getCellNum()); 541 if (setValue) 542 { 543 errRec.setValue(getErrorCellValue()); 544 } 545 errRec.setXFIndex(( short ) cellStyle.getIndex()); 546 errRec.setRow(row); 547 record = errRec; 548 break; 549 } 550 if (cellType != this.cellType) 551 { 552 int loc = sheet.getLoc(); 553 554 sheet.replaceValueRecord(record); 555 sheet.setLoc(loc); 556 } 557 this.cellType = cellType; 558 } 559 560 568 569 public int getCellType() 570 { 571 return cellType; 572 } 573 574 581 public void setCellValue(double value) 582 { 583 if ((cellType != CELL_TYPE_NUMERIC) && (cellType != CELL_TYPE_FORMULA)) 584 { 585 setCellType(CELL_TYPE_NUMERIC, false); 586 } 587 (( NumberRecord ) record).setValue(value); 588 cellValue = value; 589 } 590 591 599 public void setCellValue(Date value) 600 { 601 setCellValue(HSSFDateUtil.getExcelDate(value)); 602 } 603 604 612 public void setCellValue(Calendar value) 613 { 614 setCellValue(value.getTime()); 615 } 616 617 626 627 public void setCellValue(String value) 628 { 629 if (value == null) 630 { 631 setCellType(CELL_TYPE_BLANK, false); 632 } 633 else 634 { 635 if ((cellType != CELL_TYPE_STRING ) && ( cellType != CELL_TYPE_FORMULA)) 636 { 637 setCellType(CELL_TYPE_STRING, false); 638 } 639 int index = 0; 640 641 if (encoding == ENCODING_COMPRESSED_UNICODE) 642 { 643 index = book.addSSTString(value); 644 } 645 if (encoding == ENCODING_UTF_16) 646 { 647 index = book.addSSTString(value, true); 648 } 649 (( LabelSSTRecord ) record).setSSTIndex(index); 650 stringValue = value; 651 } 652 } 653 654 public void setCellFormula(String formula) { 655 if (formula==null) { 657 setCellType(CELL_TYPE_BLANK,false); 658 } else { 659 setCellType(CELL_TYPE_FORMULA,false); 660 FormulaRecordAggregate rec = (FormulaRecordAggregate) record; 661 rec.getFormulaRecord().setOptions(( short ) 2); 662 rec.getFormulaRecord().setValue(0); 663 664 if (rec.getXFIndex() == (short)0) rec.setXFIndex(( short ) 0x0f); 666 FormulaParser fp = new FormulaParser(formula+";",book); 667 fp.parse(); 668 Ptg[] ptg = fp.getRPNPtg(); 669 int size = 0; 670 for (int k = 0; k < ptg.length; k++) { 672 size += ptg[ k ].getSize(); 673 rec.getFormulaRecord().pushExpressionToken(ptg[ k ]); 674 } 675 rec.getFormulaRecord().setExpressionLength(( short ) size); 676 } 678 } 679 680 public String getCellFormula() { 681 String retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression()); 683 return retval; 685 } 686 687 688 692 693 public double getNumericCellValue() 694 { 695 if (cellType == CELL_TYPE_BLANK) 696 { 697 return 0; 698 } 699 if (cellType == CELL_TYPE_STRING) 700 { 701 throw new NumberFormatException ( 702 "You cannot get a numeric value from a String based cell"); 703 } 704 if (cellType == CELL_TYPE_BOOLEAN) 705 { 706 throw new NumberFormatException ( 707 "You cannot get a numeric value from a boolean cell"); 708 } 709 if (cellType == CELL_TYPE_ERROR) 710 { 711 throw new NumberFormatException ( 712 "You cannot get a numeric value from an error cell"); 713 } 714 return cellValue; 715 } 716 717 721 public Date getDateCellValue() 722 { 723 if (cellType == CELL_TYPE_BLANK) 724 { 725 return null; 726 } 727 if (cellType == CELL_TYPE_STRING) 728 { 729 throw new NumberFormatException ( 730 "You cannot get a date value from a String based cell"); 731 } 732 if (cellType == CELL_TYPE_BOOLEAN) 733 { 734 throw new NumberFormatException ( 735 "You cannot get a date value from a boolean cell"); 736 } 737 if (cellType == CELL_TYPE_ERROR) 738 { 739 throw new NumberFormatException ( 740 "You cannot get a date value from an error cell"); 741 } 742 if (book.isUsing1904DateWindowing()) { 743 return HSSFDateUtil.getJavaDate(cellValue,true); 744 } 745 else { 746 return HSSFDateUtil.getJavaDate(cellValue,false); 747 } 748 } 749 750 755 756 public String getStringCellValue() 757 { 758 if (cellType == CELL_TYPE_BLANK) 759 { 760 return ""; 761 } 762 if (cellType == CELL_TYPE_NUMERIC) 763 { 764 throw new NumberFormatException ( 765 "You cannot get a string value from a numeric cell"); 766 } 767 if (cellType == CELL_TYPE_BOOLEAN) 768 { 769 throw new NumberFormatException ( 770 "You cannot get a string value from a boolean cell"); 771 } 772 if (cellType == CELL_TYPE_ERROR) 773 { 774 throw new NumberFormatException ( 775 "You cannot get a string value from an error cell"); 776 } 777 if (cellType == CELL_TYPE_FORMULA) 778 { 779 if (stringValue==null) return ""; 780 } 781 return stringValue; 782 } 783 784 791 792 public void setCellValue(boolean value) 793 { 794 if ((cellType != CELL_TYPE_BOOLEAN ) && ( cellType != CELL_TYPE_FORMULA)) 795 { 796 setCellType(CELL_TYPE_BOOLEAN, false); 797 } 798 (( BoolErrRecord ) record).setValue(value); 799 booleanValue = value; 800 } 801 802 810 811 public void setCellErrorValue(byte value) 812 { 813 if ((cellType != CELL_TYPE_ERROR) && (cellType != CELL_TYPE_FORMULA)) 814 { 815 setCellType(CELL_TYPE_ERROR, false); 816 } 817 (( BoolErrRecord ) record).setValue(value); 818 errorValue = value; 819 } 820 821 825 826 public boolean getBooleanCellValue() 827 { 828 if (cellType == CELL_TYPE_BOOLEAN) 829 { 830 return booleanValue; 831 } 832 if (cellType == CELL_TYPE_BLANK) 833 { 834 return false; 835 } 836 throw new NumberFormatException ( 837 "You cannot get a boolean value from a non-boolean cell"); 838 } 839 840 844 845 public byte getErrorCellValue() 846 { 847 if (cellType == CELL_TYPE_ERROR) 848 { 849 return errorValue; 850 } 851 if (cellType == CELL_TYPE_BLANK) 852 { 853 return ( byte ) 0; 854 } 855 throw new NumberFormatException ( 856 "You cannot get an error value from a non-error cell"); 857 } 858 859 867 868 public void setCellStyle(HSSFCellStyle style) 869 { 870 cellStyle = style; 871 record.setXFIndex(style.getIndex()); 872 } 873 874 879 880 public HSSFCellStyle getCellStyle() 881 { 882 return cellStyle; 883 } 884 885 893 894 public short getEncoding() 895 { 896 return encoding; 897 } 898 899 907 908 public void setEncoding(short encoding) 909 { 910 this.encoding = encoding; 911 } 912 913 918 919 protected CellValueRecordInterface getCellValueRecord() 920 { 921 return record; 922 } 923 924 927 private void checkBounds(int cellNum) { 928 if (cellNum > 255) { 929 throw new RuntimeException ("You cannot have more than 255 columns "+ 930 "in a given row (IV). Because Excel can't handle it"); 931 } 932 else if (cellNum < 0) { 933 throw new RuntimeException ("You cannot reference columns with an index of less then 0."); 934 } 935 } 936 937 940 public void setAsActiveCell() 941 { 942 this.sheet.setActiveCellRow(this.row); 943 this.sheet.setActiveCellCol(this.cellNum); 944 } 945 } 946 | Popular Tags |