hssfcell.java

来自「EXCEL read and write」· Java 代码 · 共 1,104 行 · 第 1/3 页

JAVA
1,104
字号
                record = lrec;                break;            case CELL_TYPE_BLANK :                BlankRecord brec = null;                if (cellType != this.cellType)                {                    brec = new BlankRecord();                }                else                {                    brec = ( BlankRecord ) record;                }                brec.setColumn(col);                // During construction the cellStyle may be null for a Blank cell.                brec.setXFIndex(styleIndex);                brec.setRow(row);                record = brec;                break;            case CELL_TYPE_BOOLEAN :                BoolErrRecord boolRec = null;                if (cellType != this.cellType)                {                    boolRec = new BoolErrRecord();                }                else                {                    boolRec = ( BoolErrRecord ) record;                }                boolRec.setColumn(col);                if (setValue)                {                    boolRec.setValue(convertCellValueToBoolean());                }                boolRec.setXFIndex(styleIndex);                boolRec.setRow(row);                record = boolRec;                break;            case CELL_TYPE_ERROR :                BoolErrRecord errRec = null;                if (cellType != this.cellType)                {                    errRec = new BoolErrRecord();                }                else                {                    errRec = ( BoolErrRecord ) record;                }                errRec.setColumn(col);                if (setValue)                {                    errRec.setValue((byte)HSSFErrorConstants.ERROR_VALUE);                }                errRec.setXFIndex(styleIndex);                errRec.setRow(row);                record = errRec;                break;        }        if (cellType != this.cellType &&            this.cellType!=-1 )  // Special Value to indicate an uninitialized Cell        {            sheet.getSheet().replaceValueRecord(record);        }        this.cellType = cellType;    }    /**     * get the cells type (numeric, formula or string)     * @see #CELL_TYPE_STRING     * @see #CELL_TYPE_NUMERIC     * @see #CELL_TYPE_FORMULA     * @see #CELL_TYPE_BOOLEAN     * @see #CELL_TYPE_ERROR     */    public int getCellType()    {        return cellType;    }    /**     * set a numeric value for the cell     *     * @param value  the numeric value to set this cell to.  For formulas we'll set the     *        precalculated value, for numerics we'll set its value. For other types we     *        will change the cell to a numeric cell and set its value.     */    public void setCellValue(double value) {        int row=record.getRow();        short col=record.getColumn();        short styleIndex=record.getXFIndex();        switch (cellType) {            default:                setCellType(CELL_TYPE_NUMERIC, false, row, col, styleIndex);            case CELL_TYPE_ERROR:                (( NumberRecord ) record).setValue(value);                break;            case CELL_TYPE_FORMULA:                ((FormulaRecordAggregate)record).getFormulaRecord().setValue(value);                break;        }    }    /**     * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as     * a date.     *     * @param value  the date value to set this cell to.  For formulas we'll set the     *        precalculated value, for numerics we'll set its value. For other types we     *        will change the cell to a numeric cell and set its value.     */    public void setCellValue(Date value)    {        setCellValue(HSSFDateUtil.getExcelDate(value, this.book.getWorkbook().isUsing1904DateWindowing()));    }    /**     * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as     * a date.     *     * This will set the cell value based on the Calendar's timezone. As Excel     * does not support timezones this means that both 20:00+03:00 and     * 20:00-03:00 will be reported as the same value (20:00) even that there     * are 6 hours difference between the two times. This difference can be     * preserved by using <code>setCellValue(value.getTime())</code> which will     * automatically shift the times to the default timezone.     *     * @param value  the date value to set this cell to.  For formulas we'll set the     *        precalculated value, for numerics we'll set its value. For othertypes we     *        will change the cell to a numeric cell and set its value.     */    public void setCellValue(Calendar value)    {        setCellValue( HSSFDateUtil.getExcelDate(value, this.book.getWorkbook().isUsing1904DateWindowing()) );    }    /**     * set a string value for the cell. Please note that if you are using     * full 16 bit unicode you should call <code>setEncoding()</code> first.     *     * @param value  value to set the cell to.  For formulas we'll set the formula     * string, for String cells we'll set its value.  For other types we will     * change the cell to a string cell and set its value.     * If value is null then we will change the cell to a Blank cell.     * @deprecated Use setCellValue(HSSFRichTextString) instead.     */    public void setCellValue(String value)    {      HSSFRichTextString str = new HSSFRichTextString(value);      setCellValue(str);    }    /**     * set a string value for the cell. Please note that if you are using     * full 16 bit unicode you should call <code>setEncoding()</code> first.     *     * @param value  value to set the cell to.  For formulas we'll set the formula     * string, for String cells we'll set its value.  For other types we will     * change the cell to a string cell and set its value.     * If value is null then we will change the cell to a Blank cell.     */    public void setCellValue(HSSFRichTextString value) {        int row=record.getRow();        short col=record.getColumn();        short styleIndex=record.getXFIndex();        if (value == null) {            setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);            return;        }        if (cellType == CELL_TYPE_FORMULA) {            // Set the 'pre-evaluated result' for the formula            // note - formulas do not preserve text formatting.            FormulaRecordAggregate fr = (FormulaRecordAggregate) record;            fr.setCachedStringResult(value.getString());            // Update our local cache to the un-formatted version            stringValue = new HSSFRichTextString(value.getString());            // All done            return;        }        // If we get here, we're not dealing with a formula,        //  so handle things as a normal rich text cell        if (cellType != CELL_TYPE_STRING) {            setCellType(CELL_TYPE_STRING, false, row, col, styleIndex);        }        int index = 0;        UnicodeString str = value.getUnicodeString();        index = book.getWorkbook().addSSTString(str);        (( LabelSSTRecord ) record).setSSTIndex(index);        stringValue = value;        stringValue.setWorkbookReferences(book.getWorkbook(), (( LabelSSTRecord ) record));        stringValue.setUnicodeString(book.getWorkbook().getSSTString(index));    }    public void setCellFormula(String formula) {        int row=record.getRow();        short col=record.getColumn();        short styleIndex=record.getXFIndex();        if (formula==null) {            setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);            return;        }        setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);        FormulaRecordAggregate rec = (FormulaRecordAggregate) record;        FormulaRecord frec = rec.getFormulaRecord();        frec.setOptions((short) 2);        frec.setValue(0);        //only set to default if there is no extended format index already set        if (rec.getXFIndex() == (short)0) {            rec.setXFIndex((short) 0x0f);        }        Ptg[] ptgs = HSSFFormulaParser.parse(formula, book);        frec.setParsedExpression(ptgs);    }    public String getCellFormula() {        return HSSFFormulaParser.toFormulaString(book, ((FormulaRecordAggregate)record).getFormulaRecord().getParsedExpression());    }    /**     * Used to help format error messages     */    private static String getCellTypeName(int cellTypeCode) {        switch (cellTypeCode) {            case CELL_TYPE_BLANK:   return "blank";            case CELL_TYPE_STRING:  return "text";            case CELL_TYPE_BOOLEAN: return "boolean";            case CELL_TYPE_ERROR:   return "error";            case CELL_TYPE_NUMERIC: return "numeric";            case CELL_TYPE_FORMULA: return "formula";        }        return "#unknown cell type (" + cellTypeCode + ")#";    }    private static RuntimeException typeMismatch(int expectedTypeCode, int actualTypeCode, boolean isFormulaCell) {        String msg = "Cannot get a "            + getCellTypeName(expectedTypeCode) + " value from a "            + getCellTypeName(actualTypeCode) + " " + (isFormulaCell ? "formula " : "") + "cell";        return new IllegalStateException(msg);    }    private static void checkFormulaCachedValueType(int expectedTypeCode, FormulaRecord fr) {        int cachedValueType = fr.getCachedResultType();        if (cachedValueType != expectedTypeCode) {            throw typeMismatch(expectedTypeCode, cachedValueType, true);        }    }    /**     * Get the value of the cell as a number.     * For strings we throw an exception.     * For blank cells we return a 0.     * See {@link HSSFDataFormatter} for turning this     *  number into a string similar to that which     *  Excel would render this number as.     */    public double getNumericCellValue() {        switch(cellType) {            case CELL_TYPE_BLANK:                return 0.0;            case CELL_TYPE_NUMERIC:                return ((NumberRecord)record).getValue();            default:                throw typeMismatch(CELL_TYPE_NUMERIC, cellType, false);            case CELL_TYPE_FORMULA:                break;        }        FormulaRecord fr = ((FormulaRecordAggregate)record).getFormulaRecord();        checkFormulaCachedValueType(CELL_TYPE_NUMERIC, fr);        return fr.getValue();    }    /**     * Get the value of the cell as a date.     * For strings we throw an exception.     * For blank cells we return a null.     * See {@link HSSFDataFormatter} for formatting     *  this date into a string similar to how excel does.     */    public Date getDateCellValue() {        if (cellType == CELL_TYPE_BLANK) {            return null;        }        double value = getNumericCellValue();        if (book.getWorkbook().isUsing1904DateWindowing()) {            return HSSFDateUtil.getJavaDate(value, true);        }        return HSSFDateUtil.getJavaDate(value, false);    }    /**     * get the value of the cell as a string - for numeric cells we throw an exception.     * For blank cells we return an empty string.     * For formulaCells that are not string Formulas, we return empty String     * @deprecated Use the HSSFRichTextString return     */    public String getStringCellValue()    {      HSSFRichTextString str = getRichStringCellValue();      return str.getString();    }    /**     * get the value of the cell as a string - for numeric cells we throw an exception.     * For blank cells we return an empty string.     * For formulaCells that are not string Formulas, we return empty String     */    public HSSFRichTextString getRichStringCellValue() {        switch(cellType) {            case CELL_TYPE_BLANK:                return new HSSFRichTextString("");            case CELL_TYPE_STRING:                return stringValue;            default:                throw typeMismatch(CELL_TYPE_STRING, cellType, false);            case CELL_TYPE_FORMULA:                break;        }        FormulaRecordAggregate fra = ((FormulaRecordAggregate)record);        checkFormulaCachedValueType(CELL_TYPE_STRING, fra.getFormulaRecord());        String strVal = fra.getStringValue();        return new HSSFRichTextString(strVal == null ? "" : strVal);    }    /**     * set a boolean value for the cell     *     * @param value the boolean value to set this cell to.  For formulas we'll set the     *        precalculated value, for booleans we'll set its value. For other types we     *        will change the cell to a boolean cell and set its value.     */    public void setCellValue(boolean value) {        int row=record.getRow();        short col=record.getColumn();        short styleIndex=record.getXFIndex();        switch (cellType) {            default:                setCellType(CELL_TYPE_BOOLEAN, false, row, col, styleIndex);            case CELL_TYPE_ERROR:                (( BoolErrRecord ) record).setValue(value);                break;            case CELL_TYPE_FORMULA:                ((FormulaRecordAggregate)record).getFormulaRecord().setCachedResultBoolean(value);                break;        }    }    /**     * set a error value for the cell     *

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?