Ejemplo n.º 1
0
        /**
         *
         * Returns the Formatted value of a cell as a <c>String</c> regardless
         * of the cell type. If the Excel FormatBase pattern cannot be Parsed then the
         * cell value will be Formatted using a default FormatBase.
         *
         * When passed a null or blank cell, this method will return an empty
         * String (""). Formula cells will be evaluated using the given
         * {@link HSSFFormulaEvaluator} if the evaluator is non-null. If the
         * evaluator is null, then the formula String will be returned. The caller
         * is responsible for setting the currentRow on the evaluator
         *
         *
         * @param cell The cell (can be null)
         * @param evaluator The HSSFFormulaEvaluator (can be null)
         * @return a string value of the cell
         */

        public string FormatCellValue(ICell cell, IFormulaEvaluator evaluator)
        {
            if (cell == null)
            {
                return("");
            }

            CellType cellType = cell.CellType;

            if (evaluator != null && cellType == CellType.Formula)
            {
                if (evaluator == null)
                {
                    return(cell.CellFormula);
                }
                cellType = evaluator.EvaluateFormulaCell(cell);
            }
            switch (cellType)
            {
            case CellType.Formula:
                // should only occur if evaluator is null
                return(cell.CellFormula);

            case CellType.Numeric:

                if (DateUtil.IsCellDateFormatted(cell))
                {
                    return(GetFormattedDateString(cell));
                }
                return(GetFormattedNumberString(cell));

            case CellType.String:
                return(cell.RichStringCellValue.String);

            case CellType.Boolean:
                return(cell.BooleanCellValue.ToString().ToUpper());

            case CellType.Blank:
                return("");

            case CellType.Error:
                return(FormulaError.ForInt(cell.ErrorCellValue).String);
            }
            throw new Exception("Unexpected celltype (" + cellType + ")");
        }
Ejemplo n.º 2
0
        static FormulaError()
        {
            _values = new FormulaError[] {
                FormulaError.NULL,
                FormulaError.DIV0,
                FormulaError.VALUE,
                FormulaError.REF,
                FormulaError.NAME,
                FormulaError.NUM,
                FormulaError.NA,
                FormulaError.CIRCULAR_REF,
                FormulaError.FUNCTION_NOT_IMPLEMENTED
            };

            foreach (FormulaError error in _values)
            {
                bmap.Add(error.Code, error);
                imap.Add(error.LongCode, error);
                smap.Add(error.String, error);
            }
        }