private static Dictionary<string, int> GenerateFieldColumnMapping(List<FieldTitleMapping> attrs, Worksheet worksheet) { var propColumnIndexDict = new Dictionary<string, int>(); try { var importExcelTitleRow = worksheet.Cells.Rows[0]; if (importExcelTitleRow[0].IsMerged) { importExcelTitleRow = worksheet.Cells.Rows[1]; } var titleRowEnumerator = attrs.GetEnumerator(); var rowEnumerator = importExcelTitleRow.GetEnumerator(); while (titleRowEnumerator.MoveNext()) { var currentConfigField = titleRowEnumerator.Current; bool isGetMapperSuccess = false; var columIndex = 0; while (rowEnumerator.MoveNext()) { var currentCell = rowEnumerator.Current as Cell; if (!string.IsNullOrEmpty(currentCell.StringValue)) { var curTitleI18nName = currentConfigField.FieldAttribute.TitleKey.Trim(); if (curTitleI18nName.Equals(RemoveBadgeOfTitle(currentCell.StringValue), StringComparison.OrdinalIgnoreCase)) { propColumnIndexDict.Add(currentConfigField.FieldName, columIndex); isGetMapperSuccess = true; break; } } columIndex++; } if (!isGetMapperSuccess) { //propColumnIndexDict.Clear(); //return propColumnIndexDict; } rowEnumerator.Reset(); } } catch (Exception ex) { propColumnIndexDict.Clear(); } return propColumnIndexDict; } 优化这段代码
时间: 2024-02-10 22:18:07 浏览: 64
这段代码的主要问题在于它包含了多个嵌套的循环,导致时间复杂度很高,需要进行优化。以下是优化建议:
1. 将循环中的逻辑提取出来,减少循环嵌套。可以将获取标题行的逻辑提取到一个方法中,避免每次循环都去获取一遍,同时也可以将获取当前单元格的逻辑提取到一个方法中。
2. 在获取标题行后,将标题行转换为一个字典,将列名作为 key,列索引作为 value。这样在后面的循环中,就可以直接通过列名获取列索引,而不需要再次遍历标题行。
3. 在内层循环中,可以通过使用 LINQ 的 FirstOrDefault 方法,来查找第一个满足条件的单元格,而不需要使用循环遍历。这样可以大大减少时间复杂度。
4. 对于异常的处理,可以考虑将异常抛出,而不是在方法中直接清空字典。这样可以将异常的处理交给上层调用者来处理,也可以更好地定位问题。
优化后的代码如下:
```
private static Dictionary<string, int> GenerateFieldColumnMapping(List<FieldTitleMapping> attrs, Worksheet worksheet)
{
var propColumnIndexDict = new Dictionary<string, int>();
try
{
var titleRow = GetTitleRow(worksheet);
var titleDict = titleRow.Cells.Select((cell, index) => new { cell.StringValue, index })
.Where(x => !string.IsNullOrEmpty(x.StringValue))
.ToDictionary(x => RemoveBadgeOfTitle(x.StringValue), x => x.index);
foreach (var configField in attrs)
{
var titleKey = configField.FieldAttribute.TitleKey.Trim();
if (titleDict.TryGetValue(titleKey, out int columnIndex))
{
propColumnIndexDict.Add(configField.FieldName, columnIndex);
}
else
{
// 如果找不到对应的列,可以考虑抛出异常,而不是清空字典
throw new Exception($"无法找到列名为 \"{titleKey}\" 的列");
}
}
}
catch (Exception ex)
{
// 异常的处理,可以考虑将异常抛出,而不是在方法中直接清空字典
throw ex;
}
return propColumnIndexDict;
}
private static Row GetTitleRow(Worksheet worksheet)
{
var titleRow = worksheet.Cells.Rows[0];
if (titleRow[0].IsMerged)
{
titleRow = worksheet.Cells.Rows[1];
}
return titleRow;
}
private static Cell GetCurrentCell(IEnumerator enumerator)
{
if (enumerator.MoveNext())
{
return enumerator.Current as Cell;
}
return null;
}
private static string RemoveBadgeOfTitle(string title)
{
// 去掉标题中的徽章,例如:(必填)
return Regex.Replace(title, @"\((.*?)\)", "").Trim();
}
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
使用BeautifulSoup将以下代码转换为json <html><head>123</head><body> <input type="checkbox" v-model="RemitInBox" class="RemitInBox v-pristine v-valid" name="RemitInBox" value="0" v-checked="data.CheckFlag" v-disabled="data.FndDrc!='01' || data.BusiState!='10'" id="List0_RemitIn" v-click="clickBox($index,data)"> IR00252300347A IR00252300347A 2023-05-25 USD32,993.50 1/FLOKSER KIMYA SANAYI VE TICARET 1... 31980005000107125 入账 退汇 待客户确认 受理成功 打印 </body></html>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)