1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   /*
57    * Cell.java
58    *
59    * Created on September 30, 2001, 3:46 PM
60    */
61   package org.apache.poi.hssf.usermodel;
62   
63   import org.apache.poi.hssf.model.Workbook;
64   import org.apache.poi.hssf.model.Sheet;
65   import org.apache.poi.hssf.record.CellValueRecordInterface;
66   import org.apache.poi.hssf.record.Record;
67   import org.apache.poi.hssf.record.FormulaRecord;
68   import org.apache.poi.hssf.record.LabelSSTRecord;
69   import org.apache.poi.hssf.record.NumberRecord;
70   import org.apache.poi.hssf.record.BlankRecord;
71   import org.apache.poi.hssf.record.BoolErrRecord;
72   import org.apache.poi.hssf.record.ExtendedFormatRecord;
73   
74   import java.util.Date;
75   import java.util.Calendar;
76   
77   /**
78    * High level representation of a cell in a row of a spreadsheet.
79    * Cells can be numeric, formula-based or string-based (text).  The cell type
80    * specifies this.  String cells cannot conatin numbers and numeric cells cannot
81    * contain strings (at least according to our model).  Client apps should do the
82    * conversions themselves.  Formula cells are treated like string cells, simply
83    * containing a formula string.  They'll be rendered differently.
84    * <p>
85    * Cells should have their number (0 based) before being added to a row.  Only
86    * cells that have values should be added.
87    * <p>
88    * NOTE: the alpha won't be implementing formulas
89    *
90    * @author  Andrew C. Oliver (acoliver at apache dot org)
91    * @version 1.0-pre
92    */
93   
94   public class HSSFCell
95   {
96   
97       /**
98        * Numeric Cell type (0)
99        * @see #setCellType(int)
100       * @see #getCellType()
101       */
102  
103      public final static int          CELL_TYPE_NUMERIC           = 0;
104  
105      /**
106       * String Cell type (1)
107       * @see #setCellType(int)
108       * @see #getCellType()
109       */
110  
111      public final static int          CELL_TYPE_STRING            = 1;
112  
113      /**
114       * Formula Cell type (2)
115       * @see #setCellType(int)
116       * @see #getCellType()
117       */
118  
119      public final static int          CELL_TYPE_FORMULA           = 2;
120  
121      /**
122       * Blank Cell type (3)
123       * @see #setCellType(int)
124       * @see #getCellType()
125       */
126  
127      public final static int          CELL_TYPE_BLANK             = 3;
128  
129      /**
130       * Boolean Cell type (4)
131       * @see #setCellType(int)
132       * @see #getCellType()
133       */
134  
135      public final static int          CELL_TYPE_BOOLEAN           = 4;
136  
137      /**
138       * Error Cell type (5)
139       * @see #setCellType(int)
140       * @see #getCellType()
141       */
142  
143      public final static int          CELL_TYPE_ERROR             = 5;
144      public final static short        ENCODING_COMPRESSED_UNICODE = 0;
145      public final static short        ENCODING_UTF_16             = 1;
146      private short                    cellNum;
147      private int                      cellType;
148      private HSSFCellStyle            cellStyle;
149      private double                   cellValue;
150      private String                   stringValue;
151      private boolean                  booleanValue;
152      private byte                     errorValue;
153      private short                    encoding;
154      private Workbook                 book;
155      private Sheet                    sheet;
156      private short                    row;
157      private CellValueRecordInterface record;
158  
159      /**
160       * Creates new Cell - Should only be called by HSSFRow.  This creates a cell
161       * from scratch.
162       * <p>
163       * When the cell is initially created it is set to CELL_TYPE_BLANK. Cell types
164       * can be changed/overwritten by calling setCellValue with the appropriate
165       * type as a parameter although conversions from one type to another may be
166       * prohibited.
167       *
168       * @param book - Workbook record of the workbook containing this cell
169       * @param sheet - Sheet record of the sheet containing this cell
170       * @param row   - the row of this cell
171       * @param col   - the column for this cell
172       *
173       * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short)
174       */
175  
176      protected HSSFCell(Workbook book, Sheet sheet, short row, short col)
177      {
178          cellNum      = col;
179          this.row     = row;
180          cellStyle    = null;
181          cellValue    = 0;
182          stringValue  = null;
183          booleanValue = false;
184          errorValue   = ( byte ) 0;
185          this.book    = book;
186          this.sheet   = sheet;
187  
188          // Relying on the fact that by default the cellType is set to 0 which
189          // is different to CELL_TYPE_BLANK hence the following method call correctly
190          // creates a new blank cell.
191          setCellType(CELL_TYPE_BLANK, false);
192          ExtendedFormatRecord xf = book.getExFormatAt(0xf);
193  
194          setCellStyle(new HSSFCellStyle(( short ) 0xf, xf));
195      }
196  
197      /**
198       * Creates new Cell - Should only be called by HSSFRow.  This creates a cell
199       * from scratch.
200       *
201       * @param book - Workbook record of the workbook containing this cell
202       * @param sheet - Sheet record of the sheet containing this cell
203       * @param row   - the row of this cell
204       * @param col   - the column for this cell
205       * @param type  - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK,
206       *                CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
207       *                Type of cell
208       * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short,int)
209       * @deprecated As of 22-Jan-2002 use @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short)
210       * and use setCellValue to specify the type lazily.
211       */
212  
213      protected HSSFCell(Workbook book, Sheet sheet, short row, short col,
214                         int type)
215      {
216          cellNum      = col;
217          this.row     = row;
218          cellType     = type;
219          cellStyle    = null;
220          cellValue    = 0;
221          stringValue  = null;
222          booleanValue = false;
223          errorValue   = ( byte ) 0;
224          this.book    = book;
225          this.sheet   = sheet;
226          switch (type)
227          {
228  
229              case CELL_TYPE_NUMERIC :
230                  record = new NumberRecord();
231                  (( NumberRecord ) record).setColumn(col);
232                  (( NumberRecord ) record).setRow(row);
233                  (( NumberRecord ) record).setValue(( short ) 0);
234                  (( NumberRecord ) record).setXFIndex(( short ) 0);
235                  break;
236  
237              case CELL_TYPE_STRING :
238                  record = new LabelSSTRecord();
239                  (( LabelSSTRecord ) record).setColumn(col);
240                  (( LabelSSTRecord ) record).setRow(row);
241                  (( LabelSSTRecord ) record).setXFIndex(( short ) 0);
242                  break;
243  
244              case CELL_TYPE_BLANK :
245                  record = new BlankRecord();
246                  (( BlankRecord ) record).setColumn(col);
247                  (( BlankRecord ) record).setRow(row);
248                  (( BlankRecord ) record).setXFIndex(( short ) 0);
249                  break;
250  
251              case CELL_TYPE_FORMULA :
252                  record = new FormulaRecord();
253                  (( FormulaRecord ) record).setColumn(col);
254                  (( FormulaRecord ) record).setRow(row);
255                  (( FormulaRecord ) record).setXFIndex(( short ) 0);
256              case CELL_TYPE_BOOLEAN :
257                  record = new BoolErrRecord();
258                  (( BoolErrRecord ) record).setColumn(col);
259                  (( BoolErrRecord ) record).setRow(row);
260                  (( BoolErrRecord ) record).setXFIndex(( short ) 0);
261                  (( BoolErrRecord ) record).setValue(false);
262                  break;
263  
264              case CELL_TYPE_ERROR :
265                  record = new BoolErrRecord();
266                  (( BoolErrRecord ) record).setColumn(col);
267                  (( BoolErrRecord ) record).setRow(row);
268                  (( BoolErrRecord ) record).setXFIndex(( short ) 0);
269                  (( BoolErrRecord ) record).setValue(( byte ) 0);
270                  break;
271          }
272          ExtendedFormatRecord xf = book.getExFormatAt(0xf);
273  
274          setCellStyle(new HSSFCellStyle(( short ) 0xf, xf));
275      }
276  
277      /**
278       * Creates an HSSFCell from a CellValueRecordInterface.  HSSFSheet uses this when
279       * reading in cells from an existing sheet.
280       *
281       * @param book - Workbook record of the workbook containing this cell
282       * @param sheet - Sheet record of the sheet containing this cell
283       * @param cval - the Cell Value Record we wish to represent
284       */
285  
286      protected HSSFCell(Workbook book, Sheet sheet, short row,
287                         CellValueRecordInterface cval)
288      {
289          cellNum     = cval.getColumn();
290          record      = cval;
291          this.row    = row;
292          cellType    = determineType(cval);
293          cellStyle   = null;
294          stringValue = null;
295          this.book   = book;
296          this.sheet  = sheet;
297          switch (cellType)
298          {
299  
300              case CELL_TYPE_NUMERIC :
301                  cellValue = (( NumberRecord ) cval).getValue();
302                  break;
303  
304              case CELL_TYPE_STRING :
305                  stringValue =
306                      book
307                      .getSSTString((( LabelSSTRecord ) cval).getSSTIndex());
308                  break;
309  
310              case CELL_TYPE_BLANK :
311                  break;
312  
313              case CELL_TYPE_FORMULA :
314                  cellValue = (( FormulaRecord ) cval).getValue();
315                  break;
316  
317              case CELL_TYPE_BOOLEAN :
318                  booleanValue = (( BoolErrRecord ) cval).getBooleanValue();
319                  break;
320  
321              case CELL_TYPE_ERROR :
322                  errorValue = (( BoolErrRecord ) cval).getErrorValue();
323                  break;
324          }
325          ExtendedFormatRecord xf = book.getExFormatAt(cval.getXFIndex());
326  
327          setCellStyle(new HSSFCellStyle(( short ) cval.getXFIndex(), xf));
328      }
329  
330      /**
331       * private constructor to prevent blank construction
332       */
333      private HSSFCell()
334      {
335      }
336  
337      /**
338       * used internally -- given a cell value record, figure out its type
339       */
340      private int determineType(CellValueRecordInterface cval)
341      {
342          Record record = ( Record ) cval;
343          int    sid    = record.getSid();
344          int    retval = 0;
345  
346          switch (sid)
347          {
348  
349              case NumberRecord.sid :
350                  retval = HSSFCell.CELL_TYPE_NUMERIC;
351                  break;
352  
353              case BlankRecord.sid :
354                  retval = HSSFCell.CELL_TYPE_BLANK;
355                  break;
356  
357              case LabelSSTRecord.sid :
358                  retval = HSSFCell.CELL_TYPE_STRING;
359                  break;
360  
361              case FormulaRecord.sid :
362                  retval = HSSFCell.CELL_TYPE_FORMULA;
363                  break;
364  
365              case BoolErrRecord.sid :
366                  BoolErrRecord boolErrRecord = ( BoolErrRecord ) record;
367  
368                  retval = (boolErrRecord.isBoolean())
369                           ? HSSFCell.CELL_TYPE_BOOLEAN
370                           : HSSFCell.CELL_TYPE_ERROR;
371                  break;
372          }
373          return retval;
374      }
375  
376      /**
377       * set the cell's number within the row (0 based)
378       * @param num  short the cell number
379       */
380  
381      public void setCellNum(short num)
382      {
383          cellNum = num;
384          record.setColumn(num);
385      }
386  
387      /**
388       *  get the cell's number within the row
389       * @return short reperesenting the column number (logical!)
390       */
391  
392      public short getCellNum()
393      {
394          return cellNum;
395      }
396  
397      /**
398       * set the cells type (numeric, formula or string) -- DONT USE FORMULAS IN THIS RELEASE
399       * WE'LL THROW YOU A RUNTIME EXCEPTION IF YOU DO
400       * @see #CELL_TYPE_NUMERIC
401       * @see #CELL_TYPE_STRING
402       * @see #CELL_TYPE_FORMULA
403       * @see #CELL_TYPE_BLANK
404       * @see #CELL_TYPE_BOOLEAN
405       * @see #CELL_TYPE_ERROR
406       */
407  
408      public void setCellType(int cellType)
409      {
410          setCellType(cellType, true);
411      }
412  
413      /**
414       * sets the cell type. The setValue flag indicates whether to bother about
415       *  trying to preserve the current value in the new record if one is created.
416       *  <p>
417       *  The @see #setCellValue method will call this method with false in setValue
418       *  since it will overwrite the cell value later
419       *
420       */
421  
422      private void setCellType(int cellType, boolean setValue)
423      {
424  
425          // if (cellType == CELL_TYPE_FORMULA)
426          // {
427          // throw new RuntimeException(
428          // "Formulas have not been implemented in this release");
429          // }
430          if (cellType > CELL_TYPE_ERROR)
431          {
432              throw new RuntimeException("I have no idea what type that is!");
433          }
434          switch (cellType)
435          {
436  
437              case CELL_TYPE_FORMULA :
438                  FormulaRecord frec = null;
439  
440                  if (cellType != this.cellType)
441                  {
442                      frec = new FormulaRecord();
443                  }
444                  else
445                  {
446                      frec = ( FormulaRecord ) record;
447                  }
448                  frec.setColumn(getCellNum());
449                  if (setValue)
450                  {
451                      frec.setValue(getNumericCellValue());
452                  }
453                  frec.setXFIndex(( short ) cellStyle.getIndex());
454                  frec.setRow(row);
455                  record = frec;
456                  break;
457  
458              case CELL_TYPE_NUMERIC :
459                  NumberRecord nrec = null;
460  
461                  if (cellType != this.cellType)
462                  {
463                      nrec = new NumberRecord();
464                  }
465                  else
466                  {
467                      nrec = ( NumberRecord ) record;
468                  }
469                  nrec.setColumn(getCellNum());
470                  if (setValue)
471                  {
472                      nrec.setValue(getNumericCellValue());
473                  }
474                  nrec.setXFIndex(( short ) cellStyle.getIndex());
475                  nrec.setRow(row);
476                  record = nrec;
477                  break;
478  
479              case CELL_TYPE_STRING :
480                  LabelSSTRecord lrec = null;
481  
482                  if (cellType != this.cellType)
483                  {
484                      lrec = new LabelSSTRecord();
485                  }
486                  else
487                  {
488                      lrec = ( LabelSSTRecord ) record;
489                  }
490                  lrec.setColumn(getCellNum());
491                  lrec.setRow(row);
492                  lrec.setXFIndex(( short ) cellStyle.getIndex());
493                  if (setValue)
494                  {
495                      if ((getStringCellValue() != null)
496                              && (!getStringCellValue().equals("")))
497                      {
498                          int sst = 0;
499  
500                          if (encoding == ENCODING_COMPRESSED_UNICODE)
501                          {
502                              sst = book.addSSTString(getStringCellValue());
503                          }
504                          if (encoding == ENCODING_UTF_16)
505                          {
506                              sst = book.addSSTString(getStringCellValue(),
507                                                      true);
508                          }
509                          lrec.setSSTIndex(sst);
510                      }
511                  }
512                  record = lrec;
513                  break;
514  
515              case CELL_TYPE_BLANK :
516                  BlankRecord brec = null;
517  
518                  if (cellType != this.cellType)
519                  {
520                      brec = new BlankRecord();
521                  }
522                  else
523                  {
524                      brec = ( BlankRecord ) record;
525                  }
526                  brec.setColumn(getCellNum());
527  
528                  // During construction the cellStyle may be null for a Blank cell.
529                  if (cellStyle != null)
530                  {
531                      brec.setXFIndex(( short ) cellStyle.getIndex());
532                  }
533                  else
534                  {
535                      brec.setXFIndex(( short ) 0);
536                  }
537                  brec.setRow(row);
538                  record = brec;
539                  break;
540  
541              case CELL_TYPE_BOOLEAN :
542                  BoolErrRecord boolRec = null;
543  
544                  if (cellType != this.cellType)
545                  {
546                      boolRec = new BoolErrRecord();
547                  }
548                  else
549                  {
550                      boolRec = ( BoolErrRecord ) record;
551                  }
552                  boolRec.setColumn(getCellNum());
553                  if (setValue)
554                  {
555                      boolRec.setValue(getBooleanCellValue());
556                  }
557                  boolRec.setXFIndex(( short ) cellStyle.getIndex());
558                  boolRec.setRow(row);
559                  record = boolRec;
560                  break;
561  
562              case CELL_TYPE_ERROR :
563                  BoolErrRecord errRec = null;
564  
565                  if (cellType != this.cellType)
566                  {
567                      errRec = new BoolErrRecord();
568                  }
569                  else
570                  {
571                      errRec = ( BoolErrRecord ) record;
572                  }
573                  errRec.setColumn(getCellNum());
574                  if (setValue)
575                  {
576                      errRec.setValue(getErrorCellValue());
577                  }
578                  errRec.setXFIndex(( short ) cellStyle.getIndex());
579                  errRec.setRow(row);
580                  record = errRec;
581                  break;
582          }
583          if (cellType != this.cellType)
584          {
585              int loc = sheet.getLoc();
586  
587              sheet.replaceValueRecord(record);
588              sheet.setLoc(loc);
589          }
590          this.cellType = cellType;
591      }
592  
593      /**
594       * get the cells type (numeric, formula or string)
595       * @see #CELL_TYPE_STRING
596       * @see #CELL_TYPE_NUMERIC
597       * @see #CELL_TYPE_FORMULA
598       * @see #CELL_TYPE_BOOLEAN
599       * @see #CELL_TYPE_ERROR
600       */
601  
602      public int getCellType()
603      {
604          return cellType;
605      }
606  
607      /**
608       * set a numeric value for the cell
609       *
610       * @param value  the numeric value to set this cell to.  For formulas we'll set the
611       *        precalculated value, for numerics we'll set its value. For other types we
612       *        will change the cell to a numeric cell and set its value.
613       */
614      public void setCellValue(double value)
615      {
616          if ((cellType != CELL_TYPE_NUMERIC) && (cellType != CELL_TYPE_FORMULA))
617          {
618              setCellType(CELL_TYPE_NUMERIC, false);
619          }
620          (( NumberRecord ) record).setValue(value);
621          cellValue = value;
622      }
623  
624      /**
625       * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
626       * a date.
627       *
628       * @param value  the date value to set this cell to.  For formulas we'll set the
629       *        precalculated value, for numerics we'll set its value. For other types we
630       *        will change the cell to a numeric cell and set its value.
631       */
632      public void setCellValue(Date value)
633      {
634          setCellValue(HSSFDateUtil.getExcelDate(value));
635      }
636  
637      /**
638       * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
639       * a date.
640       *
641       * @param value  the date value to set this cell to.  For formulas we'll set the
642       *        precalculated value, for numerics we'll set its value. For othertypes we
643       *        will change the cell to a numeric cell and set its value.
644       */
645      public void setCellValue(Calendar value)
646      {
647          setCellValue(value.getTime());
648      }
649  
650      /**
651       * set a string value for the cell.
652       *
653       * @param value  value to set the cell to.  For formulas we'll set the formula
654       * string, for String cells we'll set its value.  For other types we will
655       * change the cell to a string cell and set its value.
656       * If value is null then we will change the cell to a Blank cell.
657       */
658  
659      public void setCellValue(String value)
660      {
661          if (value == null)
662          {
663              setCellType(CELL_TYPE_BLANK, false);
664          }
665          else
666          {
667              if ((cellType != CELL_TYPE_STRING)
668                      && (cellType != CELL_TYPE_FORMULA))
669              {
670                  setCellType(CELL_TYPE_STRING, false);
671              }
672              int index = 0;
673  
674              if (encoding == ENCODING_COMPRESSED_UNICODE)
675              {
676                  index = book.addSSTString(value);
677              }
678              if (encoding == ENCODING_UTF_16)
679              {
680                  index = book.addSSTString(value, true);
681              }
682              (( LabelSSTRecord ) record).setSSTIndex(index);
683              stringValue = value;
684          }
685      }
686  
687      /**
688       * get the value of the cell as a number.  For strings we throw an exception.
689       * For blank cells we return a 0.
690       */
691  
692      public double getNumericCellValue()
693      {
694          if (cellType == CELL_TYPE_BLANK)
695          {
696              return 0;
697          }
698          if (cellType == CELL_TYPE_STRING)
699          {
700              throw new NumberFormatException(
701                  "You cannot get a numeric value from a String based cell");
702          }
703          if (cellType == CELL_TYPE_BOOLEAN)
704          {
705              throw new NumberFormatException(
706                  "You cannot get a numeric value from a boolean cell");
707          }
708          if (cellType == CELL_TYPE_ERROR)
709          {
710              throw new NumberFormatException(
711                  "You cannot get a numeric value from an error cell");
712          }
713          return cellValue;
714      }
715  
716      /**
717       * get the value of the cell as a date.  For strings we throw an exception.
718       * For blank cells we return a null.
719       */
720      public Date getDateCellValue()
721      {
722          if (cellType == CELL_TYPE_BLANK)
723          {
724              return null;
725          }
726          if (cellType == CELL_TYPE_STRING)
727          {
728              throw new NumberFormatException(
729                  "You cannot get a date value from a String based cell");
730          }
731          if (cellType == CELL_TYPE_BOOLEAN)
732          {
733              throw new NumberFormatException(
734                  "You cannot get a date value from a boolean cell");
735          }
736          if (cellType == CELL_TYPE_ERROR)
737          {
738              throw new NumberFormatException(
739                  "You cannot get a date value from an error cell");
740          }
741          return HSSFDateUtil.getJavaDate(cellValue);
742      }
743  
744      /**
745       * get the value of the cell as a string - for numeric cells we throw an exception.
746       * For blank cells we return an empty string.
747       */
748  
749      public String getStringCellValue()
750      {
751          if (cellType == CELL_TYPE_BLANK)
752          {
753              return "";
754          }
755          if (cellType == CELL_TYPE_NUMERIC)
756          {
757              throw new NumberFormatException(
758                  "You cannot get a string value from a numeric cell");
759          }
760          if (cellType == CELL_TYPE_BOOLEAN)
761          {
762              throw new NumberFormatException(
763                  "You cannot get a string value from a boolean cell");
764          }
765          if (cellType == CELL_TYPE_ERROR)
766          {
767              throw new NumberFormatException(
768                  "You cannot get a string value from an error cell");
769          }
770          return stringValue;
771      }
772  
773      /**
774       * set a boolean value for the cell
775       *
776       * @param value the boolean value to set this cell to.  For formulas we'll set the
777       *        precalculated value, for booleans we'll set its value. For other types we
778       *        will change the cell to a boolean cell and set its value.
779       */
780  
781      public void setCellValue(boolean value)
782      {
783          if ((cellType != CELL_TYPE_BOOLEAN)
784                  && (cellType != CELL_TYPE_FORMULA))
785          {
786              setCellType(CELL_TYPE_BOOLEAN, false);
787          }
788          (( BoolErrRecord ) record).setValue(value);
789          booleanValue = value;
790      }
791  
792      /**
793       * set a error value for the cell
794       *
795       * @param value the error value to set this cell to.  For formulas we'll set the
796       *        precalculated value ??? IS THIS RIGHT??? , for errors we'll set
797       *        its value. For other types we will change the cell to an error
798       *        cell and set its value.
799       */
800  
801      public void setCellErrorValue(byte value)
802      {
803          if ((cellType != CELL_TYPE_ERROR) && (cellType != CELL_TYPE_FORMULA))
804          {
805              setCellType(CELL_TYPE_ERROR, false);
806          }
807          (( BoolErrRecord ) record).setValue(value);
808          errorValue = value;
809      }
810  
811      /**
812       * get the value of the cell as a boolean.  For strings, numbers, and errors, we throw an exception.
813       * For blank cells we return a false.
814       */
815  
816      public boolean getBooleanCellValue()
817      {
818          if (cellType == CELL_TYPE_BOOLEAN)
819          {
820              return booleanValue;
821          }
822          if (cellType == CELL_TYPE_BLANK)
823          {
824              return false;
825          }
826          throw new NumberFormatException(
827              "You cannot get a boolean value from a non-boolean cell");
828      }
829  
830      /**
831       * get the value of the cell as an error code.  For strings, numbers, and booleans, we throw an exception.
832       * For blank cells we return a 0.
833       */
834  
835      public byte getErrorCellValue()
836      {
837          if (cellType == CELL_TYPE_ERROR)
838          {
839              return errorValue;
840          }
841          if (cellType == CELL_TYPE_BLANK)
842          {
843              return ( byte ) 0;
844          }
845          throw new NumberFormatException(
846              "You cannot get an error value from a non-error cell");
847      }
848  
849      /**
850       * set the style for the cell.  The style should be an HSSFCellStyle created/retreived from
851       * the HSSFWorkbook.
852       *
853       * @param style  reference contained in the workbook
854       * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
855       * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
856       */
857  
858      public void setCellStyle(HSSFCellStyle style)
859      {
860          cellStyle = style;
861          record.setXFIndex(style.getIndex());
862      }
863  
864      /**
865       * get the style for the cell.  This is a reference to a cell style contained in the workbook
866       * object.
867       * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
868       */
869  
870      public HSSFCellStyle getCellStyle()
871      {
872          return cellStyle;
873      }
874  
875      /**
876       * used for internationalization, currently 0 for compressed unicode or 1 for 16-bit
877       *
878       * @see #ENCODING_COMPRESSED_UNICODE
879       * @see #ENCODING_UTF_16
880       *
881       * @return 1 or 0 for compressed or uncompressed (used only with String type)
882       */
883  
884      public short getEncoding()
885      {
886          return encoding;
887      }
888  
889      /**
890       * set the encoding to either 8 or 16 bit. (US/UK use 8-bit, rest of the western world use 16bit)
891       *
892       * @see #ENCODING_COMPRESSED_UNICODE
893       * @see #ENCODING_UTF_16
894       *
895       * @param encoding either ENCODING_COMPRESSED_UNICODE (0) or ENCODING_UTF_16 (1)
896       */
897  
898      public void setEncoding(short encoding)
899      {
900          this.encoding = encoding;
901      }
902  
903      /**
904       * Should only be used by HSSFSheet and friends.  Returns the low level CellValueRecordInterface record
905       *
906       * @return CellValueRecordInterface representing the cell via the low level api.
907       */
908  
909      protected CellValueRecordInterface getCellValueRecord()
910      {
911          return record;
912      }
913  }
914  ??????????????????????getSSTString??????????????????????????????????????LabelSSTRecord???????????????????????????????????????????????????????cval??????????????????CELL_TYPE_BLANK??????????????????CELL_TYPE_FORMULA?????????????????cellValue????????????????????????????????FormulaRecord????????????????????????????????????????????????cval??????????????????CELL_TYPE_BOOLEAN?????????????????booleanValue???????????????????????????????????BoolErrRecord???????????????????????????????????????????????????cval??????????????????CELL_TYPE_ERROR?????????????????errorValue?????????????????????????????????BoolErrRecord?????????????????????????????????????????????????cval?????????ExtendedFormatRecord???????????????????????????????????book????????????????????????????????????????getExFormatAt??????????????????????????????????????????????????????cval???????????????????????????????????????????????????????????getXFIndex?????????setCellStyle??????????????????????????HSSFCellStyle??????????????????????????????????????????????????cval???????????????????????????????????????????????????????getXFIndex?????????????????????????????????????????????????????????????????????xf??????????????????????????????????????????????????????????????????????????????????????HSSFCell??????????????????????????????????????????????????????????????????????????????????????????????????????????determineType???????????????????????????????CellValueRecordInterface?????????Record???????????????????????????Record????????????????????????????????????cval?????????????????????????record?????????????????sid??????????????????NumberRecord???????????????????????????????sid?????????????????retval??????????????????????????HSSFCell???????????????????????????????????CELL_TYPE_NUMERIC??????????????????BlankRecord??????????????????????????????sid?????????????????retval??????????????????????????HSSFCell???????????????????????????????????CELL_TYPE_BLANK??????????????????LabelSSTRecord?????????????????????????????????sid?????????????????retval??????????????????????????HSSFCell???????????????????????????????????CELL_TYPE_STRING??????????????????FormulaRecord????????????????????????????????sid?????????????????retval??????????????????????????HSSFCell???????????????????????????????????CELL_TYPE_FORMULA??????????????????BoolErrRecord????????????????????????????????sid?????????????????BoolErrRecord?????????????????????????????????????????????????BoolErrRecord?????????????????????????????????????????????????????????????????record?????????????????retval???????????????????????????boolErrRecord?????????????????????????????????????????isBoolean????????????????????????????HSSFCell?????????????????????????????????????CELL_TYPE_BOOLEAN????????????????????????????HSSFCell?????????????????????????????????????CELL_TYPE_ERROR????????????????retval????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellNum?????????cellNum???????????????????num?????????record????????????????setColumn??????????????????????????num???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getCellNum???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellType?????????setCellType?????????????????????cellType??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellType??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????cellType????????????????????????CELL_TYPE_ERROR?????????????????cellType??????????????????CELL_TYPE_FORMULA?????????????????FormulaRecord?????????????????????cellType?????????????????????frec????????????????????????????????FormulaRecord?????????????????????frec??????????????????????????????FormulaRecord??????????????????????????????????????????????record?????????????????frec??????????????????????setColumn????????????????????????????????getCellNum?????????????????????setValue?????????????????????frec??????????????????????????setValue???????????????????????????????????getNumericCellValue?????????????????frec??????????????????????setXFIndex???????????????????????????????????????????cellStyle?????????????????????????????????????????????????????getIndex?????????????????frec??????????????????????setRow?????????????????????????????row?????????????????record??????????????????????????frec??????????????????CELL_TYPE_NUMERIC?????????????????NumberRecord?????????????????????cellType?????????????????????nrec????????????????????????????????NumberRecord?????????????????????nrec??????????????????????????????NumberRecord?????????????????????????????????????????????record?????????????????nrec??????????????????????setColumn????????????????????????????????getCellNum?????????????????????setValue?????????????????????nrec??????????????????????????setValue???????????????????????????????????getNumericCellValue?????????????????nrec??????????????????????setXFIndex???????????????????????????????????????????cellStyle?????????????????????????????????????????????????????getIndex?????????????????nrec??????????????????????setRow?????????????????????????????row?????????????????record??????????????????????????nrec??????????????????CELL_TYPE_STRING?????????????????LabelSSTRecord?????????????????????cellType?????????????????????lrec????????????????????????????????LabelSSTRecord?????????????????????lrec??????????????????????????????LabelSSTRecord???????????????????????????????????????????????record?????????????????lrec??????????????????????setColumn????????????????????????????????getCellNum?????????????????lrec??????????????????????setRow?????????????????????????????row?????????????????lrec??????????????????????setXFIndex???????????????????????????????????????????cellStyle?????????????????????????????????????????????????????getIndex?????????????????????setValue??????????????????????????getStringCellValue??????????????????????????????????getStringCellValue?????????????????????????????encoding?????????????????????????????????????????ENCODING_COMPRESSED_UNICODE?????????????????????????????sst???????????????????????????????????book????????????????????????????????????????addSSTString?????????????????????????????????????????????????????getStringCellValue?????????????????????????????encoding?????????????????????????????????????????ENCODING_UTF_16?????????????????????????????sst???????????????????????????????????book????????????????????????????????????????addSSTString?????????????????????????????????????????????????????getStringCellValue?????????????????????????lrec??????????????????????????????setSSTIndex??????????????????????????????????????????sst?????????????????record??????????????????????????lrec??????????????????CELL_TYPE_BLANK?????????????????BlankRecord?????????????????????cellType?????????????????????brec????????????????????????????????BlankRecord?????????????????????brec??????????????????????????????BlankRecord????????????????????????????????????????????record?????????????????brec??????????????????????setColumn????????????????????????????????getCellNum????????????????????????????????????????????????????????????????????????????????????????????????????????cellStyle?????????????????????brec??????????????????????????setXFIndex???????????????????????????????????????????????cellStyle?????????????????????????????????????????????????????????getIndex?????????????????????brec??????????????????????????setXFIndex?????????????????brec??????????????????????setRow?????????????????????????????row?????????????????record??????????????????????????brec??????????????????CELL_TYPE_BOOLEAN?????????????????BoolErrRecord?????????????????????cellType?????????????????????boolRec???????????????????????????????????BoolErrRecord?????????????????????boolRec?????????????????????????????????BoolErrRecord?????????????????????????????????????????????????record?????????????????boolRec?????????????????????????setColumn???????????????????????????????????getCellNum?????????????????????setValue?????????????????????boolRec?????????????????????????????setValue??????????????????????????????????????getBooleanCellValue?????????????????boolRec?????????????????????????setXFIndex??????????????????????????????????????????????cellStyle????????????????????????????????????????????????????????getIndex?????????????????boolRec?????????????????????????setRow????????????????????????????????row?????????????????record??????????????????????????boolRec??????????????????CELL_TYPE_ERROR?????????????????BoolErrRecord?????????????????????cellType?????????????????????errRec??????????????????????????????????BoolErrRecord?????????????????????errRec????????????????????????????????BoolErrRecord????????????????????????????????????????????????record?????????????????errRec????????????????????????setColumn??????????????????????????????????getCellNum?????????????????????setValue?????????????????????errRec????????????????????????????setValue?????????????????????????????????????getErrorCellValue?????????????????errRec????????????????????????setXFIndex?????????????????????????????????????????????cellStyle???????????????????????????????????????????????????????getIndex?????????????????errRec????????????????????????setRow???????????????????????????????row?????????????????record??????????????????????????errRec?????????????cellType???????????????????????sheet?????????????????????????????getLoc?????????????sheet???????????????????replaceValueRecord??????????????????????????????????????record?????????????sheet???????????????????setLoc??????????????????????????loc?????????????????????????cellType???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getCellType????????????????cellType????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellValue??????????????cellType??????????????????????????CELL_TYPE_NUMERIC?????????????????????????????????????????????????cellType?????????????????????????????????????????????????????????????CELL_TYPE_FORMULA?????????????setCellType?????????????????????????CELL_TYPE_NUMERIC????????????NumberRecord???????????????????????????record????????????????????????????????????????????value?????????cellValue?????????????????????value???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellValue?????????setCellValue??????????????????????HSSFDateUtil???????????????????????????????????getExcelDate????????????????????????????????????????????????value??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellValue?????????setCellValue??????????????????????value??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellValue?????????????value?????????????setCellType?????????????????????????CELL_TYPE_BLANK??????????????????cellType??????????????????????????????CELL_TYPE_STRING?????????????????????????cellType?????????????????????????????????????CELL_TYPE_FORMULA?????????????????setCellType?????????????????????????????CELL_TYPE_STRING?????????????????encoding?????????????????????????????ENCODING_COMPRESSED_UNICODE?????????????????index?????????????????????????book??????????????????????????????addSSTString???????????????????????????????????????????value?????????????????encoding?????????????????????????????ENCODING_UTF_16?????????????????index?????????????????????????book??????????????????????????????addSSTString???????????????????????????????????????????value????????????????LabelSSTRecord?????????????????????????????????record?????????????????????????????????????????????????????index?????????????stringValue???????????????????????????value???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getNumericCellValue?????????????cellType?????????????????????????CELL_TYPE_BLANK?????????????cellType?????????????????????????CELL_TYPE_STRING?????????????cellType?????????????????????????CELL_TYPE_BOOLEAN?????????????cellType?????????????????????????CELL_TYPE_ERROR????????????????cellValue??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getDateCellValue?????????????cellType?????????????????????????CELL_TYPE_BLANK?????????????cellType?????????????????????????CELL_TYPE_STRING?????????????cellType?????????????????????????CELL_TYPE_BOOLEAN?????????????cellType?????????????????????????CELL_TYPE_ERROR????????????????HSSFDateUtil?????????????????????????????getJavaDate?????????????????????????????????????????cellValue?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getStringCellValue?????????????cellType?????????????????????????CELL_TYPE_BLANK?????????????cellType?????????????????????????CELL_TYPE_NUMERIC?????????????cellType?????????????????????????CELL_TYPE_BOOLEAN?????????????cellType?????????????????????????CELL_TYPE_ERROR????????????????stringValue???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellValue??????????????cellType??????????????????????????CELL_TYPE_BOOLEAN?????????????????????cellType?????????????????????????????????CELL_TYPE_FORMULA?????????????setCellType?????????????????????????CELL_TYPE_BOOLEAN????????????BoolErrRecord????????????????????????????record?????????????????????????????????????????????value?????????booleanValue????????????????????????value????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellErrorValue??????????????cellType??????????????????????????CELL_TYPE_ERROR???????????????????????????????????????????????cellType???????????????????????????????????????????????????????????CELL_TYPE_FORMULA?????????????setCellType?????????????????????????CELL_TYPE_ERROR????????????BoolErrRecord????????????????????????????record?????????????????????????????????????????????value?????????errorValue??????????????????????value???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getBooleanCellValue?????????????cellType?????????????????????????CELL_TYPE_BOOLEAN????????????????????booleanValue?????????????cellType?????????????????????????CELL_TYPE_BLANK??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getErrorCellValue?????????????cellType?????????????????????????CELL_TYPE_ERROR????????????????????errorValue?????????????cellType?????????????????????????CELL_TYPE_BLANK????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setCellStyle??????????????????????????????HSSFCellStyle?????????cellStyle?????????????????????style?????????record????????????????setXFIndex???????????????????????????style?????????????????????????????????getIndex??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????HSSFCellStyle??????????????????????????getCellStyle????????????????cellStyle?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getEncoding????????????????encoding????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setEncoding?????????????????????????encoding???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????CellValueRecordInterface????????????????????????????????????????getCellValueRecord