如何在水晶报表中显示单词数量!

时间:2009-05-24 11:02:17

标签: .net crystal-reports

我正在使用Windows窗体应用程序。我需要在水晶报告中用文字显示数量。 如何在水晶报告中用文字显示金额?

3 个答案:

答案 0 :(得分:2)

在报告中创建一个公式字段,并使用内置功能的ToWords

答案 1 :(得分:2)

尝试使用此

IF {#TOTAL}<100 OR Remainder ({#TOTAL},100 )=0 then
''& ProperCase (towords ({#TOTAL},0))& 'Only ' 
ELSE
'' & ProperCase (towords ({#TOTAL}-Remainder ({#TOTAL},100 ),0)) & ' & ' & ProperCase(ToWords (Remainder ({#TOTAL},100 ),0))&' Only'

答案 2 :(得分:1)

您可以在类对象中编写自定义属性(如果将其绑定到对象数据源),该属性将返回单词中的amount值。

如果要将其绑定到表或从存储过程输出,则需要在结果集中向表中添加一列,并使用单词中的amount值填充它。

现在,您可以将标签绑定到列/属性,以单词显示金额。

编辑:代码示例(希望这可以帮助您)

class Program
{
    static void Main(string[] args)
    {
        Expenses e = new Expenses();
        e.Total = 137313959;
        Console.WriteLine(e.TotalInWords);
        Console.ReadLine();
    }

    public class Expenses
    {
        public int Total
        {
            get;
            set;
        }

        public string TotalInWords
        {
            get
            {
                return ConvertToWords(Total);
            }
        }

        private static string ConvertToWords(int amount)
        {
            string s = amount + "";
            int g = (s.Length / 3);//groups of three digits
            int a = (s.Length % 3);
            StringBuilder sb = new StringBuilder();

            if (g == 0) //only three or less digits
            {
                sb.Append(ConvertToHunderds(s, false));
            }
            else
            {
                if (a > 0)
                {
                    sb.AppendFormat("{0} {1} ", ConvertToHunderds(s.Substring(0, a),
                             false), groups[g]);
                }

                int idx = a;

                while (g > 0)
                {
                    sb.AppendFormat(" {0} {1} ", ConvertToHunderds(s.Substring(a, 3), 
                                     g == 1), groups[--g]);
                    a += 3;
                }
            }

            return sb.ToString();
        }

        private static string ConvertToHunderds(string s,bool useAnd)
        {
            char[] c =new char[s.Length];

            for (int i = s.Length-1,j=0; i >=0 && j<c.Length; j++, i--)
            {
                c[j] = s[i];
            }

            if (c.Length == 3)
            {
                if (c[2] == '0')
                    return string.Format("{0}{1}", (useAnd ? "and " : ""), GetTens(c[1], 
                             c[0]));
                else
                    return string.Format("{0} hundred {1} {2}", digits[((int)c[2]) -
                                       48],
                        (useAnd ? "and" : ""),
                        GetTens(c[1], c[0]));
            }
            else if(c.Length==2)
            {
                return useAnd ? "and " : "" + GetTens(c[1], c[0]);
            }
            else
            {
                return digits[((int)c[0]) - 48];
            }
        }

        private static string GetTens(char ten, char one)
        {
            if (one == '0')
                if (ten == '1')
                    return eleven2nineteen[((int)ten) - 49];
                else
                    return tens[((int)ten) - 49];

            if(ten=='1')
                return  eleven2nineteen[((int)one) - 49];

            return tens[((int)ten) - 49] //because tens[0] = 10 so subtract 49 (ascii 1)
                + " " 
                + digits[((int)one) - 48];
        }

        private static string[] groups = new[] { ""/*hundreds group*/, "thousand", 
                             "million", "billion", "trilliion" };
        private static string[] digits = new[] { "zero", "one", "two", "three", "four",
                  "five", "six", "seven", "eight", "nine" };
        private static string[] eleven2nineteen = new[] { "eleven", "twelve", 
                  "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", 
                   "eighteen", "nineteen" };
        private static string[] tens = new[] { "ten", "twenty", "thirty", "fourty", 
              "fifty", "sixty", "seventy", "eighty", "ninety" };
    }

此输出:

one hunderd  thirty seven million  three hunderd  thirteen thousand  nine 
hundred and fifty nine

PS: - 这仅用于说明目的。如果(曾)你使用它,请使用适当的异常处理等。