如何在C#中将带特定参数的LoadAnimal函数移至Load函数中调用
问题需求
需要将LoadAnimal()函数的调用从LoadAnimals()中转移到Load()函数内,调整函数参数与实现逻辑,保持原有功能不变。
Excel文件结构
工作表1:Zoo
| Name | Location |
|---|---|
| Zoo | Paris |
| END |
工作表2:Animals
| Name | Species | Family | Sex | Date Of Birth |
|---|---|---|---|---|
| Zoe | Elephant | Elephantidae | F | 03/19/2004 |
| Victor | Deer | Cervidae | M | |
| Camille | Eagle | Accipitridae | F | 09/03/2108 |
| END |
动物专属工作表(示例)
工作表:Zoe
| Name | Unit | Value |
|---|---|---|
| Height | cm | 280 |
| Weight | kg | 4 000 |
| END |
工作表:Victor
| Name | Unit | Value |
|---|---|---|
| Height | cm | 150 |
| Weight | kg | 75 |
| END |
现有代码
Load函数
public bool Load(string fileName) { bool returnValue = false; if (File.Exists(fileName)) { SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false); if (LoadZoo(spreadsheetDocument)) { returnValue = true; } if (LoadAnimals(spreadsheetDocument)) { returnValue = true; } spreadsheetDocument.Close(); } return returnValue; }
LoadAnimals函数
public Dictionary<string, IXlsxAnimals> Animals { get; private set; } = new Dictionary<string, IXlsxAnimals>(); private bool LoadAnimals(SpreadsheetDocument spreadsheetDocument) { bool returnValue = false; WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; WorksheetPart worksheetPart = ExcelHelper.GetWorksheetFromSheetname(workbookPart, "Animals"); if (worksheetPart != null) { SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == "Animals"); if (sheet != null && sheet.Name == "Animals") { string endingCell = ""; foreach (Row row in sheetData.Descendants<Row>()) { endingCell = ExcelHelper.GetCellValue(workbookPart, sheetData, $"A{row.RowIndex}"); if (endingCell == null || endingCell == "END") { break; } string animalName = ExcelHelper.GetCellValue(workbookPart, sheetData, $"A{row.RowIndex}"); if (animalName == "Name" || Animals.ContainsKey(animalName)) { continue; } XlsxAnimals xlsxAnimals = new XlsxAnimals() { Name = animalName, Species = ExcelHelper.GetCellValue(workbookPart, sheetData, $"B{row.RowIndex}"), Family = ExcelHelper.GetCellValue(workbookPart, sheetData, $"C{row.RowIndex}"), Sex = ExcelHelper.GetCellValue(workbookPart, sheetData, $"D{row.RowIndex}"), DateOfBirth = ExcelHelper.GetCellValue(workbookPart, sheetData, $"E{row.RowIndex}"), Animal = new Dictionary<string, IXlsxAnimal>() }; Animals.Add(xlsxAnimals.Name, xlsxAnimals); returnValue = true; } } } return returnValue; }
原LoadAnimal函数
private bool LoadAnimal (SpreadsheetDocument spreadsheetDocument, Dictionary<string, IXlsxAnimal> Animal, XlsxAnimals xlsxAnimals) { bool returnValue = false; WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; WorksheetPart worksheetPart = ExcelHelper.GetworksheetFromSheetName(workbookPart, xlsxAnimals.Name); if (worksheetPart != null) { SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == xlsxAnimals.Name); if (sheet != null && sheet.Name == xlsxAnimals.Name) { string endingCell = ""; foreach (Row row in sheetData.Descendants<Row>()) { endingCell = ExcelHelper.GetCellValue(workbookPart, sheetData, $"A{row.RowIndex}"); if (endingCell == null || endingCell == "END") { break; } string propertyName = ExcelHelper.GetCellValue(workbookPart, sheetData, $"A{row.RowIndex}"); if (propertyName == "Name" || Animal.ContainsKey(propertyName)) { continue; } XlsxAnimal xlsxAnimal = new XlsxAnimal() { Name = propertyName, Unit = ExcelHelper.GetCellValue(workbookPart, sheetData, $"B{row.RowIndex}"), Value = ExcelHelper.GetCellValue(workbookPart, sheetData, $"C{row.RowIndex}") }; Animal.Add(xlsxAnimal.Name, xlsxAnimal); returnValue = true; } } } return returnValue; }
修改方案
1. 重构LoadAnimal函数
调整参数,让它直接遍历已加载的Animals字典,批量处理所有动物的工作表数据:
private bool LoadAnimal(SpreadsheetDocument spreadsheetDocument) { bool returnValue = false; if (Animals.Count == 0) { return returnValue; } WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; foreach (var animalEntry in Animals.Values) { WorksheetPart worksheetPart = ExcelHelper.GetworksheetFromSheetName(workbookPart, animalEntry.Name); if (worksheetPart == null) { continue; } SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); Sheet sheet = workbookPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == animalEntry.Name); if (sheet == null || sheet.Name != animalEntry.Name) { continue; } string endingCell = ""; foreach (Row row in sheetData.Descendants<Row>()) { endingCell = ExcelHelper.GetCellValue(workbookPart, sheetData, $"A{row.RowIndex}"); if (endingCell == null || endingCell == "END") { break; } string propertyName = ExcelHelper.GetCellValue(workbookPart, sheetData, $"A{row.RowIndex}"); if (propertyName == "Name" || animalEntry.Animal.ContainsKey(propertyName)) { continue; } XlsxAnimal xlsxAnimal = new XlsxAnimal() { Name = propertyName, Unit = ExcelHelper.GetCellValue(workbookPart, sheetData, $"B{row.RowIndex}"), Value = ExcelHelper.GetCellValue(workbookPart, sheetData, $"C{row.RowIndex}") }; animalEntry.Animal.Add(xlsxAnimal.Name, xlsxAnimal); returnValue = true; } } return returnValue; }
2. 修改Load函数
在LoadAnimals执行完成后调用新的LoadAnimal:
public bool Load(string fileName) { bool returnValue = false; if (File.Exists(fileName)) { SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false); try { if (LoadZoo(spreadsheetDocument)) { returnValue = true; } if (LoadAnimals(spreadsheetDocument)) { returnValue = true; } // 新增LoadAnimal调用,批量加载所有动物的专属工作表数据 if (LoadAnimal(spreadsheetDocument)) { returnValue = true; } } finally { spreadsheetDocument.Close(); } } return returnValue; }
3. 清理LoadAnimals函数
移除原函数中调用LoadAnimal的代码,确保该函数仅负责加载动物的基础信息,不处理专属工作表数据。
额外修正
原代码中存在一些拼写与逻辑问题,已在修改后的代码中修复:
workbookPart.workbook→workbookPart.Workbook(属性名大小写错误)FirstOrdefault→FirstOrDefault(方法名拼写错误)Wroksheet→Worksheet(类名拼写错误)ContainKey→ContainsKey(方法名拼写错误)XlsxAnimal.Name→xlsxAnimal.Name(变量名大小写错误)- 移除原代码中
while+foreach的无效嵌套循环,改为直接遍历行并判断终止条件
内容的提问来源于stack exchange,提问作者Quack_boi
相关产品推荐
相关产品推荐

