ExcelHelper.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Configuration;
  9. namespace wispro.sp.utility
  10. {
  11. public class ExcelHelper
  12. {
  13. //string[] temList = new string[] {"PACN1917256",
  14. // "PAUS2010223",
  15. // "PAUS1914936",
  16. // "PACN1815861",
  17. // "PACN1913445",
  18. // "PACN1816576",
  19. // "PACN2113860",
  20. // "PACN1812755-结案",
  21. // "PACN2015701",
  22. // "PCTCN1815005",
  23. // "PACN1913883-加快",
  24. // "PACN1915086-加快",
  25. // "PACN1810141",
  26. // "PACN1815247",
  27. // "PACN1812737",
  28. // "PACN1813923",
  29. // "PACN1912513",
  30. // "PACN1912504",
  31. // "PAEPO2112337",
  32. // "PAUS2111949",
  33. // "PATW2111951",
  34. // "PAEPO2111950",
  35. // "PAEPO2111788",
  36. // "PAEPO2013035",
  37. // "PAEPO1918035",
  38. // "PAEPO1813034",
  39. // "PAGB1711461" };
  40. public DataTable MerageExcel(string desFileName,string[] excelFiles)
  41. {
  42. DataTable retDt = null;
  43. foreach(string strFileName in excelFiles)
  44. {
  45. if (System.IO.File.Exists(strFileName))
  46. {
  47. if (retDt == null)
  48. {
  49. retDt = NPOIExcel.ExcelToDataTable(strFileName, true,true);
  50. DataTable temdt = new DataTable();
  51. foreach(DataColumn col in retDt.Columns)
  52. {
  53. DataColumn temCol = new DataColumn();
  54. temCol.ColumnName = col.ColumnName;
  55. temCol.DataType = col.DataType;
  56. temCol.Caption = col.Caption;
  57. temdt.Columns.Add(temCol);
  58. }
  59. MerageDataTable(temdt, retDt);
  60. retDt = temdt;
  61. }
  62. else
  63. {
  64. DataTable dt = NPOIExcel.ExcelToDataTable(strFileName, true, true);
  65. MerageDataTable(retDt, dt);
  66. }
  67. }
  68. else
  69. {
  70. throw new ApplicationException($"文件不存在:{strFileName}");
  71. }
  72. }
  73. string strCalculatedFile = ConfigurationSettings.AppSettings["CalculatedFile"];// "21.01-21.06 工程师绩效报表-总表.xlsx";
  74. DataTable CalculatedTable = NPOIExcel.ExcelToDataTable(strCalculatedFile, true);
  75. #region 删除空行
  76. List<DataRow> delRows = new List<DataRow>();
  77. System.Collections.Hashtable temHash = new System.Collections.Hashtable();
  78. foreach (DataRow row in retDt.Rows)
  79. {
  80. if ((row[0] == null || row[0].ToString() == "") && (row[1] == null || row[1].ToString() == "") && (row[2] == null || row[2].ToString() == ""))
  81. {
  82. delRows.Add(row);
  83. }
  84. else
  85. {
  86. #region 添加我方案号和处理事项相同的项目到hashTable
  87. string strKey = $"{row["我方文号"].ToString()}-{row["处理事项"].ToString()}";
  88. if(temHash.Contains(strKey))
  89. {
  90. List<DataRow> List = (List<DataRow>)temHash[strKey];
  91. List.Add(row);
  92. }
  93. else
  94. {
  95. List<DataRow> List = new List<DataRow>();
  96. List.Add(row);
  97. temHash.Add(strKey, List);
  98. }
  99. #endregion
  100. #region 处理事项为技术确认直接删除
  101. try
  102. {
  103. if (row["处理事项"].ToString().Trim() == "技术确认")
  104. {
  105. delRows.Add(row);
  106. }
  107. }
  108. catch { }
  109. #endregion
  110. #region 已核算绩效标记
  111. string strTem = isCalculatedData(row, CalculatedTable);
  112. if(strTem != "")
  113. {
  114. if (!retDt.Columns.Contains("核查数据反馈"))
  115. {
  116. retDt.Columns.Add("核查数据反馈");
  117. }
  118. row["核查数据反馈"] = strTem;
  119. }
  120. #endregion
  121. #region 处理君龙处理人
  122. row["处理人"] = FormatStaffName(row["处理人"].ToString());
  123. #endregion
  124. #region 处理人和核稿人相同时,删除核稿人
  125. if (row["处理人"].ToString() == row["核稿人"].ToString())
  126. {
  127. row["核稿人"] = "";
  128. }
  129. #endregion
  130. #region 严重超期降系数处理,如果代理人没有填写客观原因才降系数,如果没有客户期限,期限为内部期限,如果没有内部期限,期限为委托日期+30天
  131. try
  132. {
  133. if (row["处理事项"].ToString() == "新申请")
  134. {
  135. DateTime dt1 = DateTime.MinValue;
  136. if (!string.IsNullOrEmpty(row["返稿日"].ToString()))
  137. {
  138. try
  139. {
  140. dt1 = DateTime.Parse(row["返稿日"].ToString());
  141. }
  142. catch
  143. {
  144. dt1 = DateTime.Parse("1899-12-30").AddDays(double.Parse(row["返稿日"].ToString()));
  145. row["返稿日"] = dt1;
  146. }
  147. }
  148. DateTime dt2 = DateTime.MinValue;
  149. if (!string.IsNullOrEmpty(row["客户期限"].ToString()))
  150. {
  151. try
  152. {
  153. dt2 = DateTime.Parse(row["客户期限"].ToString());
  154. }
  155. catch
  156. {
  157. dt2 = DateTime.Parse("1899-12-30").AddDays(double.Parse(row["客户期限"].ToString()));
  158. row["客户期限"] = dt2;
  159. }
  160. }
  161. else
  162. {
  163. if (!string.IsNullOrEmpty(row["内部期限"].ToString()))
  164. {
  165. try
  166. {
  167. dt2 = DateTime.Parse(row["内部期限"].ToString());
  168. }
  169. catch
  170. {
  171. dt2 = DateTime.Parse("1899-12-30").AddDays(double.Parse(row["内部期限"].ToString()));
  172. row["内部期限"] = dt2;
  173. }
  174. }
  175. else
  176. {
  177. if (!string.IsNullOrEmpty(row["委案日期"].ToString()))
  178. {
  179. try
  180. {
  181. dt2 = DateTime.Parse(row["委案日期"].ToString()).AddDays(30);
  182. }
  183. catch
  184. {
  185. dt2 = DateTime.Parse("1899-12-30").AddDays(double.Parse(row["内部期限"].ToString())).AddDays(30);
  186. row["内部期限"] = dt2.AddDays(-30);
  187. }
  188. }
  189. }
  190. }
  191. string strReason = row["备注(发文严重超期是否属客观原因,若为否,请填写原因)"].ToString();
  192. if ((dt1 - dt2).TotalDays > 30 && !string.IsNullOrEmpty(strReason))
  193. {
  194. if (!retDt.Columns.Contains("是否降系数"))
  195. {
  196. retDt.Columns.Add("是否降系数");
  197. }
  198. switch (row["案件系数"].ToString())
  199. {
  200. case "S":
  201. row["案件系数"] = "A";
  202. row["是否降系数"] = $"严重超期降系数【超期:{(dt1 - dt2).TotalDays}】 S-A";
  203. break;
  204. case "A":
  205. row["案件系数"] = "B";
  206. row["是否降系数"] = $"严重超期降系数【超期:{(dt1 - dt2).TotalDays}】 A-B";
  207. break;
  208. case "B":
  209. row["案件系数"] = "C";
  210. row["是否降系数"] = $"严重超期降系数【超期:{(dt1 - dt2).TotalDays}】 B-C";
  211. break;
  212. case "C":
  213. row["案件系数"] = "D";
  214. row["是否降系数"] = $"严重超期降系数【超期:{(dt1 - dt2).TotalDays}】 C-D";
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. catch { }
  221. #endregion
  222. }
  223. }
  224. #region 删除我方文号和处理事项相同,比较少处理人的记录
  225. foreach (string strKey in temHash.Keys)
  226. {
  227. List<DataRow> rows = (List<DataRow>)temHash[strKey];
  228. if(rows.Count > 1)
  229. {
  230. DataRow retRow = null;
  231. List<string> clrList = null;
  232. foreach(DataRow row in rows)
  233. {
  234. if (retRow == null)
  235. {
  236. retRow = row;
  237. clrList = row["处理人"].ToString().Split(new char[] { ',', ',', '、' }).ToList<string>();
  238. }
  239. else
  240. {
  241. List<string> temClrList = row["处理人"].ToString().Split(new char[] { ',', ',', '、' }).ToList<string>();
  242. if(temClrList.Count > clrList.Count)
  243. {
  244. delRows.Add(retRow);
  245. retRow = row;
  246. clrList = temClrList;
  247. }
  248. else
  249. {
  250. delRows.Add(row);
  251. }
  252. }
  253. }
  254. }
  255. }
  256. #endregion
  257. foreach(DataRow row in delRows)
  258. {
  259. retDt.Rows.Remove(row);
  260. }
  261. #endregion
  262. CalculatedTable.Dispose();
  263. NPOIExcel.DataTableToExcel(retDt ,desFileName);
  264. return retDt;
  265. }
  266. private void MerageDataTable(DataTable retDt, DataTable dt)
  267. {
  268. int iRow = 0;
  269. foreach (DataRow row in dt.Rows)
  270. {
  271. iRow++;
  272. //判断是否有相同的记录,我方文号+处理事项+处理人相同
  273. DataRow oldRow = isExists(row, retDt);
  274. if (oldRow == null)
  275. {
  276. DataRow newRow = retDt.NewRow();
  277. foreach (DataColumn col in dt.Columns)
  278. {
  279. if (retDt.Columns.Contains(col.ColumnName))
  280. {
  281. newRow[col.ColumnName] = row[col.ColumnName];
  282. }
  283. }
  284. retDt.Rows.Add(newRow);
  285. }
  286. else
  287. {
  288. foreach (DataColumn col in dt.Columns)
  289. {
  290. if (retDt.Columns.Contains(col.ColumnName))
  291. {
  292. if (string.IsNullOrEmpty(oldRow[col.ColumnName].ToString())
  293. && !string.IsNullOrEmpty(row[col.ColumnName].ToString()))
  294. {
  295. oldRow[col.ColumnName] = row[col.ColumnName];
  296. }
  297. }
  298. }
  299. }
  300. }
  301. }
  302. /// <summary>
  303. /// 处理君龙处理人
  304. /// </summary>
  305. /// <param name="strName"></param>
  306. /// <returns></returns>
  307. private string FormatStaffName(string strName)
  308. {
  309. if (string.IsNullOrEmpty(strName))
  310. {
  311. return strName;
  312. }
  313. string strReturn = "";
  314. string[] Names = strName.Trim().Split(new char[] { ',' });
  315. foreach(string name in Names)
  316. {
  317. strReturn = string.IsNullOrEmpty(strReturn) ? name.Replace("-君龙", "") : $"{strReturn},{name.Replace("-君龙", "")}";
  318. }
  319. return strReturn;
  320. }
  321. private DataRow isExists(DataRow row,DataTable dt)
  322. {
  323. //DataRow[] temRows = dt.Select($"我方文号='{row["我方文号"].ToString()}' and 处理事项='{row["处理事项"].ToString()}' and 处理人='{row["处理人"].ToString()}'");
  324. //if (temRows.Length > 0)
  325. //{
  326. // return temRows[0];
  327. //}
  328. //else
  329. //{
  330. // return null;
  331. //}
  332. foreach (DataRow temRow in dt.Rows)
  333. {
  334. if(row["我方文号"].ToString() == temRow["我方文号"].ToString() &&
  335. row["处理事项"].ToString() == temRow["处理事项"].ToString() &&
  336. row["处理人"].ToString() == temRow["处理人"].ToString())
  337. {
  338. System.Diagnostics.Debug.WriteLine(string.Format("{0} 重复项目", row["我方文号"].ToString()));
  339. return temRow;
  340. }
  341. }
  342. return null;
  343. }
  344. private string isCalculatedData(DataRow row ,DataTable CalculatedTable)
  345. {
  346. foreach (DataRow temRow in CalculatedTable.Rows)
  347. {
  348. if (row["我方文号"].ToString() == temRow["我方文号"].ToString() &&
  349. row["处理事项"].ToString() == temRow["处理事项"].ToString() )
  350. {
  351. if(row["处理事项"].ToString() == "处理审查意见")
  352. {
  353. if ( row["案件阶段"].ToString() == temRow["案件阶段"].ToString())
  354. return temRow["绩效核算月份"].ToString() + "已核算绩效!";
  355. }
  356. else
  357. {
  358. return temRow["绩效核算月份"].ToString() + "已核算绩效!";
  359. }
  360. }
  361. }
  362. return "";
  363. }
  364. /// <summary>
  365. /// 填充点数
  366. /// </summary>
  367. /// <param name="strPath">总表文件路径</param>
  368. /// <param name="dianshuFilePath">点数规则文件路径</param>
  369. public void FillDianShu(string strPath,string dianshuFilePath)
  370. {
  371. DataTable dsTable = NPOIExcel.ExcelToDataTable(dianshuFilePath,true);
  372. //DataRow[] Rows = dsTable.Select("1=1", "转换条件");
  373. DataTable dt = NPOIExcel.ExcelToDataTable(strPath, true);
  374. for(int iRow = dsTable.Rows.Count-1;iRow>=0;iRow--)
  375. //foreach(DataRow row in dsTable.Rows)
  376. {
  377. DataRow row = dsTable.Rows[iRow];
  378. if (!string.IsNullOrEmpty(row["转换条件"].ToString().Trim()))
  379. {
  380. try
  381. {
  382. DataRow[] temRows = dt.Select(row["转换条件"].ToString().Trim());
  383. foreach (DataRow dataRow in temRows)
  384. {
  385. dataRow["基本点数"] = double.Parse(row["点数"].ToString());
  386. }
  387. }
  388. catch(Exception ex)
  389. {
  390. System.Diagnostics.Debug.WriteLine(ex.ToString());
  391. }
  392. }
  393. }
  394. #region 翻译按字数计算
  395. DataRow[] fyList = dt.Select("处理事项='翻译'");
  396. foreach (DataRow dataRow in fyList)
  397. {
  398. string strBZ = dataRow["备注(填表注意事项)"].ToString();
  399. System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("(\\d{1,})字");
  400. System.Text.RegularExpressions.Match m = r.Match(strBZ);
  401. if (m.Success)
  402. {
  403. double iChar = double.Parse(m.Groups[1].Value);
  404. if (strBZ.Contains("中-英"))
  405. {
  406. double ds = (iChar*0.16)/1000.00;
  407. dataRow["基本点数"] = ds.ToString();
  408. }
  409. else
  410. {
  411. if (strBZ.Contains("中-德") || strBZ.Contains("德-中"))
  412. {
  413. double ds = (iChar * 0.18) / 1000.00;
  414. dataRow["基本点数"] = ds.ToString();
  415. }
  416. else
  417. {
  418. if (strBZ.Contains("英-中"))
  419. {
  420. double ds = (iChar * 0.1) / 1000.00;
  421. dataRow["基本点数"] = ds.ToString();
  422. }
  423. }
  424. }
  425. }
  426. else
  427. {
  428. }
  429. }
  430. #endregion
  431. #region 已算绩效,基本点数为0
  432. DataRow[] ysjxList = dt.Select("备注(填表注意事项)='已算绩效' or 备注(填表注意事项)='绩效已算' or 备注(填表注意事项)='已计算' or 备注(填表注意事项)='绩效已核算' or 核查数据反馈 like '已核算绩效'");
  433. foreach (DataRow dataRow in ysjxList)
  434. {
  435. dataRow["基本点数"] = "0";
  436. }
  437. #endregion
  438. NPOIExcel.DataTableToExcel(dt, strPath);
  439. }
  440. }
  441. }