コード例 #1
0
ファイル: DataFormatter.cs プロジェクト: purehzj/npoi-1
        private FormatBase GetFormat(double cellValue, int formatIndex, String formatStr)
        {
            // Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format.
            if (emulateCsv && cellValue == 0.0 && formatStr.Contains("#") && !formatStr.Contains("0"))
            {
                formatStr = formatStr.Replace("#", "");
            }
            FormatBase format = (FormatBase)formats[formatStr];

            if (format != null)
            {
                return(format);
            }
            if (formatStr.Equals("General", StringComparison.CurrentCultureIgnoreCase) || "@".Equals(formatStr))
            {
                if (DataFormatter.IsWholeNumber(cellValue))
                {
                    return(generalWholeNumFormat);
                }
                return(generalDecimalNumFormat);
            }
            format             = CreateFormat(cellValue, formatIndex, formatStr);
            formats[formatStr] = format;
            return(format);
        }
コード例 #2
0
ファイル: DataFormatter.cs プロジェクト: thinhmascot/NPOI
        private FormatBase GetFormat(double cellValue, int formatIndex, String formatStr)
        {
            FormatBase format = (FormatBase)formats[formatStr];

            if (format != null)
            {
                return(format);
            }
            if (formatStr.Equals("General"))
            {
                if (DataFormatter.IsWholeNumber(cellValue))
                {
                    return(generalWholeNumFormat);
                }
                return(generalDecimalNumFormat);
            }
            format             = CreateFormat(cellValue, formatIndex, formatStr);
            formats[formatStr] = format;
            return(format);
        }
コード例 #3
0
        private FormatBase GetFormat(double cellValue, int formatIndex, String formatStrIn)
        {
            //      // Might be better to separate out the n p and z formats, falling back to p when n and z are not set.
            //      // That however would require other code to be re factored.
            //      String[] formatBits = formatStrIn.split(";");
            //      int i = cellValue > 0.0 ? 0 : cellValue < 0.0 ? 1 : 2;
            //      String formatStr = (i < formatBits.length) ? formatBits[i] : formatBits[0];

            String formatStr = formatStrIn;
            // Excel supports positive/negative/zero, but java
            // doesn't, so we need to do it specially
            int firstAt = formatStr.IndexOf(';');
            int lastAt  = formatStr.LastIndexOf(';');

            // p and p;n are ok by default. p;n;z and p;n;z;s need to be fixed.
            if (firstAt != -1 && firstAt != lastAt)
            {
                int secondAt = formatStr.IndexOf(';', firstAt + 1);
                if (secondAt == lastAt)
                { // p;n;z
                    if (cellValue == 0.0)
                    {
                        formatStr = formatStr.Substring(lastAt + 1);
                    }
                    else
                    {
                        formatStr = formatStr.Substring(0, lastAt);
                    }
                }
                else
                {
                    if (cellValue == 0.0)
                    { // p;n;z;s
                        formatStr = formatStr.Substring(secondAt + 1, lastAt - (secondAt + 1));
                    }
                    else
                    {
                        formatStr = formatStr.Substring(0, secondAt);
                    }
                }
            }

            // Excel's # with value 0 will output empty where Java will output 0. This hack removes the # from the format.
            if (emulateCsv && cellValue == 0.0 && formatStr.Contains("#") && !formatStr.Contains("0"))
            {
                formatStr = formatStr.Replace("#", "");
            }
            FormatBase format = (FormatBase)formats[formatStr];

            if (format != null)
            {
                return(format);
            }
            // Is it one of the special built in types, General or @?
            if (formatStr.Equals("General", StringComparison.CurrentCultureIgnoreCase) || "@".Equals(formatStr))
            {
                if (DataFormatter.IsWholeNumber(cellValue))
                {
                    return(generalWholeNumFormat);
                }
                return(generalDecimalNumFormat);
            }

            // Build a formatter, and cache it
            format             = CreateFormat(cellValue, formatIndex, formatStr);
            formats[formatStr] = format;
            return(format);
        }