Exemplo n.º 1
0
        /// <summary>
        /// Format specified cell
        /// </summary>
        /// <param name="cell">cell to be formatted</param>
        /// <param name="culture">culture for parsing</param>
        /// <returns>Formatted text used to display as cell content</returns>
        public string FormatCell(Cell cell, CultureInfo culture)
        {
            object data = cell.InnerData;

            // check numeric
            bool isNumeric = false;

            double value = 0;

            if (data is double)
            {
                value     = (double)data;
                isNumeric = true;
            }
            else if (data is int)
            {
                value     = (double)(int)data;
                isNumeric = true;
            }
            else if (data is long)
            {
                value = (double)(long)data;
            }
            else if (data is float)
            {
                value     = (double)(float)data;
                isNumeric = true;
            }
            else if (data is decimal)
            {
                value     = (double)(decimal)data;
                isNumeric = true;
            }
            else if (data is string str)
            {
                isNumeric = double.TryParse(str, NumberStyles.Any, culture, out value);

                if (isNumeric)
                {
                    cell.InnerData = value;
                }
            }
            else if (data is DateTime dt)
            {
                value     = dt.ToOADate();
                isNumeric = true;
            }

            if (isNumeric)
            {
                INumberFormatArgs arg = cell.DataFormatArgs as INumberFormatArgs;

                var numberPart = FormatNumberCellAndGetPattern(cell, ref value, arg);

                if (value < 0 &&
                    arg is NumberFormatArgs nargs &&
                    (arg.NegativeStyle & NumberNegativeStyle.CustomSymbol) == NumberNegativeStyle.CustomSymbol)
                {
                    numberPart = nargs.CustomNegativePrefix + numberPart + nargs.CustomNegativePostfix;
                }

                return(value.ToString(numberPart));
            }
            return(null);
        }
Exemplo n.º 2
0
        internal static string FormatNumberCellAndGetPattern(Cell cell, ref double value, INumberFormatArgs arg)
        {
            if (cell.InnerStyle.HAlign == ReoGridHorAlign.General)
            {
                cell.RenderHorAlign = ReoGridRenderHorAlign.Right;
            }

            short decimals     = 2;
            bool  useSeparator = true;
            NumberNegativeStyle negativeStyle = NumberNegativeStyle.Default;

            if (arg != null)
            {
                decimals      = arg.DecimalPlaces;
                useSeparator  = arg.UseSeparator;
                negativeStyle = arg.NegativeStyle;
            }

            if (value < 0)
            {
                if ((negativeStyle & NumberNegativeStyle.Red) == NumberNegativeStyle.Red)
                {
                    cell.RenderColor = SolidColor.Red;
                }
                else
                {
                    cell.RenderColor = SolidColor.Transparent;
                }
            }

            // decimal places
            string decimalPlacePart = new string('0', decimals);

            // number
            string numberPart = (useSeparator ? "#,##0." : "0.") + decimalPlacePart;

            if ((negativeStyle & NumberNegativeStyle.Brackets) == NumberNegativeStyle.Brackets)
            {
                numberPart = (value < 0) ? ("(" + numberPart + ")") : numberPart;
            }
            else if ((negativeStyle & NumberNegativeStyle.Prefix_Sankaku) == NumberNegativeStyle.Prefix_Sankaku)
            {
                numberPart = (value < 0) ? ("▲ " + numberPart) : numberPart;
            }

            // negative
            if ((negativeStyle & NumberNegativeStyle.Minus) == 0)
            {
                value = Math.Abs(value);
            }

            return(numberPart);
        }
Exemplo n.º 3
0
        internal static string FormatNumberCellAndGetPattern(Cell cell, ref double value, INumberFormatArgs arg)
        {
            if (cell.InnerStyle.HAlign == ReoGridHorAlign.General)
            {
                cell.RenderHorAlign = ReoGridRenderHorAlign.Right;
            }

            /*if (cell.DataFormat == CellDataFormatFlag.General)
             * {
             *      return Convert.ToString(value);
             * }*/

            short decimals     = 0;
            bool  useSeparator = false;
            NumberNegativeStyle negativeStyle = NumberNegativeStyle.Default;

            if (arg != null)
            {
                decimals      = arg.DecimalPlaces;
                useSeparator  = arg.UseSeparator;
                negativeStyle = arg.NegativeStyle;
            }

            // decimal places
            string decimalPlacePart = new string('0', decimals);

            // number
            string numberPart = (useSeparator ? "#,##0." : "0.") + decimalPlacePart;

            if ((negativeStyle & NumberNegativeStyle.CustomSymbol) == NumberNegativeStyle.DollarSymbol)
            {
                numberPart = "$" + numberPart;
            }
            if (value < 0)
            {
                if ((negativeStyle & NumberNegativeStyle.Red) != 0)
                {
                    cell.RenderColor = SolidColor.Red;
                }
                else
                {
                    cell.RenderColor = SolidColor.Transparent;
                }
                if ((negativeStyle & NumberNegativeStyle.Brackets) != 0)
                {
                    numberPart = "(" + numberPart + ")";
                }
                else if ((negativeStyle & NumberNegativeStyle.Prefix_Sankaku) != 0)
                {
                    numberPart = "▲ " + numberPart;
                }
                if ((negativeStyle & NumberNegativeStyle.Minus) == 0)
                {
                    value = Math.Abs(value);
                }
            }
            return(numberPart);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Format given cell
        /// </summary>
        /// <param name="cell">Instance of cell to be formatted</param>
        /// <returns></returns>
        public string FormatCell(Cell cell)
        {
            object data = cell.InnerData;

            // check numeric
            bool isNumeric = false;

            double value = 0;

            if (data is double)
            {
                value     = (double)data;
                isNumeric = true;
            }
            else if (data is int)
            {
                value     = (double)(int)data;
                isNumeric = true;
            }
            else if (data is long)
            {
                value = (double)(long)data;
            }
            else if (data is float)
            {
                value     = (double)(float)data;
                isNumeric = true;
            }
            else if (data is decimal)
            {
                value     = (double)(decimal)data;
                isNumeric = true;
            }
            else if (data is string)
            {
                string strdata = (data as string).Trim();

                isNumeric = double.TryParse(strdata, out value);

                if (!isNumeric)
                {
                    isNumeric = double.TryParse(strdata.Replace(",", ""), out value);
                }

                if (isNumeric)
                {
                    cell.InnerData = value;
                }
            }
            else if (data is DateTime)
            {
                value     = ((DateTime)data - new DateTime(1900, 1, 1)).TotalDays;
                isNumeric = true;
            }

            if (isNumeric)
            {
                string prefix  = null;
                string postfix = null;

                INumberFormatArgs arg = cell.DataFormatArgs as INumberFormatArgs;

                var numberPart = FormatNumberCellAndGetPattern(cell, ref value, arg);

                if (arg is NumberFormatArgs)
                {
                    NumberFormatArgs nargs = (NumberFormatArgs)cell.DataFormatArgs;
                    prefix  = nargs.CustomNegativePrefix;
                    postfix = nargs.CustomNegativePostfix;
                }

                if (arg != null &&
                    (arg.NegativeStyle & NumberNegativeStyle.CustomSymbol) == NumberNegativeStyle.CustomSymbol)
                {
                    numberPart = (value < 0) ? (prefix + numberPart + postfix) : numberPart;
                }

                return(value.ToString(numberPart));
            }
            else
            {
                return(null);
            }
        }