Frmsinghjxh.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Collections;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using Core.Mes.Client.Comm.Control;
  13. using Core.Mes.Client.Comm.Format;
  14. using Core.Mes.Client.Comm.Tool;
  15. using CoreFS.CA06;
  16. using Infragistics.Win;
  17. using Infragistics.Win.UltraWinGrid;
  18. using NPOI.SS.UserModel;
  19. using Core.StlMes.Client.PnCost.Models;
  20. namespace Core.StlMes.Client.PnCost
  21. {
  22. public partial class Frmsinghjxh : FrmBase
  23. {
  24. #region 定义变量
  25. public Frmsinghjxh()
  26. {
  27. InitializeComponent();
  28. EntityHelper.ShowGridCaption<SingnhjxhEntity>(ultraGrid1.DisplayLayout.Bands[0]);
  29. }
  30. private DataTable dtgang;
  31. #endregion
  32. #region 方法
  33. public override void ToolBar_Click(object sender, string ToolbarKey)
  34. {
  35. base.ToolBar_Click(sender, ToolbarKey);
  36. switch (ToolbarKey)
  37. {
  38. case "Query":
  39. Query();
  40. break;
  41. case "doSave":
  42. doSave();
  43. break;
  44. case "doAdd":
  45. doAdd();
  46. break;
  47. case "doDelete":
  48. this.doDelete();
  49. break;
  50. }
  51. }
  52. private void doDelete()
  53. {
  54. this.ultraGrid1.UpdateData();
  55. IQueryable<UltraGridRow> checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" XUANZE = 'True'");
  56. if (checkMagRows.Count() == 0)
  57. {
  58. MessageUtil.ShowTips("请选择需要删除的数据!");
  59. return;
  60. }
  61. ArrayList parm = new ArrayList();
  62. foreach (UltraGridRow uRow in checkMagRows)
  63. {
  64. SingnhjxhEntity SingnhjxhEntity = (SingnhjxhEntity)uRow.ListObject;
  65. string OfflineResultTity = JSONFormat.Format(SingnhjxhEntity);
  66. parm.Add(OfflineResultTity);
  67. }
  68. CoreClientParam ccp = new CoreClientParam();
  69. ccp.ServerName = "com.steering.mes.cost.Frmsinghjxh";
  70. ccp.MethodName = "doDelete";
  71. ccp.ServerParams = new object[] { parm };
  72. ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  73. if (ccp.ReturnCode != -1)
  74. {
  75. if (ccp.ReturnInfo.Equals("操作成功"))
  76. {
  77. // DoQueryInList();
  78. Query();
  79. MessageUtil.ShowTips(ccp.ReturnInfo);
  80. }
  81. else
  82. {
  83. MessageUtil.ShowTips(ccp.ReturnInfo);
  84. }
  85. }
  86. }
  87. ///
  88. private void doAdd()
  89. {
  90. List<SingnhjxhEntity> list = null;
  91. var openFileDialog = new OpenFileDialog
  92. {
  93. Filter = "定额消耗|*.xls;*.xlsx"
  94. };
  95. if (openFileDialog.ShowDialog() == DialogResult.OK)
  96. {
  97. list = ReadExcelToSingnhjxhEntity(openFileDialog.FileName);
  98. }
  99. if (list == null) return;
  100. if (list.Count <= 0)
  101. {
  102. MessageBox.Show("Excel无数据");
  103. return;
  104. }
  105. string userName = UserInfo.GetUserName();
  106. var ccp = new CoreClientParam();
  107. ccp.ServerName = "com.steering.mes.cost.Frmsinghjxh";
  108. ccp.MethodName = "DoAddSinghjxh";
  109. ccp.ServerParams = new object[]
  110. {
  111. list.Select(JSONFormat.Format).ToList(),
  112. userName
  113. };
  114. ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  115. if (ccp.ReturnCode != -1)
  116. {
  117. MessageUtil.ShowTips(ccp.ReturnInfo);
  118. if (ccp.ReturnInfo.Equals("导入成功"))
  119. {
  120. // Query();
  121. }
  122. }
  123. }
  124. public List<SingnhjxhEntity> ReadExcelToSingnhjxhEntity(string fileName)
  125. {
  126. //定义要返回的datatable对象
  127. List<SingnhjxhEntity> SingnhjxhEntitys = new List<SingnhjxhEntity>();
  128. try
  129. {
  130. if (!File.Exists(fileName))
  131. {
  132. return null;
  133. }
  134. //根据指定路径读取文件
  135. using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
  136. {
  137. //根据文件流创建excel数据结构
  138. NPOI.SS.UserModel.IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs);
  139. //IWorkbook workbook = new HSSFWorkbook(fs);
  140. for (int k = 0; k < workbook.NumberOfSheets; k++)
  141. {
  142. NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(k);
  143. if (sheet != null)
  144. {
  145. //最后一列的标号
  146. int rowCount = sheet.LastRowNum;
  147. for (int i = 1; i <= rowCount; ++i)
  148. {
  149. SingnhjxhEntity entity = new SingnhjxhEntity();
  150. NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
  151. if (row == null) continue; //没有数据的行默认是null       
  152. if (row.Cells.Count < 4)
  153. {
  154. throw new Exception("无足够列");
  155. }
  156. string SgSign = GetCellValue(row.Cells[0]);
  157. string cic = GetCellValue(row.Cells[1]);
  158. if (SgSign == "" || SgSign == null)
  159. {
  160. throw new Exception("牌号为空");
  161. }
  162. if (cic == "" || cic == null)
  163. {
  164. throw new Exception("索引号为空");
  165. }
  166. decimal outsife;
  167. decimal outhcmnfe;
  168. decimal outmcmnfe;
  169. decimal outlcmnfe;
  170. decimal outhccrfe;
  171. decimal outmccrfe;
  172. decimal outlccrfe;
  173. decimal outwccrfe;
  174. decimal outmofe;
  175. decimal outvfe;
  176. decimal outnbfe;
  177. decimal outenl;
  178. decimal outcathodecopper;
  179. decimal outwcu;
  180. decimal outlfrb;
  181. decimal outsicaba;
  182. decimal outal;
  183. decimal outalwire;
  184. decimal outtifecorewire;
  185. decimal outbfecorewire;
  186. decimal outswire;
  187. decimal outcawire;
  188. decimal outalpowder;
  189. decimal outnicalon;
  190. decimal outferrosiliconpowder;
  191. decimal outblowgraphitepowder;
  192. decimal outslagcarburizingball;
  193. decimal outsimnhj;
  194. decimal.TryParse(GetCellValue(row.Cells[2]), out outsife);
  195. decimal.TryParse(GetCellValue(row.Cells[3]), out outhcmnfe);
  196. decimal.TryParse(GetCellValue(row.Cells[4]), out outmcmnfe);
  197. decimal.TryParse(GetCellValue(row.Cells[5]), out outlcmnfe);
  198. decimal.TryParse(GetCellValue(row.Cells[6]), out outhccrfe);
  199. decimal.TryParse(GetCellValue(row.Cells[7]), out outmccrfe);
  200. decimal.TryParse(GetCellValue(row.Cells[8]), out outlccrfe);
  201. decimal.TryParse(GetCellValue(row.Cells[9]), out outwccrfe);
  202. decimal.TryParse(GetCellValue(row.Cells[10]), out outmofe);
  203. decimal.TryParse(GetCellValue(row.Cells[11]), out outvfe);
  204. decimal.TryParse(GetCellValue(row.Cells[12]), out outnbfe);
  205. decimal.TryParse(GetCellValue(row.Cells[13]), out outenl);
  206. decimal.TryParse(GetCellValue(row.Cells[14]), out outcathodecopper);
  207. decimal.TryParse(GetCellValue(row.Cells[15]), out outwcu);
  208. decimal.TryParse(GetCellValue(row.Cells[16]), out outlfrb);
  209. decimal.TryParse(GetCellValue(row.Cells[17]), out outsicaba);
  210. decimal.TryParse(GetCellValue(row.Cells[18]), out outal);
  211. decimal.TryParse(GetCellValue(row.Cells[19]), out outalwire);
  212. decimal.TryParse(GetCellValue(row.Cells[20]), out outtifecorewire);
  213. decimal.TryParse(GetCellValue(row.Cells[21]), out outbfecorewire);
  214. decimal.TryParse(GetCellValue(row.Cells[22]), out outswire);
  215. decimal.TryParse(GetCellValue(row.Cells[23]), out outcawire);
  216. decimal.TryParse(GetCellValue(row.Cells[24]), out outalpowder);
  217. decimal.TryParse(GetCellValue(row.Cells[25]), out outnicalon);
  218. decimal.TryParse(GetCellValue(row.Cells[26]), out outferrosiliconpowder);
  219. decimal.TryParse(GetCellValue(row.Cells[27]), out outblowgraphitepowder);
  220. decimal.TryParse(GetCellValue(row.Cells[28]), out outslagcarburizingball);
  221. decimal.TryParse(GetCellValue(row.Cells[29]), out outsimnhj);
  222. entity.OutSife = outsife;
  223. entity.OutHcMnfe = outhcmnfe;
  224. entity.OutMcMnfe = outmcmnfe;
  225. entity.OutLcMnfe = outlcmnfe;
  226. entity.OutHcCrfe = outhccrfe;
  227. entity.OutMcCrfe = outmccrfe;
  228. entity.OutLcCrfe = outlccrfe;
  229. entity.OutWcCrfe = outwccrfe;
  230. entity.OutMofe = outmofe;
  231. entity.OutVfe = outvfe;
  232. entity.OutNbfe = outnbfe;
  233. entity.OutEni = outenl;
  234. entity.OutCathodeCopper = outcathodecopper;
  235. entity.OutWcu = outwcu;
  236. entity.OutLfrb = outlfrb;
  237. entity.OutSicaba = outsicaba;
  238. entity.OutAl = outal;
  239. entity.OutAlWire = outalwire;
  240. entity.OutTifeCoreWire = outtifecorewire;
  241. entity.OutBfeCoreWire = outbfecorewire;
  242. entity.OutSWire = outswire;
  243. entity.OutCaWire = outcawire;
  244. entity.OutAlPowder = outalpowder;
  245. entity.OutNicalon = outnicalon;
  246. entity.OutFerrosiliconPowder = outferrosiliconpowder;
  247. entity.OutBlowGraphitePowder = outblowgraphitepowder;
  248. entity.OutSlagCarburizingBall = outslagcarburizingball;
  249. entity.Cic = cic;
  250. entity.SgSign = SgSign;
  251. entity.OutSimnhj = outsimnhj;
  252. SingnhjxhEntitys.Add(entity);
  253. }
  254. }
  255. }
  256. }
  257. return SingnhjxhEntitys;
  258. }
  259. catch (Exception ex)
  260. {
  261. MessageBox.Show("EXCEL格式错误:" + ex.Message);
  262. return null;
  263. }
  264. }
  265. private string GetCellValue(ICell cell)
  266. {
  267. string value = "";
  268. switch (cell.CellType)
  269. {
  270. case CellType.Blank:
  271. value = "";
  272. break;
  273. case CellType.Numeric:
  274. short format = cell.CellStyle.DataFormat;
  275. value = cell.NumericCellValue.ToString2();
  276. break;
  277. case CellType.String:
  278. value = cell.StringCellValue;
  279. break;
  280. }
  281. return value;
  282. }
  283. /// <summary>
  284. /// 保存
  285. /// </summary>
  286. private void doSave()
  287. {
  288. try
  289. {
  290. this.ultraGrid1.UpdateData();
  291. IQueryable<UltraGridRow> checkMagRows = this.ultraGrid1.Rows.AsQueryable().Where(" XUANZE = 'True'");
  292. if (checkMagRows.Count() == 0)
  293. {
  294. MessageUtil.ShowTips("请选择需要修改的数据!");
  295. return;
  296. }
  297. ArrayList parm = new ArrayList();
  298. foreach (UltraGridRow uRow in checkMagRows)
  299. {
  300. SingnhjxhEntity entity = (SingnhjxhEntity)uRow.ListObject;
  301. entity.DuMaker = UserInfo.GetUserName();
  302. string SingnhjxhEntity = JSONFormat.Format(entity);
  303. parm.Add(SingnhjxhEntity);
  304. }
  305. CoreClientParam ccp = new CoreClientParam();
  306. ccp.ServerName = "com.steering.mes.cost.Frmsinghjxh";
  307. ccp.MethodName = "doSave";
  308. ccp.ServerParams = new object[] { parm };
  309. ccp = ob.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  310. if (ccp.ReturnCode != -1)
  311. {
  312. if (ccp.ReturnInfo.Equals("操作成功"))
  313. {
  314. // DoQueryInList();
  315. Query();
  316. MessageUtil.ShowTips(ccp.ReturnInfo);
  317. }
  318. else
  319. {
  320. MessageUtil.ShowTips(ccp.ReturnInfo);
  321. }
  322. }
  323. }
  324. catch
  325. {
  326. return;
  327. }
  328. }
  329. /// 查询方法
  330. /// <summary>
  331. /// 查询方法
  332. /// </summary>
  333. private void Query()
  334. {
  335. List<SingnhjxhEntity> listSource = EntityHelper.GetData<SingnhjxhEntity>(
  336. "com.steering.mes.cost.Frmsinghjxh.Query", new object[] { cicText.Text, gzText.Text }, this.ob);
  337. singnhjxhEntityBindingSource.DataSource = listSource;
  338. GridHelper.RefreshAndAutoSize(ultraGrid1);
  339. }
  340. /// 保存方法
  341. /// <summary>
  342. /// 保存方法
  343. /// </summary>
  344. private void Save()
  345. {
  346. }
  347. #endregion
  348. #region
  349. #endregion
  350. }
  351. }