FrmExcelToGrid.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 Core.Mes.Client.Comm.Tool;
  11. using System.Data.OleDb;
  12. using System.IO;
  13. using Infragistics.Win.UltraWinGrid;
  14. using NPOI.HSSF.UserModel;
  15. using NPOI.SS.UserModel;
  16. using NPOI.XSSF.UserModel;
  17. using System.Threading;
  18. using System.Collections;
  19. using Core.Mes.Client.Comm.Format;
  20. using Core.Mes.Client.Comm.Server;
  21. namespace Core.StlMes.Client.SaleOrder.ReviewForm
  22. {
  23. public partial class FrmExcelToGrid : FrmBase
  24. {
  25. public FrmExcelToGrid()
  26. {
  27. InitializeComponent();
  28. }
  29. public FrmExcelToGrid(OpeBase ob)
  30. {
  31. InitializeComponent();
  32. this.ob = ob;
  33. }
  34. HSSFWorkbook hssfworkbook; //Office 2003
  35. XSSFWorkbook xssfworkbook; //Office 2007 2010
  36. ISheet sheet = null;
  37. private string saleOrg;
  38. /// <summary>
  39. /// 销售组织
  40. /// </summary>
  41. public string SaleOrg
  42. {
  43. get { return saleOrg; }
  44. set { saleOrg = value; }
  45. }
  46. /// <summary>
  47. /// 打开选择的Excel
  48. /// </summary>
  49. /// <param name="path"></param>
  50. private void InitializeWorkbook(string path)
  51. {
  52. try
  53. {
  54. using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
  55. {
  56. hssfworkbook = new HSSFWorkbook(file);
  57. sheet = hssfworkbook.GetSheetAt(0);
  58. }
  59. }
  60. catch
  61. {
  62. using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
  63. {
  64. xssfworkbook = new XSSFWorkbook(file);
  65. sheet = xssfworkbook.GetSheetAt(0);
  66. }
  67. }
  68. }
  69. delegate void SetGridCallBack(DataTable dt);
  70. private void SetGrid(DataTable dt)
  71. {
  72. if (ultraGrid1.InvokeRequired)
  73. {
  74. SetGridCallBack sg = new SetGridCallBack(SetGrid);
  75. this.Invoke(sg, new object[] { dt });
  76. }
  77. else
  78. {
  79. ultraGrid1.DataSource = dt;
  80. ultraGrid1.DisplayLayout.Bands[0].Columns["选择"].Header.CheckBoxVisibility = Infragistics.Win.UltraWinGrid.HeaderCheckBoxVisibility.WhenUsingCheckEditor;
  81. ultraGrid1.DisplayLayout.Bands[0].Columns[1].Width = 135;
  82. this.Cursor = Cursors.Default;
  83. }
  84. }
  85. /// <summary>
  86. /// 将Excel内容转化为DataTable
  87. /// </summary>
  88. private void ConvertToDataTable()
  89. {
  90. //ISheet sheet = hssfworkbook.GetSheetAt(0);
  91. System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
  92. IRow iRow = sheet.GetRow(0);
  93. short count = iRow.LastCellNum;
  94. if (saleOrg == "100102")
  95. {
  96. if (count != 5)
  97. {
  98. MessageUtil.ShowWarning("导入的Excel列数与模板列数不符!");
  99. return;
  100. }
  101. }
  102. else
  103. {
  104. if (count != 6)
  105. {
  106. MessageUtil.ShowWarning("导入的Excel列数与模板列数不符!");
  107. return;
  108. }
  109. }
  110. DataTable dt = new DataTable();
  111. for (int j = 0; j < count + 1; j++)
  112. {
  113. if (j == 0) //加一列选择框
  114. {
  115. dt.Columns.Add("选择");
  116. }
  117. else
  118. {
  119. dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
  120. }
  121. }
  122. dt.Columns[0].DefaultValue = "False";
  123. dt.Columns[0].DataType = typeof(Boolean);
  124. while (rows.MoveNext())
  125. {
  126. IRow row = null;
  127. try
  128. {
  129. row = (HSSFRow)rows.Current;
  130. }
  131. catch
  132. {
  133. row = (XSSFRow)rows.Current;
  134. }
  135. DataRow dr = dt.NewRow();
  136. for (int i = 1; i < row.LastCellNum + 1; i++)
  137. {
  138. ICell cell = row.GetCell(i - 1);
  139. if (cell == null)
  140. {
  141. dr[i] = null;
  142. }
  143. else
  144. {
  145. dr[i] = cell.ToString();
  146. }
  147. }
  148. dt.Rows.Add(dr);
  149. }
  150. SetGrid(dt);
  151. }
  152. /// <summary>
  153. /// 使用系统软件打开模板
  154. /// </summary>
  155. /// <param name="sender"></param>
  156. /// <param name="e"></param>
  157. private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  158. {
  159. string path = "";
  160. if (saleOrg == "100102")
  161. {
  162. path = System.Windows.Forms.Application.StartupPath + "\\PriceTemplate\\PriceTemplateGM.xls";
  163. }
  164. else
  165. {
  166. path = System.Windows.Forms.Application.StartupPath + "\\PriceTemplate\\PriceTemplateXS.xls";
  167. }
  168. try
  169. {
  170. System.Diagnostics.Process.Start(path);
  171. }
  172. catch (Exception ex)
  173. {
  174. MessageUtil.ShowWarning("没有可用的模板!");
  175. }
  176. }
  177. private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
  178. {
  179. switch (e.Tool.Key)
  180. {
  181. case "Import":
  182. ImportExcel();
  183. break;
  184. case "Confirm":
  185. ConfirmData();
  186. break;
  187. case "Close":
  188. this.Close();
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. private void ImportExcel()
  195. {
  196. OpenFileDialog ofd = new OpenFileDialog();
  197. ofd.Filter = "所有文件|*.xls;*.xlsx|xls文件(*.xls)|*.xls|xlsx文件(*.xlsx)|*.xlsx";
  198. ofd.Multiselect = false;
  199. if (ofd.ShowDialog() == DialogResult.OK)
  200. {
  201. this.Cursor = Cursors.WaitCursor;
  202. string filePath = ofd.FileName;
  203. InitializeWorkbook(filePath);
  204. //ConvertToDataTable();
  205. Thread t = new Thread(new ThreadStart(ConvertToDataTable));
  206. t.Start();
  207. }
  208. }
  209. Dictionary<string, string> dic = new Dictionary<string, string>();
  210. Dictionary<string, ArrayList> price = new Dictionary<string, ArrayList>();
  211. /// <summary>
  212. /// 确认数据
  213. /// </summary>
  214. private void ConfirmData()
  215. {
  216. ultraGrid1.UpdateData();
  217. int flag = 0;
  218. foreach (UltraGridRow row in ultraGrid1.Rows)
  219. {
  220. if (row.Cells["选择"].Value.ToString().ToUpper() == "TRUE")
  221. {
  222. flag += 1;
  223. string orderNoSeq = row.Cells[1].Value.ToString();
  224. string balPrice = row.Cells[2].Value.ToString();
  225. string outPrice = row.Cells[3].Value.ToString();
  226. string traPrice = row.Cells[4].Value.ToString();
  227. string pakPrice = row.Cells[5].Value.ToString();
  228. string priceBase = row.Cells[6].Value.ToString();
  229. if (orderNoSeq == "")
  230. {
  231. MessageUtil.ShowWarning("合同号(合同号/合同行号)不能为空!");
  232. return;
  233. }
  234. string orderNo = "";
  235. string seq = "";
  236. try
  237. {
  238. orderNo = orderNoSeq.Substring(0, orderNoSeq.LastIndexOf("/"));
  239. seq = orderNoSeq.Substring(orderNoSeq.LastIndexOf("/") + 1, orderNoSeq.Length - 1 - orderNoSeq.LastIndexOf("/"));
  240. }
  241. catch (Exception)
  242. {
  243. MessageUtil.ShowWarning("合同号(合同号/合同行号)跟模板数据规则不符!");
  244. return;
  245. }
  246. if (balPrice == "")
  247. {
  248. MessageUtil.ShowWarning("结算价不能为空!");
  249. return;
  250. }
  251. else
  252. {
  253. if (!StringUtil.IsNumber(balPrice))
  254. {
  255. MessageUtil.ShowWarning("结算价不是数字!");
  256. return;
  257. }
  258. }
  259. if (outPrice == "")
  260. {
  261. if (saleOrg != "100102")
  262. {
  263. MessageUtil.ShowWarning("出厂价不能为空!");
  264. return;
  265. }
  266. }
  267. else
  268. {
  269. if (!StringUtil.IsNumber(outPrice))
  270. {
  271. MessageUtil.ShowWarning("结算价不是数字!");
  272. return;
  273. }
  274. }
  275. if (traPrice != "")
  276. {
  277. if (!StringUtil.IsNumber(traPrice))
  278. {
  279. MessageUtil.ShowWarning("运费不是数字!");
  280. return;
  281. }
  282. }
  283. if (pakPrice != "")
  284. {
  285. if (!StringUtil.IsNumber(pakPrice))
  286. {
  287. MessageUtil.ShowWarning("吊装费不是数字!");
  288. return;
  289. }
  290. }
  291. if (priceBase == "")
  292. {
  293. MessageUtil.ShowWarning("定价说明不能为空!");
  294. return;
  295. }
  296. if (!dic.ContainsKey(orderNo))
  297. {
  298. dic.Add(orderNo, priceBase);
  299. }
  300. ArrayList list = new ArrayList();
  301. list.Add(orderNo);
  302. list.Add(seq);
  303. list.Add(balPrice);
  304. list.Add(outPrice);
  305. list.Add(traPrice);
  306. list.Add(pakPrice);
  307. if (price.ContainsKey(orderNo))
  308. {
  309. ArrayList temp = price[orderNo];
  310. temp.Add(list);
  311. price[orderNo] = temp;
  312. }
  313. else
  314. {
  315. ArrayList parm = new ArrayList();
  316. parm.Add(list);
  317. price[orderNo] = parm;
  318. }
  319. }
  320. }
  321. if (flag == 0)
  322. {
  323. MessageUtil.ShowWarning("请勾选或者拖选您要确认的数据!");
  324. return;
  325. }
  326. if (MessageUtil.ShowYesNoAndQuestion("是否确认勾选的数据?") == DialogResult.No) return;
  327. SlmOrderPriceEntity sop = new SlmOrderPriceEntity();
  328. //提报信息
  329. sop.ReportName = UserInfo.GetUserName();
  330. sop.ReportUnitcode = UserInfo.GetDeptid();
  331. sop.ReportUnitdesc = UserInfo.GetDepartment();
  332. sop.ReportDeptcode = ClsBaseInfo.GetDepartIdBySectionId(UserInfo.GetDeptid(), this.ob);
  333. sop.ReportDeptdesc = ClsBaseInfo.GetDepartBySectionId(UserInfo.GetDeptid(), this.ob);
  334. //评审通过信息
  335. sop.DealName = UserInfo.GetUserName();
  336. sop.DealUnitcode = UserInfo.GetDeptid();
  337. sop.DealUnitdesc = UserInfo.GetDepartment();
  338. sop.DealDeptcode = ClsBaseInfo.GetDepartIdBySectionId(UserInfo.GetDeptid(), this.ob);
  339. sop.DealDeptdesc = ClsBaseInfo.GetDepartBySectionId(UserInfo.GetDeptid(), this.ob);
  340. CoreClientParam ccp = new CoreClientParam();
  341. ccp.IfShowErrMsg = false;
  342. ccp.ServerName = "com.steering.pss.sale.order.CoreExcelToGrid";
  343. ccp.MethodName = "confirmDataToDB";
  344. ccp.ServerParams = new object[] { dic, JSONFormat.Format(sop), price };
  345. ccp = this.ExecuteNonQuery(ccp, CoreInvokeType.Internal);
  346. if (ccp != null)
  347. {
  348. if (ccp.ReturnCode == -1)
  349. {
  350. MessageUtil.ShowWarning(ccp.ReturnInfo);
  351. return;
  352. }
  353. MessageUtil.ShowTips("数据确认成功!");
  354. }
  355. }
  356. private void ultraGrid1_AfterSelectChange(object sender, Infragistics.Win.UltraWinGrid.AfterSelectChangeEventArgs e)
  357. {
  358. foreach (UltraGridRow uRow in ultraGrid1.Selected.Rows)
  359. {
  360. if (uRow.GetType() != typeof(Infragistics.Win.UltraWinGrid.UltraGridGroupByRow))
  361. {
  362. uRow.Cells["选择"].Value = true;
  363. }
  364. }
  365. }
  366. }
  367. }