KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFCell


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18
19 /*
20  * Cell.java
21  *
22  * Created on September 30, 2001, 3:46 PM
23  */

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 JavaDoc;
34 import java.util.Date JavaDoc;
35
36 /**
37  * High level representation of a cell in a row of a spreadsheet.
38  * Cells can be numeric, formula-based or string-based (text). The cell type
39  * specifies this. String cells cannot conatin numbers and numeric cells cannot
40  * contain strings (at least according to our model). Client apps should do the
41  * conversions themselves. Formula cells are treated like string cells, simply
42  * containing a formula string. They'll be rendered differently.
43  * <p>
44  * Cells should have their number (0 based) before being added to a row. Only
45  * cells that have values should be added.
46  * <p>
47  * NOTE: the alpha won't be implementing formulas
48  *
49  * @author Andrew C. Oliver (acoliver at apache dot org)
50  * @author Dan Sherman (dsherman at isisph.com)
51  * @author Brian Sanders (kestrel at burdell dot org) Active Cell support
52  * @version 1.0-pre
53  */

54
55 public class HSSFCell
56 {
57
58     /**
59      * Numeric Cell type (0)
60      * @see #setCellType(int)
61      * @see #getCellType()
62      */

63
64     public final static int CELL_TYPE_NUMERIC = 0;
65
66     /**
67      * String Cell type (1)
68      * @see #setCellType(int)
69      * @see #getCellType()
70      */

71
72     public final static int CELL_TYPE_STRING = 1;
73
74     /**
75      * Formula Cell type (2)
76      * @see #setCellType(int)
77      * @see #getCellType()
78      */

79
80     public final static int CELL_TYPE_FORMULA = 2;
81
82     /**
83      * Blank Cell type (3)
84      * @see #setCellType(int)
85      * @see #getCellType()
86      */

87
88     public final static int CELL_TYPE_BLANK = 3;
89
90     /**
91      * Boolean Cell type (4)
92      * @see #setCellType(int)
93      * @see #getCellType()
94      */

95
96     public final static int CELL_TYPE_BOOLEAN = 4;
97
98     /**
99      * Error Cell type (5)
100      * @see #setCellType(int)
101      * @see #getCellType()
102      */

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 JavaDoc 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 short row;
118
private int row;
119     private CellValueRecordInterface record;
120
121     /**
122      * Creates new Cell - Should only be called by HSSFRow. This creates a cell
123      * from scratch.
124      * <p>
125      * When the cell is initially created it is set to CELL_TYPE_BLANK. Cell types
126      * can be changed/overwritten by calling setCellValue with the appropriate
127      * type as a parameter although conversions from one type to another may be
128      * prohibited.
129      *
130      * @param book - Workbook record of the workbook containing this cell
131      * @param sheet - Sheet record of the sheet containing this cell
132      * @param row - the row of this cell
133      * @param col - the column for this cell
134      *
135      * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short)
136      */

137
138     //protected HSSFCell(Workbook book, Sheet sheet, short row, short col)
139
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         // Relying on the fact that by default the cellType is set to 0 which
153
// is different to CELL_TYPE_BLANK hence the following method call correctly
154
// creates a new blank cell.
155
setCellType(CELL_TYPE_BLANK, false);
156         ExtendedFormatRecord xf = book.getExFormatAt(0xf);
157
158         setCellStyle(new HSSFCellStyle(( short ) 0xf, xf));
159     }
160
161     /**
162      * Creates new Cell - Should only be called by HSSFRow. This creates a cell
163      * from scratch.
164      *
165      * @param book - Workbook record of the workbook containing this cell
166      * @param sheet - Sheet record of the sheet containing this cell
167      * @param row - the row of this cell
168      * @param col - the column for this cell
169      * @param type - CELL_TYPE_NUMERIC, CELL_TYPE_STRING, CELL_TYPE_FORMULA, CELL_TYPE_BLANK,
170      * CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
171      * Type of cell
172      * @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short,int)
173      * @deprecated As of 22-Jan-2002 use @see org.apache.poi.hssf.usermodel.HSSFRow#createCell(short)
174      * and use setCellValue to specify the type lazily.
175      */

176
177     //protected HSSFCell(Workbook book, Sheet sheet, short row, short col,
178
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     /**
245      * Creates an HSSFCell from a CellValueRecordInterface. HSSFSheet uses this when
246      * reading in cells from an existing sheet.
247      *
248      * @param book - Workbook record of the workbook containing this cell
249      * @param sheet - Sheet record of the sheet containing this cell
250      * @param cval - the Cell Value Record we wish to represent
251      */

252
253     //protected HSSFCell(Workbook book, Sheet sheet, short row,
254
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     /**
299      * private constructor to prevent blank construction
300      */

301     private HSSFCell()
302     {
303     }
304
305     /**
306      * used internally -- given a cell value record, figure out its type
307      */

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     /**
345      * set the cell's number within the row (0 based)
346      * @param num short the cell number
347      */

348
349     public void setCellNum(short num)
350     {
351         cellNum = num;
352         record.setColumn(num);
353     }
354
355     /**
356      * get the cell's number within the row
357      * @return short reperesenting the column number (logical!)
358      */

359
360     public short getCellNum()
361     {
362         return cellNum;
363     }
364
365     /**
366      * set the cells type (numeric, formula or string)
367      * @see #CELL_TYPE_NUMERIC
368      * @see #CELL_TYPE_STRING
369      * @see #CELL_TYPE_FORMULA
370      * @see #CELL_TYPE_BLANK
371      * @see #CELL_TYPE_BOOLEAN
372      * @see #CELL_TYPE_ERROR
373      */

374
375     public void setCellType(int cellType)
376     {
377         setCellType(cellType, true);
378     }
379
380     /**
381      * sets the cell type. The setValue flag indicates whether to bother about
382      * trying to preserve the current value in the new record if one is created.
383      * <p>
384      * The @see #setCellValue method will call this method with false in setValue
385      * since it will overwrite the cell value later
386      *
387      */

388
389     private void setCellType(int cellType, boolean setValue)
390     {
391
392         // if (cellType == CELL_TYPE_FORMULA)
393
// {
394
// throw new RuntimeException(
395
// "Formulas have not been implemented in this release");
396
// }
397
if (cellType > CELL_TYPE_ERROR)
398         {
399             throw new RuntimeException JavaDoc("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                 // During construction the cellStyle may be null for a Blank cell.
496
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     /**
561      * get the cells type (numeric, formula or string)
562      * @see #CELL_TYPE_STRING
563      * @see #CELL_TYPE_NUMERIC
564      * @see #CELL_TYPE_FORMULA
565      * @see #CELL_TYPE_BOOLEAN
566      * @see #CELL_TYPE_ERROR
567      */

568
569     public int getCellType()
570     {
571         return cellType;
572     }
573
574     /**
575      * set a numeric value for the cell
576      *
577      * @param value the numeric value to set this cell to. For formulas we'll set the
578      * precalculated value, for numerics we'll set its value. For other types we
579      * will change the cell to a numeric cell and set its value.
580      */

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     /**
592      * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
593      * a date.
594      *
595      * @param value the date value to set this cell to. For formulas we'll set the
596      * precalculated value, for numerics we'll set its value. For other types we
597      * will change the cell to a numeric cell and set its value.
598      */

599     public void setCellValue(Date JavaDoc value)
600     {
601         setCellValue(HSSFDateUtil.getExcelDate(value));
602     }
603
604     /**
605      * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as
606      * a date.
607      *
608      * @param value the date value to set this cell to. For formulas we'll set the
609      * precalculated value, for numerics we'll set its value. For othertypes we
610      * will change the cell to a numeric cell and set its value.
611      */

612     public void setCellValue(Calendar JavaDoc value)
613     {
614         setCellValue(value.getTime());
615     }
616
617     /**
618      * set a string value for the cell. Please note that if you are using
619      * full 16 bit unicode you should call <code>setEncoding()</code> first.
620      *
621      * @param value value to set the cell to. For formulas we'll set the formula
622      * string, for String cells we'll set its value. For other types we will
623      * change the cell to a string cell and set its value.
624      * If value is null then we will change the cell to a Blank cell.
625      */

626
627     public void setCellValue(String JavaDoc 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 JavaDoc formula) {
655         //Workbook.currentBook=book;
656
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             //only set to default if there is no extended format index already set
665
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             //System.out.println("got Ptgs " + ptg.length);
671
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             //Workbook.currentBook = null;
677
}
678     }
679
680     public String JavaDoc getCellFormula() {
681         //Workbook.currentBook=book;
682
String JavaDoc retval = FormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());
683         //Workbook.currentBook=null;
684
return retval;
685     }
686
687
688     /**
689      * get the value of the cell as a number. For strings we throw an exception.
690      * For blank cells we return a 0.
691      */

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 JavaDoc(
702                 "You cannot get a numeric value from a String based cell");
703         }
704         if (cellType == CELL_TYPE_BOOLEAN)
705         {
706             throw new NumberFormatException JavaDoc(
707                 "You cannot get a numeric value from a boolean cell");
708         }
709         if (cellType == CELL_TYPE_ERROR)
710         {
711             throw new NumberFormatException JavaDoc(
712                 "You cannot get a numeric value from an error cell");
713         }
714         return cellValue;
715     }
716
717     /**
718      * get the value of the cell as a date. For strings we throw an exception.
719      * For blank cells we return a null.
720      */

721     public Date JavaDoc getDateCellValue()
722     {
723         if (cellType == CELL_TYPE_BLANK)
724         {
725             return null;
726         }
727         if (cellType == CELL_TYPE_STRING)
728         {
729             throw new NumberFormatException JavaDoc(
730                 "You cannot get a date value from a String based cell");
731         }
732         if (cellType == CELL_TYPE_BOOLEAN)
733         {
734             throw new NumberFormatException JavaDoc(
735                 "You cannot get a date value from a boolean cell");
736         }
737         if (cellType == CELL_TYPE_ERROR)
738         {
739             throw new NumberFormatException JavaDoc(
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     /**
751      * get the value of the cell as a string - for numeric cells we throw an exception.
752      * For blank cells we return an empty string.
753      * For formulaCells that are not string Formulas, we return empty String
754      */

755
756     public String JavaDoc getStringCellValue()
757     {
758         if (cellType == CELL_TYPE_BLANK)
759         {
760             return "";
761         }
762         if (cellType == CELL_TYPE_NUMERIC)
763         {
764             throw new NumberFormatException JavaDoc(
765                 "You cannot get a string value from a numeric cell");
766         }
767         if (cellType == CELL_TYPE_BOOLEAN)
768         {
769             throw new NumberFormatException JavaDoc(
770                 "You cannot get a string value from a boolean cell");
771         }
772         if (cellType == CELL_TYPE_ERROR)
773         {
774             throw new NumberFormatException JavaDoc(
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     /**
785      * set a boolean value for the cell
786      *
787      * @param value the boolean value to set this cell to. For formulas we'll set the
788      * precalculated value, for booleans we'll set its value. For other types we
789      * will change the cell to a boolean cell and set its value.
790      */

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     /**
803      * set a error value for the cell
804      *
805      * @param value the error value to set this cell to. For formulas we'll set the
806      * precalculated value ??? IS THIS RIGHT??? , for errors we'll set
807      * its value. For other types we will change the cell to an error
808      * cell and set its value.
809      */

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     /**
822      * get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception.
823      * For blank cells we return a false.
824      */

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 JavaDoc(
837             "You cannot get a boolean value from a non-boolean cell");
838     }
839
840     /**
841      * get the value of the cell as an error code. For strings, numbers, and booleans, we throw an exception.
842      * For blank cells we return a 0.
843      */

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 JavaDoc(
856             "You cannot get an error value from a non-error cell");
857     }
858
859     /**
860      * set the style for the cell. The style should be an HSSFCellStyle created/retreived from
861      * the HSSFWorkbook.
862      *
863      * @param style reference contained in the workbook
864      * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createCellStyle()
865      * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
866      */

867
868     public void setCellStyle(HSSFCellStyle style)
869     {
870         cellStyle = style;
871         record.setXFIndex(style.getIndex());
872     }
873
874     /**
875      * get the style for the cell. This is a reference to a cell style contained in the workbook
876      * object.
877      * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(short)
878      */

879
880     public HSSFCellStyle getCellStyle()
881     {
882         return cellStyle;
883     }
884
885     /**
886      * used for internationalization, currently 0 for compressed unicode or 1 for 16-bit
887      *
888      * @see #ENCODING_COMPRESSED_UNICODE
889      * @see #ENCODING_UTF_16
890      *
891      * @return 1 or 0 for compressed or uncompressed (used only with String type)
892      */

893
894     public short getEncoding()
895     {
896         return encoding;
897     }
898
899     /**
900      * set the encoding to either 8 or 16 bit. (US/UK use 8-bit, rest of the western world use 16bit)
901      *
902      * @see #ENCODING_COMPRESSED_UNICODE
903      * @see #ENCODING_UTF_16
904      *
905      * @param encoding either ENCODING_COMPRESSED_UNICODE (0) or ENCODING_UTF_16 (1)
906      */

907
908     public void setEncoding(short encoding)
909     {
910         this.encoding = encoding;
911     }
912
913     /**
914      * Should only be used by HSSFSheet and friends. Returns the low level CellValueRecordInterface record
915      *
916      * @return CellValueRecordInterface representing the cell via the low level api.
917      */

918
919     protected CellValueRecordInterface getCellValueRecord()
920     {
921         return record;
922     }
923
924     /**
925      * @throws RuntimeException if the bounds are exceeded.
926      */

927     private void checkBounds(int cellNum) {
928       if (cellNum > 255) {
929           throw new RuntimeException JavaDoc("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 JavaDoc("You cannot reference columns with an index of less then 0.");
934       }
935     }
936     
937     /**
938      * Sets this cell as the active cell for the worksheet
939      */

940     public void setAsActiveCell()
941     {
942         this.sheet.setActiveCellRow(this.row);
943         this.sheet.setActiveCellCol(this.cellNum);
944     }
945 }
946
Popular Tags