UIK050103.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 CoreFS.CA06;
  10. using System.Collections;
  11. using Infragistics.Win.UltraWinGrid;
  12. using Excel1 = Microsoft.Office.Interop.Excel;
  13. using System.Diagnostics;
  14. using System.Reflection;
  15. using System.Runtime.InteropServices;
  16. namespace Core.LZMes.Client.UIK
  17. {
  18. public partial class UIK050103 : FrmBase
  19. {
  20. public UIK050103()
  21. {
  22. InitializeComponent();
  23. }
  24. public override void ToolBar_Click(object sender, string ToolbarKey)
  25. {
  26. switch (ToolbarKey)
  27. {
  28. case "Query"://查询
  29. this.DoQuery();
  30. break;
  31. case "export"://添加换下轧辊实绩
  32. this.DoExport(ultraGrid1, "磨削实绩查询");
  33. break;
  34. case "Update"://下发换下轧辊实绩
  35. this.DoUpdate();
  36. break;
  37. }
  38. }
  39. private void DoQuery()
  40. {
  41. try
  42. {
  43. string zgNum = "";
  44. string beginTime = "";
  45. string endTime = "";
  46. string type = "";
  47. CoreClientParam ccp = new CoreClientParam();
  48. DataTable dt = new DataTable();
  49. zgNum = this.ultraTextzgNum.Text;
  50. type = this.ultraComboEditor1.Text;
  51. beginTime = ultraDateTimeEditor1.Value != null ? ultraDateTimeEditor1.DateTime.ToString("yyyyMMdd") : "";
  52. endTime = ultraDateTimeEditor2.Value != null ? ultraDateTimeEditor2.DateTime.ToString("yyyyMMdd") : "";
  53. this.dataSet1.Clear();
  54. ArrayList al = new ArrayList();
  55. al.Add("UIK050103_01.SELECT");
  56. al.Add(zgNum);
  57. al.Add(type);
  58. al.Add(beginTime);
  59. al.Add(endTime);
  60. al.Add(zgNum);
  61. al.Add(type);
  62. al.Add(beginTime);
  63. al.Add(endTime);
  64. ccp = new CoreClientParam();
  65. ccp.ServerName = "UIB.COM.ComDBQuery";
  66. ccp.MethodName = "doSimpleQuery";
  67. ccp.ServerParams = new object[] { al };
  68. ccp.IfShowErrMsg = false;
  69. ccp.SourceDataTable = this.dataSet1.Tables[0];
  70. this.ExecuteQueryToDataTable(ccp, CoreInvokeType.Internal);
  71. }
  72. // }
  73. catch (Exception Ex)
  74. {
  75. MessageBox.Show(Ex.ToString());
  76. }
  77. }
  78. private void DoExport(UltraGrid tmpDataTable, string rFileName)
  79. {
  80. string fileName = saveFileDialog1.FileName;
  81. string FileName;
  82. saveFileDialog1.FileName = rFileName;
  83. saveFileDialog1.DefaultExt = ".xls";
  84. saveFileDialog1.Filter = "excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";
  85. if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
  86. {
  87. fileName = saveFileDialog1.FileName;
  88. FileName = fileName;
  89. }
  90. else
  91. {
  92. return;
  93. }
  94. string FilePath = saveFileDialog1.FileName;
  95. if (tmpDataTable == null)
  96. return;
  97. int rowNum = tmpDataTable.Rows.Count;
  98. int columnNum = tmpDataTable.DisplayLayout.Bands[0].Columns.Count;
  99. int rowIndex = 1;
  100. int columnIndex = 0;
  101. Excel1.Application xlApp = new Excel1.ApplicationClass();
  102. xlApp.DefaultFilePath = "";
  103. xlApp.DisplayAlerts = false;
  104. xlApp.SheetsInNewWorkbook = 1; //设置导出表格工作簿sheet数量
  105. Excel1._Workbook xlBook = (Excel1._Workbook)(xlApp.Workbooks.Add(Missing.Value));//添加新工作簿
  106. Excel1.Worksheet ws = (Excel1.Worksheet)xlBook.Worksheets[1];
  107. ws.Name = rFileName;
  108. Excel1.Range r = ws.get_Range("A1", NumToExeclRowStr(columnNum - 1) + "1");
  109. object[] objHeader = new object[columnNum];
  110. //将DataTable的列名导入Excel表第一行
  111. foreach (UltraGridColumn dc in tmpDataTable.DisplayLayout.Bands[0].Columns)
  112. {
  113. if (columnIndex < columnNum-1)
  114. {
  115. objHeader[columnIndex] = dc.Header.Caption;
  116. columnIndex++;
  117. }
  118. }
  119. r.Value2 = objHeader;
  120. //将DataTable中的数据导入Excel中
  121. int h = 0;
  122. for (int i = 0; i < rowNum; i++)
  123. {
  124. //if (tmpDataTable.Rows[i].Cells[columnNum].Text.ToString() == "True")
  125. // {
  126. rowIndex++;
  127. columnIndex = 0;
  128. for (int j = 0; j < columnNum - 1; j++)
  129. {
  130. objHeader[columnIndex] = tmpDataTable.Rows[h].Cells[j].Text.ToString();
  131. columnIndex++;
  132. }
  133. r = ws.get_Range("A" + (h + 2), NumToExeclRowStr(columnNum) + (h + 2));
  134. r.Value2 = objHeader;
  135. h++;
  136. // }
  137. }
  138. //将合同号的数据转换为excel中的数字格式,否则为科学计数法格式
  139. Excel1.Range RMXNEW;
  140. string Inf0 = "";
  141. Inf0 = "V1:" + "V2";
  142. RMXNEW = ws.get_Range(Inf0, Type.Missing);
  143. RMXNEW.EntireColumn.HorizontalAlignment = Excel1.XlHAlign.xlHAlignCenter;
  144. RMXNEW.EntireColumn.NumberFormat = "0";
  145. Excel1.Range RMXNEW1;
  146. string Inf1 = "";
  147. Inf1 = "W1:" + "W2";
  148. RMXNEW1 = ws.get_Range(Inf1, Type.Missing);
  149. RMXNEW1.EntireColumn.HorizontalAlignment = Excel1.XlHAlign.xlHAlignCenter;
  150. RMXNEW1.EntireColumn.NumberFormat = "0";
  151. ////导出到EXCEL中 只需要某些列,下面删除多余的列(已经在datatable中按字段排好序)
  152. //Excel1.Range RMX1;
  153. //string Inf1 = "";
  154. //Inf1 = "A1:" + "W1";
  155. //RMX1 = ws.get_Range(Inf1, Type.Missing);
  156. //RMX1.EntireColumn.Delete(0);
  157. r.EntireColumn.AutoFit();
  158. xlBook.SaveCopyAs(FilePath);
  159. this.KillSpecialExcel(xlApp);
  160. MessageBox.Show("数据成功导出到:" + saveFileDialog1.FileName.ToString(),
  161. "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
  162. //System.Diagnostics.Process.Start(fileName);
  163. }
  164. private string NumTochr(int Num)
  165. {
  166. int n = 64 + Num;
  167. return "" + (Char)n;
  168. }
  169. private string NumToExeclRowStr(int Num)
  170. {
  171. int X, Y;
  172. if (Num < 27)
  173. {
  174. return NumTochr(Num);
  175. }
  176. X = Num / 26;
  177. Y = Num - X * 26;
  178. //if (Y == 0) Y = 1;
  179. return NumTochr(X) + NumTochr(Y);
  180. }
  181. #region 结束EXCEL.EXE进程的方法
  182. /// <summary>
  183. /// 结束EXCEL.EXE进程的方法
  184. /// </summary>
  185. /// <param name="m_objExcel">EXCEL对象</param>
  186. [DllImport("user32.dll", SetLastError = true)]
  187. static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
  188. public void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
  189. {
  190. try
  191. {
  192. if (m_objExcel != null)
  193. {
  194. int lpdwProcessId;
  195. GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId);
  196. System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. MessageBox.Show(ex.Message);
  202. }
  203. }
  204. #endregion
  205. private void ultraGrid1_DoubleClickRow(object sender, DoubleClickRowEventArgs e)
  206. {
  207. Infragistics.Win.UltraWinGrid.UltraGrid uGrid = (Infragistics.Win.UltraWinGrid.UltraGrid)sender;
  208. ultraTextStand_id.Text = uGrid.ActiveRow.Cells["ROLLID"].Text;
  209. ultraTextEditor3.Text = uGrid.ActiveRow.Cells["roll_type"].Text;//轧辊类型
  210. ultraTextEditor6.Text = uGrid.ActiveRow.Cells["RESERVE"].Text;//直径
  211. ultraTextShangID.Text = uGrid.ActiveRow.Cells["stand_id"].Text;//机架编号
  212. ultraTextEditor2.Text = uGrid.ActiveRow.Cells["spec_no"].Text;//顺序号
  213. ultraCalendarDis_time.Text = uGrid.ActiveRow.Cells["REG_DTIME"].Text;//时间
  214. ultraTextEditor4.Text = uGrid.ActiveRow.Cells["DIS"].Text;
  215. }
  216. private void DoUpdate()
  217. {
  218. try
  219. {
  220. ArrayList al = new ArrayList();
  221. ArrayList list = new ArrayList();
  222. string roll_num = this.ultraTextStand_id.Text; //轧辊编号
  223. string rolltype = this.ultraTextEditor3.Text;//
  224. string diam = this.ultraTextEditor6.Text; //轧辊区域
  225. string jijia = this.ultraTextShangID.Text;//上编号
  226. string shunxu = this.ultraTextEditor2.Text;//软点
  227. //string shijian = this.ultraCalendarDis_time.Value != null ? ultraDateTimeEditor1.DateTime.ToString("yyyyMMddhhmiss") : ""; //this.dateTimePicker1.Text.ToString();
  228. string shijian = this.ultraCalendarDis_time.Text; //this.dateTimePicker1.Text.ToString();
  229. string qufen = this.ultraTextEditor4.Text;
  230. if(qufen=="UP")
  231. {
  232. list.Add("UIK050103_01.UPDATE");
  233. }
  234. else if(qufen == "LO")
  235. {
  236. list.Add("UIK050103_02.UPDATE");
  237. }
  238. list.Add(rolltype);
  239. list.Add(diam);
  240. list.Add(jijia);
  241. list.Add(shijian);
  242. list.Add(roll_num);
  243. list.Add(shunxu);
  244. al.Add(list);
  245. if (al.Count == 0)
  246. {
  247. MessageBox.Show("请勾选数据再做保存操作!", "提示");
  248. return;
  249. }
  250. CoreClientParam ccp = new CoreClientParam();
  251. ccp.ServerName = "UIB.COM.ComDBSave";
  252. ccp.MethodName = "doXmlSave";
  253. ccp.ServerParams = new object[] { al };
  254. ccp = this.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  255. this.DoQuery();
  256. if (0 != ccp.ReturnCode)
  257. {
  258. MessageBox.Show("保存失败!");
  259. }
  260. else
  261. {
  262. MessageBox.Show("保存成功!");
  263. }
  264. }
  265. catch (Exception Ex)
  266. {
  267. MessageBox.Show(Ex.ToString());
  268. }
  269. }
  270. private void UIK050103_Load(object sender, EventArgs e)
  271. {
  272. }
  273. }
  274. }