FastReportScript.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace PTMedicalInsurance.Common
  9. {
  10. public class FastReportFunction
  11. {
  12. /// <summary>
  13. /// 兼容方法
  14. /// </summary>
  15. /// <returns></returns>
  16. public static string moneyToChinese(object money)
  17. {
  18. return MoneyToUpper(string.Format("{0}", money));
  19. }
  20. /// <summary>
  21. /// 金额转换成中文大写金额
  22. /// </summary>
  23. /// <param name="LowerMoney">eg:10.74</param>
  24. /// <returns></returns>
  25. public static string MoneyToUpper(string LowerMoney)
  26. {
  27. string functionReturnValue = null;
  28. bool IsNegative = false; // 是否是负数
  29. if (LowerMoney.Trim().Substring(0, 1) == "-")
  30. {
  31. // 是负数则先转为正数
  32. LowerMoney = LowerMoney.Trim().Remove(0, 1);
  33. IsNegative = true;
  34. }
  35. string strLower = null;
  36. string strUpart = null;
  37. string strUpper = null;
  38. int iTemp = 0;
  39. // 保留两位小数 123.489→123.49  123.4→123.4
  40. LowerMoney = Math.Round(double.Parse(LowerMoney), 2).ToString();
  41. if (LowerMoney.IndexOf(".") > 0)
  42. {
  43. if (LowerMoney.IndexOf(".") == LowerMoney.Length - 2)
  44. {
  45. LowerMoney = LowerMoney + "0";
  46. }
  47. }
  48. else
  49. {
  50. LowerMoney = LowerMoney + ".00";
  51. }
  52. strLower = LowerMoney;
  53. iTemp = 1;
  54. strUpper = "";
  55. while (iTemp <= strLower.Length)
  56. {
  57. switch (strLower.Substring(strLower.Length - iTemp, 1))
  58. {
  59. case ".":
  60. strUpart = "圆";
  61. break;
  62. case "0":
  63. strUpart = "零";
  64. break;
  65. case "1":
  66. strUpart = "壹";
  67. break;
  68. case "2":
  69. strUpart = "贰";
  70. break;
  71. case "3":
  72. strUpart = "叁";
  73. break;
  74. case "4":
  75. strUpart = "肆";
  76. break;
  77. case "5":
  78. strUpart = "伍";
  79. break;
  80. case "6":
  81. strUpart = "陆";
  82. break;
  83. case "7":
  84. strUpart = "柒";
  85. break;
  86. case "8":
  87. strUpart = "捌";
  88. break;
  89. case "9":
  90. strUpart = "玖";
  91. break;
  92. }
  93. switch (iTemp)
  94. {
  95. case 1:
  96. strUpart = strUpart + "分";
  97. break;
  98. case 2:
  99. strUpart = strUpart + "角";
  100. break;
  101. case 3:
  102. strUpart = strUpart + "";
  103. break;
  104. case 4:
  105. strUpart = strUpart + "";
  106. break;
  107. case 5:
  108. strUpart = strUpart + "拾";
  109. break;
  110. case 6:
  111. strUpart = strUpart + "佰";
  112. break;
  113. case 7:
  114. strUpart = strUpart + "仟";
  115. break;
  116. case 8:
  117. strUpart = strUpart + "万";
  118. break;
  119. case 9:
  120. strUpart = strUpart + "拾";
  121. break;
  122. case 10:
  123. strUpart = strUpart + "佰";
  124. break;
  125. case 11:
  126. strUpart = strUpart + "仟";
  127. break;
  128. case 12:
  129. strUpart = strUpart + "亿";
  130. break;
  131. case 13:
  132. strUpart = strUpart + "拾";
  133. break;
  134. case 14:
  135. strUpart = strUpart + "佰";
  136. break;
  137. case 15:
  138. strUpart = strUpart + "仟";
  139. break;
  140. case 16:
  141. strUpart = strUpart + "万";
  142. break;
  143. default:
  144. strUpart = strUpart + "";
  145. break;
  146. }
  147. strUpper = strUpart + strUpper;
  148. iTemp = iTemp + 1;
  149. }
  150. strUpper = strUpper.Replace("零拾", "零");
  151. strUpper = strUpper.Replace("零佰", "零");
  152. strUpper = strUpper.Replace("零仟", "零");
  153. strUpper = strUpper.Replace("零零零", "零");
  154. strUpper = strUpper.Replace("零零", "零");
  155. strUpper = strUpper.Replace("零角零分", "整");
  156. strUpper = strUpper.Replace("零分", "整");
  157. strUpper = strUpper.Replace("零角", "零");
  158. strUpper = strUpper.Replace("零亿零万零圆", "亿圆");
  159. strUpper = strUpper.Replace("亿零万零圆", "亿圆");
  160. strUpper = strUpper.Replace("零亿零万", "亿");
  161. strUpper = strUpper.Replace("零万零圆", "万圆");
  162. strUpper = strUpper.Replace("零亿", "亿");
  163. strUpper = strUpper.Replace("零万", "万");
  164. strUpper = strUpper.Replace("零圆", "圆");
  165. strUpper = strUpper.Replace("零零", "零");
  166. // 对壹圆以下的金额的处理
  167. if (strUpper.Substring(0, 1) == "圆")
  168. {
  169. strUpper = strUpper.Substring(1, strUpper.Length - 1);
  170. }
  171. if (strUpper.Substring(0, 1) == "零")
  172. {
  173. strUpper = strUpper.Substring(1, strUpper.Length - 1);
  174. }
  175. if (strUpper.Substring(0, 1) == "角")
  176. {
  177. strUpper = strUpper.Substring(1, strUpper.Length - 1);
  178. }
  179. if (strUpper.Substring(0, 1) == "分")
  180. {
  181. strUpper = strUpper.Substring(1, strUpper.Length - 1);
  182. }
  183. if (strUpper.Substring(0, 1) == "整")
  184. {
  185. strUpper = "零圆整";
  186. }
  187. functionReturnValue = strUpper;
  188. if (IsNegative == true)
  189. {
  190. return "负" + functionReturnValue;
  191. }
  192. else
  193. {
  194. return functionReturnValue;
  195. }
  196. }
  197. /// <summary>
  198. /// 取两点时间之间的天数、小时、分钟、秒钟
  199. /// </summary>
  200. /// <param name="DateTime1"></param>
  201. /// <param name="DateTime2"></param>
  202. /// <returns></returns>
  203. public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
  204. {
  205. string dateDiff = null;
  206. TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
  207. TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
  208. TimeSpan ts = ts1.Subtract(ts2).Duration();
  209. //dateDiff = ts.Days.ToString() + "天"
  210. // + ts.Hours.ToString() + "小时"
  211. // + ts.Minutes.ToString() + "分钟"
  212. // + ts.Seconds.ToString() + "秒";
  213. if (ts.Days == 0)
  214. dateDiff = "1天";
  215. else
  216. dateDiff = ts.Days.ToString() + "天";
  217. return dateDiff;
  218. }
  219. public static void Register()
  220. {
  221. Type funType = typeof(FastReportFunction);
  222. MethodInfo methodUpper = funType.GetMethod("MoneyToUpper");
  223. MethodInfo methodChinese = funType.GetMethod("moneyToChinese");
  224. if (!FastReport.Utils.RegisteredObjects.IsTypeRegistered(funType))
  225. {
  226. FastReport.Utils.RegisteredObjects.Add(funType, "Functions,CustomFunctions",0x34);
  227. if(methodUpper != null)
  228. {
  229. FastReport.Utils.RegisteredObjects.AddFunction(methodUpper, "CustomFunctions");
  230. }
  231. if(methodChinese != null)
  232. {
  233. FastReport.Utils.RegisteredObjects.AddFunction(methodChinese, "CustomFunctions");
  234. }
  235. }
  236. }
  237. }
  238. }