PlanComm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Runtime.Serialization;
  12. using System.Runtime.Serialization.Formatters.Binary;
  13. using Microsoft.Office.Interop.Excel; //APP
  14. using System.Xml;
  15. using System.Data.SqlClient;
  16. using System.Diagnostics;
  17. using System.Reflection; //MISSIN
  18. using Infragistics.Win.UltraWinGrid;
  19. using Infragistics.Win;
  20. using Infragistics.Win.UltraWinGrid.ExcelExport;
  21. using Infragistics.Excel;
  22. namespace Core.StlMes.Client.Plan.Order
  23. {
  24. public class PlanComm
  25. {
  26. public static double PI = 3.1415926;
  27. /// <summary>
  28. /// 管米单重计算(t/m)
  29. /// </summary>
  30. /// <param name="outdiameter">外径(mm)</param>
  31. /// <param name="aimwallthick">壁厚(mm)</param>
  32. /// <returns></returns>
  33. public static double WeightOfMi(double outdiameter, double aimwallthick)
  34. {
  35. double weightOfmi = 0.02466 * aimwallthick * (outdiameter - aimwallthick) / 1000;
  36. return weightOfmi;
  37. }
  38. /// <summary>
  39. /// 管坯米单重(t/m)
  40. /// </summary>
  41. /// <param name="outdiameter">管坯断面(mm)</param>
  42. /// <returns></returns>
  43. public static double GpweightOfmi(double outdiameter)
  44. {
  45. return (7.8 * PI / 4 * outdiameter * outdiameter) / 1000 / 1000;
  46. }
  47. /// <summary>
  48. /// 单位英尺长度转米
  49. /// </summary>
  50. /// <param name="lenth">英尺长度</param>
  51. /// <returns>米长度</returns>
  52. public static double FootoMi(double lenth)
  53. {
  54. double converate = 0.3048; //换算率
  55. return Math.Round((lenth * converate), 2);
  56. }
  57. /// <summary>
  58. /// 导出
  59. /// </summary>
  60. public static void Export(ref UltraGrid myGrid1, string strFileName)
  61. {
  62. try
  63. {
  64. if (myGrid1.Rows.Count == 0) return;
  65. if (strFileName.Length == 0) strFileName = "未命名";
  66. SaveFileDialog dlg = new SaveFileDialog();
  67. dlg.Title = "保存";
  68. dlg.OverwritePrompt = true;
  69. dlg.Filter = "Excel文件(*.xls)|*.xls";
  70. dlg.AddExtension = true;
  71. dlg.FileName = strFileName;
  72. if (dlg.ShowDialog() == DialogResult.OK)
  73. {
  74. strFileName = dlg.FileName;
  75. using (UltraGridExcelExporter ultraGridExcelExporter1 = new UltraGridExcelExporter())
  76. {
  77. ultraGridExcelExporter1.Export(myGrid1, strFileName);
  78. }
  79. if (MessageBox.Show("数据导出成功!\n需要打开所导出文件吗?", "提示",
  80. MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  81. {
  82. ProcessStartInfo p = new ProcessStartInfo(strFileName);
  83. p.WorkingDirectory = Path.GetDirectoryName(strFileName);
  84. Process.Start(p);
  85. }
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. MessageBox.Show(ex.Message);
  91. }
  92. }
  93. /// <summary>
  94. /// 设置grid单元格可编辑
  95. /// </summary>
  96. /// <param name="band">grid表格</param>
  97. /// <param name="strs">可编辑的列头名称数组</param>
  98. public static void setGridActivation(UltraGridBand band, params string[] strs)
  99. {
  100. foreach (UltraGridColumn column in band.Columns)
  101. {
  102. if (!strs.Contains(column.Key))
  103. {
  104. column.CellActivation = Activation.ActivateOnly;
  105. }
  106. else
  107. {
  108. column.CellActivation = Activation.AllowEdit;
  109. column.CellAppearance.BackColor = Color.FromArgb(255, 255, 128);
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 表格下拉框,可以有ValueList来提供,可以选择的样式
  115. /// </summary>
  116. /// <param name="table"></param>
  117. /// <param name="strKey"></param>
  118. /// <param name="strText"></param>
  119. /// <returns></returns>
  120. public static ValueList GeneralValuelist(ref System.Data.DataTable table, string strKey, string strText)
  121. {
  122. if (table == null || !table.Columns.Contains(strKey) || !table.Columns.Contains(strText))
  123. {
  124. return null;
  125. }
  126. ArrayList alist = new ArrayList();
  127. ValueList vlist = new ValueList();
  128. for (int i = 0; i < table.Rows.Count; i++)
  129. {
  130. try
  131. {
  132. if (!alist.Contains(table.Rows[i][strKey]))
  133. {
  134. alist.Add(table.Rows[i][strKey]);
  135. vlist.ValueListItems.Add(table.Rows[i][strKey], Convert.ToString(table.Rows[i][strText]));
  136. }
  137. }
  138. catch { }
  139. }
  140. return vlist;
  141. }
  142. /// <summary>
  143. /// 自定义对象深拷贝
  144. /// </summary>
  145. /// <typeparam name="T">类名</typeparam>
  146. /// <param name="RealObject">源对象</param>
  147. /// <returns></returns>
  148. public static T Clone<T>(T RealObject)
  149. {
  150. using (Stream objectStream = new MemoryStream())
  151. {
  152. //利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
  153. IFormatter formatter = new BinaryFormatter();
  154. formatter.Serialize(objectStream, RealObject);
  155. objectStream.Seek(0, SeekOrigin.Begin);
  156. return (T)formatter.Deserialize(objectStream);
  157. }
  158. }
  159. /// <summary>
  160. /// 键盘按下Ctrl+D,拷贝活动单元格的数据到所有已勾选的此列的单元格中
  161. /// </summary>
  162. /// <param name="ugrid">UltraGrid</param>
  163. /// <param name="e"></param>
  164. /// <param name="strs">可以进行列名称</param>
  165. public static void setGridCopyActColumn(UltraGrid ugrid, KeyEventArgs e, params string[] strs)
  166. {
  167. if (e.Control && e.KeyCode == Keys.D)
  168. {
  169. if (ugrid != null && ugrid.ActiveRow != null)
  170. {
  171. UltraGridRow ugr = ugrid.ActiveRow;
  172. foreach (string colName in strs)
  173. {
  174. if (ugrid.ActiveCell.Column.Key.Equals(colName))
  175. {
  176. if (ugr.Cells[colName].Activation != Activation.AllowEdit)
  177. {
  178. return;
  179. }
  180. ugrid.UpdateData();
  181. ArrayList list = new ArrayList();
  182. IQueryable<UltraGridRow> checkRows = ugrid.Rows.AsQueryable().Where(" CHECK = 'True' ");
  183. if (checkRows.Count() == 0)
  184. {
  185. return;
  186. }
  187. foreach (UltraGridRow uRow in checkRows)
  188. {
  189. if (uRow != ugr && uRow.Cells[colName].Activation == Activation.AllowEdit)
  190. {
  191. uRow.Cells[colName].Value = ugr.Cells[colName].Value;
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// 键盘按下Ctrl+D,拷贝活动单元格的数据到所有已勾选的此列的单元格中
  201. /// </summary>
  202. /// <param name="ugrid">UltraGrid</param>
  203. /// <param name="key">选择列名(例如"CHC")</param>
  204. /// <param name="e"></param>
  205. /// <param name="strs">可以进行列名称</param>
  206. public static void setGridCopyActColumn(UltraGrid ugrid, string key, KeyEventArgs e, params string[] strs)
  207. {
  208. if (e.Control && e.KeyCode == Keys.D)
  209. {
  210. if (ugrid != null && ugrid.ActiveRow != null)
  211. {
  212. UltraGridRow ugr = ugrid.ActiveRow;
  213. foreach (string colName in strs)
  214. {
  215. if (ugrid.ActiveCell.Column.Key.Equals(colName))
  216. {
  217. if (ugr.Cells[colName].Activation != Activation.AllowEdit)
  218. {
  219. return;
  220. }
  221. ugrid.UpdateData();
  222. ArrayList list = new ArrayList();
  223. IQueryable<UltraGridRow> checkRows = ugrid.Rows.AsQueryable().Where(key + " = 'True'");
  224. if (checkRows.Count() == 0)
  225. {
  226. return;
  227. }
  228. foreach (UltraGridRow uRow in checkRows)
  229. {
  230. if (uRow != ugr && uRow.Cells[colName].Activation == Activation.AllowEdit)
  231. {
  232. uRow.Cells[colName].Value = ugr.Cells[colName].Value;
  233. }
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// 返回table的column列中是否存在str
  242. /// </summary>
  243. /// <param name="dt">datable</param>
  244. /// <param name="column">列头名</param>
  245. /// <param name="str">匹配字符串</param>
  246. /// <returns></returns>
  247. public static bool isInDataTable(System.Data.DataTable dt, string column, string str)
  248. {
  249. bool isInTable = false;
  250. if (dt == null || column.Equals(""))
  251. {
  252. return isInTable;
  253. }
  254. if (!dt.Columns.Contains(column))
  255. {
  256. return isInTable;
  257. }
  258. foreach(DataRow dr in dt.Rows)
  259. {
  260. if (dr[column].ToString().Equals(str))
  261. {
  262. isInTable = true;
  263. }
  264. }
  265. return isInTable;
  266. }
  267. /// <summary>
  268. /// 设置数字类型列千分位显示,小数点保留位数
  269. /// </summary>
  270. /// <param name="band">UltraGridBand</param>
  271. /// <param name="maxDigit">数字最大位数(如:99999.99,最大位数5位)</param>
  272. /// <param name="digit">保留小数位数(如:99999.99,保留2位小数)</param>
  273. /// <param name="strs">数字单元列</param>
  274. public static void setGridDigitalColMaxInput(UltraGridBand band, int maxDigit, int digit, params string[] strs)
  275. {
  276. if (band == null)
  277. {
  278. return;
  279. }
  280. string maskInput = "{LOC}";
  281. if (maxDigit > 0)
  282. {
  283. string str = "";
  284. for (int i = 0; i < maxDigit; i++)
  285. {
  286. if (i != 0 && (i % 3) == 0)
  287. {
  288. str = "," + str;
  289. }
  290. str = "n" + str;
  291. }
  292. maskInput = maskInput + str;
  293. if (digit > 0)
  294. {
  295. maskInput += ".";
  296. for (int i = 0; i < digit; i++)
  297. {
  298. maskInput += "n";
  299. }
  300. }
  301. foreach (UltraGridColumn column in band.Columns)
  302. {
  303. if (strs.Contains(column.Key))
  304. {
  305. column.MaskDisplayMode = Infragistics.Win.UltraWinMaskedEdit.MaskMode.IncludeLiterals;
  306. column.MaskInput = maskInput;
  307. }
  308. }
  309. }
  310. }
  311. /// <summary>
  312. /// 设置Grid列是数字类型,显示样式
  313. /// </summary>
  314. /// <param name="band">UltraGridBand</param>
  315. /// <param name="maxDigit">数字最大位数(如:99999.99,最大位数5位)</param>
  316. /// <param name="digit">保留小数位数(如:99999.99,保留2位小数)</param>
  317. /// <param name="strs">数字单元列</param>
  318. public static void setGridDigitalCol(UltraGridBand band, int maxDigit, int digit, params string[] strs)
  319. {
  320. if (band == null)
  321. {
  322. return;
  323. }
  324. string maskInput = "{LOC}";
  325. if (maxDigit > 0)
  326. {
  327. string str = "";
  328. for (int i = 0; i < maxDigit; i++)
  329. {
  330. if (i != 0 && (i % 3) == 0)
  331. {
  332. str = "," + str;
  333. }
  334. str = "n" + str;
  335. }
  336. maskInput = maskInput + str;
  337. if (digit > 0)
  338. {
  339. maskInput += ".";
  340. for (int i = 0; i < digit; i++)
  341. {
  342. maskInput += "n";
  343. }
  344. }
  345. foreach (UltraGridColumn column in band.Columns)
  346. {
  347. if (strs.Contains(column.Key))
  348. {
  349. column.MaskDisplayMode = Infragistics.Win.UltraWinMaskedEdit.MaskMode.IncludeLiterals;
  350. column.MaskInput = maskInput;
  351. column.CellAppearance.TextHAlign = HAlign.Right;
  352. if (digit > 0)
  353. {
  354. column.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Double;
  355. }
  356. else
  357. {
  358. column.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Integer;
  359. }
  360. }
  361. }
  362. }
  363. }
  364. /// <summary>
  365. /// 等待窗口
  366. /// </summary>
  367. public static void WaitFromOpen(Cursor cursor)
  368. {
  369. cursor = Cursors.WaitCursor; //控制鼠标的样式为等待
  370. if (Constant.WaitingForm == null)
  371. {
  372. Constant.WaitingForm = new WaitingForm();
  373. }
  374. Constant.WaitingForm.ShowToUser = true;
  375. Constant.WaitingForm.Show();
  376. Constant.WaitingForm.Update();
  377. }
  378. /// <summary>
  379. /// 关闭等待
  380. /// </summary>
  381. public static void WaitFromColse(Cursor cursor)
  382. {
  383. cursor = Cursors.Default;
  384. Constant.WaitingForm.ShowToUser = false;
  385. Constant.WaitingForm.Close();
  386. Constant.WaitingForm = null;
  387. }
  388. }
  389. }