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; } worksheet使用的是 Aspose.Cells,优化这段代码
时间: 2024-03-28 20:42:28 浏览: 21
首先,可以将 `importExcelTitleRow` 的判断和赋值提取到一个方法中,避免重复代码:
```
private static Row GetImportExcelTitleRow(Worksheet worksheet)
{
var importExcelTitleRow = worksheet.Cells.Rows[0];
if (importExcelTitleRow[0].IsMerged)
{
importExcelTitleRow = worksheet.Cells.Rows[1];
}
return importExcelTitleRow;
}
```
接着,可以将 `titleRowEnumerator` 和 `rowEnumerator` 的初始化提取到 `while` 循环外部,避免每次循环都创建新的实例:
```
var titleRowEnumerator = attrs.GetEnumerator();
var rowEnumerator = GetImportExcelTitleRow(worksheet).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();
}
```
最后,可以将 `columIndex` 的自增放到 `if` 判断的外部,避免不必要的计算:
```
var titleRowEnumerator = attrs.GetEnumerator();
var rowEnumerator = GetImportExcelTitleRow(worksheet).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;
}
}
else
{
columIndex++;
}
}
if (!isGetMapperSuccess)
{
//propColumnIndexDict.Clear();
//return propColumnIndexDict;
}
rowEnumerator.Reset();
}
```
综上所述,优化后的代码如下:
```
private static Dictionary<string, int> GenerateFieldColumnMapping(List<FieldTitleMapping> attrs, Worksheet worksheet)
{
var propColumnIndexDict = new Dictionary<string, int>();
try
{
var titleRowEnumerator = attrs.GetEnumerator();
var rowEnumerator = GetImportExcelTitleRow(worksheet).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;
}
}
else
{
columIndex++;
}
}
if (!isGetMapperSuccess)
{
//propColumnIndexDict.Clear();
//return propColumnIndexDict;
}
rowEnumerator.Reset();
}
}
catch (Exception ex)
{
propColumnIndexDict.Clear();
}
return propColumnIndexDict;
}
private static Row GetImportExcelTitleRow(Worksheet worksheet)
{
var importExcelTitleRow = worksheet.Cells.Rows[0];
if (importExcelTitleRow[0].IsMerged)
{
importExcelTitleRow = worksheet.Cells.Rows[1];
}
return importExcelTitleRow;
}
```