View | Details | Raw Unified | Return to bug 15303
Collapse All | Expand All

(-)org/apache/poi/hssf/usermodel/HSSFCell.java (-102 / +233 lines)
Lines 154-162 Link Here
154
    private short                    cellNum;
154
    private short                    cellNum;
155
    private int                      cellType;
155
    private int                      cellType;
156
    private HSSFCellStyle            cellStyle;
156
    private HSSFCellStyle            cellStyle;
157
    private double                   cellValue;
157
    private double					numericCellValue;
158
    private String                   stringValue;
158
    private String						stringCellValue;
159
    private boolean                  booleanValue;
159
    private boolean					booleanCellValue;
160
    private byte                     errorValue;
160
    private byte                     errorValue;
161
    private short                    encoding = ENCODING_COMPRESSED_UNICODE;
161
    private short                    encoding = ENCODING_COMPRESSED_UNICODE;
162
    private Workbook                 book;
162
    private Workbook                 book;
Lines 189-197 Link Here
189
        cellNum      = col;
189
        cellNum      = col;
190
        this.row     = row;
190
        this.row     = row;
191
        cellStyle    = null;
191
        cellStyle    = null;
192
        cellValue    = 0;
192
        numericCellValue    = 0;
193
        stringValue  = null;
193
        stringCellValue  = null;
194
        booleanValue = false;
194
        booleanCellValue = false;
195
        errorValue   = ( byte ) 0;
195
        errorValue   = ( byte ) 0;
196
        this.book    = book;
196
        this.book    = book;
197
        this.sheet   = sheet;
197
        this.sheet   = sheet;
Lines 230-238 Link Here
230
        this.row     = row;
230
        this.row     = row;
231
        cellType     = type;
231
        cellType     = type;
232
        cellStyle    = null;
232
        cellStyle    = null;
233
        cellValue    = 0;
233
        numericCellValue    = 0;
234
        stringValue  = null;
234
        stringCellValue  = null;
235
        booleanValue = false;
235
        booleanCellValue = false;
236
        errorValue   = ( byte ) 0;
236
        errorValue   = ( byte ) 0;
237
        this.book    = book;
237
        this.book    = book;
238
        this.sheet   = sheet;
238
        this.sheet   = sheet;
Lines 306-323 Link Here
306
        this.row    = row;
306
        this.row    = row;
307
        cellType    = determineType(cval);
307
        cellType    = determineType(cval);
308
        cellStyle   = null;
308
        cellStyle   = null;
309
        stringValue = null;
309
        stringCellValue = null;
310
        this.book   = book;
310
        this.book   = book;
311
        this.sheet  = sheet;
311
        this.sheet  = sheet;
312
        switch (cellType)
312
        switch (cellType)
313
        {
313
        {
314
314
315
            case CELL_TYPE_NUMERIC :
315
            case CELL_TYPE_NUMERIC :
316
                cellValue = (( NumberRecord ) cval).getValue();
316
                numericCellValue = (( NumberRecord ) cval).getValue();
317
                break;
317
                break;
318
318
319
            case CELL_TYPE_STRING :
319
            case CELL_TYPE_STRING :
320
                stringValue =
320
                stringCellValue =
321
                    book.getSSTString( ( (LabelSSTRecord ) cval).getSSTIndex());
321
                    book.getSSTString( ( (LabelSSTRecord ) cval).getSSTIndex());
322
                break;
322
                break;
323
323
Lines 325-335 Link Here
325
                break;
325
                break;
326
326
327
            case CELL_TYPE_FORMULA :
327
            case CELL_TYPE_FORMULA :
328
                cellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue();
328
                numericCellValue = (( FormulaRecordAggregate ) cval).getFormulaRecord().getValue();
329
                break;
329
                break;
330
330
331
            case CELL_TYPE_BOOLEAN :
331
            case CELL_TYPE_BOOLEAN :
332
                booleanValue = (( BoolErrRecord ) cval).getBooleanValue();
332
                booleanCellValue = (( BoolErrRecord ) cval).getBooleanValue();
333
                break;
333
                break;
334
334
335
            case CELL_TYPE_ERROR :
335
            case CELL_TYPE_ERROR :
Lines 632-638 Link Here
632
            setCellType(CELL_TYPE_NUMERIC, false);
632
            setCellType(CELL_TYPE_NUMERIC, false);
633
        }
633
        }
634
        (( NumberRecord ) record).setValue(value);
634
        (( NumberRecord ) record).setValue(value);
635
        cellValue = value;
635
        numericCellValue = value;
636
    }
636
    }
637
637
638
    /**
638
    /**
Lines 694-700 Link Here
694
                index = book.addSSTString(value, true);
694
                index = book.addSSTString(value, true);
695
            }
695
            }
696
            (( LabelSSTRecord ) record).setSSTIndex(index);
696
            (( LabelSSTRecord ) record).setSSTIndex(index);
697
            stringValue = value;
697
            stringCellValue = value;
698
        }
698
        }
699
    }
699
    }
700
700
Lines 732-827 Link Here
732
        return retval;
732
        return retval;
733
    }
733
    }
734
734
735
736
    /**
735
    /**
737
     * get the value of the cell as a number.  For strings we throw an exception.
736
     * Returns the value of the cell as a double.  The basic
738
     * For blank cells we return a 0.
737
     * rules that MS-Excel uses to convert cell values to
738
     * doubles are used.
739
     * <p>
740
     * If the cell is blank or String cells that cannot
741
     * be converted to a number a 0 is returned (like
742
     * MS-Excel does).
743
     * 
744
     * @throws NumberFormatException - 
745
     * 				If the cell value cannot be converted to a
746
     * 				double a NumberFormatException is thrown.
739
     */
747
     */
740
741
    public double getNumericCellValue()
748
    public double getNumericCellValue()
742
    {
749
    {
743
        if (cellType == CELL_TYPE_BLANK)
750
    	// Declare the return value here
744
        {
751
    	double dRV = (short) 0;
745
            return 0;
752
    	switch (cellType)
746
        }
753
   		{
747
        if (cellType == CELL_TYPE_STRING)
754
   			case CELL_TYPE_NUMERIC:
748
        {
755
	    	{
749
            throw new NumberFormatException(
756
	    		dRV = numericCellValue;
750
                "You cannot get a numeric value from a String based cell");
757
	    		break;
751
        }
758
	    	}
752
        if (cellType == CELL_TYPE_BOOLEAN)
759
   			case CELL_TYPE_BLANK:
753
        {
760
	        {
754
            throw new NumberFormatException(
761
	            // Return the default value but have
755
                "You cannot get a numeric value from a boolean cell");
762
	            // this check here for a break in the
756
        }
763
	            // if logic.
757
        if (cellType == CELL_TYPE_ERROR)
764
	            break;
758
        {
765
	        }
759
            throw new NumberFormatException(
766
   			case CELL_TYPE_STRING:
760
                "You cannot get a numeric value from an error cell");
767
	        {
761
        }
768
	        	try
762
        return cellValue;
769
	        	{
770
	        		dRV = Double.parseDouble(stringCellValue);
771
	        	}
772
	        	catch (NumberFormatException nfe)
773
	        	{
774
	        		// Ignored because MS-Excel returns
775
	        		// a 0 in this case and that is our
776
	        		// default return value.
777
	        	}
778
	        	break;
779
	        }
780
   			case CELL_TYPE_BOOLEAN:
781
	        {
782
	            // MS-Excel returns a -1 if true and a
783
	            // 0 if false.
784
	            dRV = booleanCellValue ? (short) -1 : (short) 0;
785
	            break;
786
	        }
787
   			case CELL_TYPE_ERROR:
788
	        {
789
	            throw new NumberFormatException(
790
	                "You cannot get a numeric value from an error cell");
791
	        }
792
        	default:
793
	        {
794
	            throw new NumberFormatException(
795
	                "You cannot get a numeric value for this cell type");
796
	        }
797
   		}
798
   		
799
        return numericCellValue;
763
    }
800
    }
764
801
765
    /**
802
    /**
766
     * get the value of the cell as a date.  For strings we throw an exception.
803
     * Returns the value of the cell as a date.  The basic
767
     * For blank cells we return a null.
804
     * rules that MS-Excel uses to convert cell values to
805
     * dates are used.
806
     * 
807
     * @throws NumberFormatException - 
808
     * 				If the cell value cannot be converted to a
809
     * 				date a NumberFormatException is thrown.
768
     */
810
     */
769
    public Date getDateCellValue()
811
    public Date getDateCellValue()
770
    {
812
    {
771
        if (cellType == CELL_TYPE_BLANK)
813
    	// Declare the return value so we always have it.
772
        {
814
    	Date dateRV = null;
773
            return null;
815
    	
774
        }
816
    	switch (cellType)
775
        if (cellType == CELL_TYPE_STRING)
817
    	{
776
        {
818
   			case CELL_TYPE_NUMERIC:
777
            throw new NumberFormatException(
819
	    	{
778
                "You cannot get a date value from a String based cell");
820
	    		dateRV = HSSFDateUtil.getJavaDate(numericCellValue);
779
        }
821
	    		break;
780
        if (cellType == CELL_TYPE_BOOLEAN)
822
	    	}
781
        {
823
   			case CELL_TYPE_BLANK:
782
            throw new NumberFormatException(
824
	        {
783
                "You cannot get a date value from a boolean cell");
825
	        	// MS-Excel returns 12:00AM - I'm not sure
784
        }
826
	        	// what to do since 12:00AM is not a date, but
785
        if (cellType == CELL_TYPE_ERROR)
827
	        	// just a time.
786
        {
828
	        	// So leave what POI is already doing.
787
            throw new NumberFormatException(
829
	            break;
788
                "You cannot get a date value from an error cell");
830
	        }
789
        }
831
   			case CELL_TYPE_STRING:
790
        if (book.isUsing1904DateWindowing()) {
832
	        {
791
            return HSSFDateUtil.getJavaDate(cellValue,true);
833
	        	// MS-Excel returns 12:00AM - I'm not sure
792
        }
834
	        	// what to do since 12:00AM is not a date, but
793
        else {
835
	        	// just a time.
794
            return HSSFDateUtil.getJavaDate(cellValue,false);
836
	        	// So leave what POI is already doing.
795
        }
837
	            throw new NumberFormatException(
838
	                "You cannot get a date value from a String based cell");
839
	        }
840
   			case CELL_TYPE_BOOLEAN:
841
	        {
842
	        	// MS-Excel returns 12:00AM if the boolean
843
	        	// value is false and null if it is true.
844
	        	// This MS values seem to be based that Excel
845
	        	// treats false as being 0 and true as -1.
846
	        	//
847
	        	// Because of this return the value returned by
848
	        	// HSSFDateUtil and put the number.
849
	        	double booleanAsDouble = booleanCellValue ? (short) -1 : (short) 0;
850
	        	dateRV = HSSFDateUtil.getJavaDate(booleanAsDouble);
851
	        	break;
852
	        }
853
   			case CELL_TYPE_ERROR:
854
	        {
855
	            throw new NumberFormatException(
856
	                "You cannot get a date value from an error cell");
857
	        }
858
	        default:
859
	        {
860
	            throw new NumberFormatException(
861
	                "You cannot get a date value for this cell type");
862
	        }
863
    	}
864
        
865
        return dateRV;
796
    }
866
    }
797
867
798
    /**
868
    /**
799
     * get the value of the cell as a string - for numeric cells we throw an exception.
869
     * Returns the value of the cell as a string - for numeric,
800
     * For blank cells we return an empty string.
870
     * boolean, or error type cells a to string conversion is
871
     * done.
801
     */
872
     */
802
803
    public String getStringCellValue()
873
    public String getStringCellValue()
804
    {
874
    {
805
        if (cellType == CELL_TYPE_BLANK)
875
    	String returnValue = "";
806
        {
876
    	
807
            return "";
877
    	switch (cellType)
808
        }
878
    	{
809
        if (cellType == CELL_TYPE_NUMERIC)
879
   			case CELL_TYPE_STRING:
810
        {
880
	    	{
811
            throw new NumberFormatException(
881
	    		returnValue = stringCellValue;
812
                "You cannot get a string value from a numeric cell");
882
	    		break;
813
        }
883
	    	}
814
        if (cellType == CELL_TYPE_BOOLEAN)
884
   			case CELL_TYPE_BLANK:
815
        {
885
	        {
816
            throw new NumberFormatException(
886
	        	// This is what MS-Excel does.
817
                "You cannot get a string value from a boolean cell");
887
	            returnValue = "";
818
        }
888
	            break;
819
        if (cellType == CELL_TYPE_ERROR)
889
	        }
820
        {
890
   			case CELL_TYPE_NUMERIC:
821
            throw new NumberFormatException(
891
	        {
822
                "You cannot get a string value from an error cell");
892
	        	returnValue = Double.toString(numericCellValue);
823
        }
893
	        	break;
824
        return stringValue;
894
	        }
895
   			case CELL_TYPE_BOOLEAN:
896
	        {
897
	            returnValue = new Boolean(booleanCellValue).toString();
898
	            break;
899
	        }
900
   			case CELL_TYPE_ERROR:
901
	        {
902
	        	byte[] byteArray = new byte[1];
903
	        	byteArray[0] = errorValue;
904
	            returnValue = new String(byteArray);
905
	            break;
906
	        }
907
	        default:
908
	        {
909
	        	// This is what POI has been doing for awhile
910
	        	// I'm not sure why it is doing this so lets
911
	        	// leave it for now.
912
	        	throw new NumberFormatException(
913
	        		"You cannot get a string value for this cell type");
914
	        }
915
    	}
916
        return returnValue;
825
    }
917
    }
826
918
827
    /**
919
    /**
Lines 839-845 Link Here
839
            setCellType(CELL_TYPE_BOOLEAN, false);
931
            setCellType(CELL_TYPE_BOOLEAN, false);
840
        }
932
        }
841
        (( BoolErrRecord ) record).setValue(value);
933
        (( BoolErrRecord ) record).setValue(value);
842
        booleanValue = value;
934
        booleanCellValue = value;
843
    }
935
    }
844
936
845
    /**
937
    /**
Lines 862-883 Link Here
862
    }
954
    }
863
955
864
    /**
956
    /**
865
     * get the value of the cell as a boolean.  For strings, numbers, and errors, we throw an exception.
957
     * Returns the value of the cell as a boolean.  The basic
866
     * For blank cells we return a false.
958
     * rules that MS-Excel uses to convert cell values to
959
     * boolean are used.
960
     * 
961
     * @throws NumberFormatException - 
962
     * 				If the cell value cannot be converted to a
963
     * 				boolean a NumberFormatException is thrown.
867
     */
964
     */
868
869
    public boolean getBooleanCellValue()
965
    public boolean getBooleanCellValue()
870
    {
966
    {
871
        if (cellType == CELL_TYPE_BOOLEAN)
967
    	// Declare the return value here and make it false.
872
        {
968
    	boolean bRV = false;
873
            return booleanValue;
969
    	
874
        }
970
        switch (cellType)
875
        if (cellType == CELL_TYPE_BLANK)
876
        {
971
        {
877
            return false;
972
   			case CELL_TYPE_BOOLEAN:
973
	        {
974
	            bRV = booleanCellValue;
975
	            break;
976
	        }
977
   			case CELL_TYPE_BLANK:
978
	        {
979
	            // We don't have do anything since the
980
	            // value is defaulted to false, but
981
	            // we have this else if block to
982
	            // act as a switch break.
983
	            break;
984
	        }
985
   			case CELL_TYPE_NUMERIC:
986
	        {
987
	        	// If the numeric value is equal to
988
	        	// one then we return true otherwise
989
	        	// we return false.  The reason we do
990
	        	// this is because this is what MS-Excel
991
	        	// does.
992
	        	bRV = numericCellValue == (short) 0 ? false: true;
993
	        	break;
994
	        }
995
   			case CELL_TYPE_STRING:
996
	        {
997
	        	// If the string value of the cell is
998
	        	// equal to "false" then the value is
999
	        	// consider false otherwise MS-Excel
1000
	        	// says it is true.
1001
	        	bRV = "false".equalsIgnoreCase(stringCellValue);
1002
	        	break;
1003
	        }
1004
        	default:
1005
	        {
1006
	        	throw new NumberFormatException(
1007
	        		"Cannot return a boolean value for this cell type.");
1008
	        }
878
        }
1009
        }
879
        throw new NumberFormatException(
1010
        
880
            "You cannot get a boolean value from a non-boolean cell");
1011
        return bRV;
881
    }
1012
    }
882
1013
883
    /**
1014
    /**

Return to bug 15303