ExcelLoad.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using OfficeOpenXml;
  7. using Newtonsoft.Json;
  8. namespace digital
  9. {
  10. public class ExcelDataManager
  11. {
  12. private readonly string _excelPath;
  13. private readonly string _progressPath;
  14. private readonly List<Dictionary<string, string>> _rows = new List<Dictionary<string, string>>();
  15. private readonly HashSet<string> _usedKeys = new HashSet<string>();
  16. private int _currentIndex = 0;
  17. private string[] _headers;
  18. // 主键列名(用于进度跟踪)
  19. private string _primaryKeyColumn = "账号";
  20. public ExcelDataManager(string excelPath, string progressPath = "progress.json", string primaryKeyColumn = "账号")
  21. {
  22. _excelPath = excelPath;
  23. _progressPath = progressPath;
  24. _primaryKeyColumn = primaryKeyColumn;
  25. LoadExcelData();
  26. LoadProgress();
  27. }
  28. private void LoadExcelData()
  29. {
  30. FileInfo file = new FileInfo(_excelPath);
  31. using (ExcelPackage package = new ExcelPackage(file))
  32. {
  33. ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
  34. int rowCount = worksheet.Dimension.Rows;
  35. int colCount = worksheet.Dimension.Columns;
  36. // 读取标题行
  37. _headers = new string[colCount];
  38. for (int col = 1; col <= colCount; col++)
  39. {
  40. _headers[col - 1] = worksheet.Cells[1, col].Text.Trim();
  41. }
  42. // 读取数据行
  43. for (int row = 2; row <= rowCount; row++)
  44. {
  45. var rowData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  46. for (int col = 1; col <= colCount; col++)
  47. {
  48. string header = _headers[col - 1];
  49. string cellValue = worksheet.Cells[row, col].Text.Trim();
  50. rowData[header] = cellValue;
  51. }
  52. // 只添加有主键的行
  53. if (rowData.ContainsKey(_primaryKeyColumn))
  54. {
  55. _rows.Add(rowData);
  56. }
  57. }
  58. }
  59. }
  60. // 顺序获取下一行数据
  61. public Dictionary<string, string> GetNextRow()
  62. {
  63. while (_currentIndex < _rows.Count)
  64. {
  65. var row = _rows[_currentIndex++];
  66. string key = GetPrimaryKey(row);
  67. if (!_usedKeys.Contains(key))
  68. {
  69. _usedKeys.Add(key);
  70. return new Dictionary<string, string>(row);
  71. }
  72. }
  73. return null;
  74. }
  75. // 根据主键值获取整行数据
  76. public Dictionary<string, string> GetRowByKey(string keyValue)
  77. {
  78. if (string.IsNullOrWhiteSpace(keyValue)) return null;
  79. var row = _rows.FirstOrDefault(r =>
  80. GetPrimaryKey(r).Equals(keyValue, StringComparison.OrdinalIgnoreCase) &&
  81. !_usedKeys.Contains(GetPrimaryKey(r)));
  82. if (row != null)
  83. {
  84. _usedKeys.Add(GetPrimaryKey(row));
  85. return new Dictionary<string, string>(row);
  86. }
  87. return null;
  88. }
  89. // 根据任意列查询获取整行数据
  90. public Dictionary<string, string> FindRowByColumnValue(string columnName, string columnValue)
  91. {
  92. if (string.IsNullOrWhiteSpace(columnName)) return null;
  93. if (string.IsNullOrWhiteSpace(columnValue)) return null;
  94. var row = _rows.FirstOrDefault(r =>
  95. r.ContainsKey(columnName) &&
  96. r[columnName].Equals(columnValue, StringComparison.OrdinalIgnoreCase) &&
  97. !_usedKeys.Contains(GetPrimaryKey(r)));
  98. if (row != null)
  99. {
  100. _usedKeys.Add(GetPrimaryKey(row));
  101. return new Dictionary<string, string>(row);
  102. }
  103. return null;
  104. }
  105. // 根据主键值获取指定列的值
  106. public string GetColumnValue(string keyValue, string columnName)
  107. {
  108. var row = GetRowByKey(keyValue);
  109. return row != null && row.TryGetValue(columnName, out string value) ? value : null;
  110. }
  111. // 根据任意列查询获取指定列的值
  112. public string GetColumnValueByQuery(string queryColumn, string queryValue, string resultColumn)
  113. {
  114. var row = FindRowByColumnValue(queryColumn, queryValue);
  115. return row != null && row.TryGetValue(resultColumn, out string value) ? value : null;
  116. }
  117. // 获取主键值
  118. private string GetPrimaryKey(Dictionary<string, string> row)
  119. {
  120. return row.TryGetValue(_primaryKeyColumn, out string value) ? value : "";
  121. }
  122. // 保存进度
  123. public void SaveProgress()
  124. {
  125. var progress = new ProgressData
  126. {
  127. CurrentIndex = _currentIndex,
  128. UsedKeys = _usedKeys.ToList()
  129. };
  130. File.WriteAllText(_progressPath, JsonConvert.SerializeObject(progress));
  131. }
  132. // 加载进度
  133. private void LoadProgress()
  134. {
  135. if (File.Exists(_progressPath))
  136. {
  137. try
  138. {
  139. var json = File.ReadAllText(_progressPath);
  140. var progress = JsonConvert.DeserializeObject<ProgressData>(json);
  141. _currentIndex = progress.CurrentIndex;
  142. _usedKeys.UnionWith(progress.UsedKeys);
  143. }
  144. catch
  145. {
  146. ResetProgress();
  147. }
  148. }
  149. }
  150. // 重置进度
  151. public void ResetProgress()
  152. {
  153. _currentIndex = 0;
  154. _usedKeys.Clear();
  155. SaveProgress();
  156. }
  157. // 获取所有列名
  158. public IEnumerable<string> GetColumnNames()
  159. {
  160. return _headers;
  161. }
  162. // 查找所有匹配的行(不标记为已使用)
  163. public List<Dictionary<string, string>> FindAllRowsByColumnValue(string columnName, string columnValue)
  164. {
  165. return _rows.Where(r =>
  166. r.ContainsKey(columnName) &&
  167. r[columnName].Equals(columnValue, StringComparison.OrdinalIgnoreCase))
  168. .Select(r => new Dictionary<string, string>(r))
  169. .ToList();
  170. }
  171. // 多条件查询
  172. public Dictionary<string, string> FindRowByMultipleConditions(
  173. params (string column, string value)[] conditions)
  174. {
  175. foreach (var row in _rows)
  176. {
  177. bool match = true;
  178. string key = GetPrimaryKey(row);
  179. if (_usedKeys.Contains(key)) continue;
  180. foreach (var condition in conditions)
  181. {
  182. if (!row.TryGetValue(condition.column, out string value) ||
  183. !value.Equals(condition.value, StringComparison.OrdinalIgnoreCase))
  184. {
  185. match = false;
  186. break;
  187. }
  188. }
  189. if (match)
  190. {
  191. _usedKeys.Add(key);
  192. return new Dictionary<string, string>(row);
  193. }
  194. }
  195. return null;
  196. }
  197. // 获取当前进度状态
  198. public (int total, int used, int remaining) GetProgressStatus()
  199. {
  200. int total = _rows.Count;
  201. int used = _usedKeys.Count;
  202. return (total, used, total - used);
  203. }
  204. // 标记主键为已使用
  205. public void MarkKeyAsUsed(string keyValue)
  206. {
  207. if (!string.IsNullOrWhiteSpace(keyValue))
  208. {
  209. _usedKeys.Add(keyValue);
  210. }
  211. }
  212. private class ProgressData
  213. {
  214. public int CurrentIndex { get; set; }
  215. public List<string> UsedKeys { get; set; }
  216. }
  217. }
  218. }