ExcelHelper.cs 19 KB

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