36 var devices =
new List<DeviceInfo>();
37 Excel.Application xlApp =
null;
38 Excel.Workbook xlWorkbook =
null;
39 Excel.Worksheet xlWorksheet =
null;
40 Excel.Range xlRange =
null;
44 xlApp =
new Excel.Application();
45 xlWorkbook = xlApp.Workbooks.Open(filePath);
46 xlWorksheet = xlWorkbook.Sheets[1];
47 xlRange = xlWorksheet.UsedRange;
49 var rowCount = xlRange.Rows.Count;
50 var colCount = xlRange.Columns.Count;
53 var columnMap =
new Dictionary<string, int>();
54 for (var j = 1; j <= colCount; j++)
56 if (xlRange.Cells[1, j].Value2 !=
null)
58 columnMap[xlRange.Cells[1, j].Value2.ToString()] = j;
63 string[] requiredColumns = {
"OrderNumber",
"Version",
"Name",
"DeviceName" };
64 foreach (var column
in requiredColumns)
66 if (!columnMap.ContainsKey(column))
68 throw new Exception($
"Required column '{column}' not found in Excel file");
73 for (var i = 2; i <= rowCount; i++)
75 if (xlRange.Cells[i, columnMap[
"OrderNumber"]].Value2 ==
null)
89 if (columnMap.TryGetValue(
"Type", out var value))
95 if (!
string.IsNullOrWhiteSpace(device.OrderNumber) &&
96 !
string.IsNullOrWhiteSpace(device.Version) &&
97 !
string.IsNullOrWhiteSpace(device.Name) &&
98 !
string.IsNullOrWhiteSpace(device.DeviceName))
109 Marshal.ReleaseComObject(xlRange);
112 if (xlWorksheet !=
null)
114 Marshal.ReleaseComObject(xlWorksheet);
117 if (xlWorkbook !=
null)
119 xlWorkbook.Close(
false);
120 Marshal.ReleaseComObject(xlWorkbook);
126 Marshal.ReleaseComObject(xlApp);
163 if (!File.Exists(excelFilePath))
165 throw new FileNotFoundException(
"Excel file not found", excelFilePath);
170 foreach (var deviceInfo
in deviceInfos)
174 MacManagement.LoggingService.LogMessage(LogTypes.GenerationInfo,
175 $
"Processing device: {deviceInfo}", module.Name);
177 var typeIdentifier =
string.IsNullOrWhiteSpace(deviceInfo.Type)
178 ? $
"OrderNumber:{deviceInfo.OrderNumber}/{deviceInfo.Version}"
179 : $
"OrderNumber:{deviceInfo.OrderNumber}/{deviceInfo.Version}/{deviceInfo.Type}";
181 MacManagement.LoggingService.LogMessage(LogTypes.GenerationInfo,
182 $
"Creating device with identifier: {typeIdentifier}", module.Name);
185 deviceInfo.DeviceName);
187 MacManagement.LoggingService.LogMessage(LogTypes.GenerationInfo,
188 $
"Successfully added device: {deviceInfo.DeviceName}", module.Name);
192 MacManagement.LoggingService.LogMessage(LogTypes.GenerationError,
193 $
"Failed to process device {deviceInfo}. Error: {ex.Message}", module.Name);
199 throw new Exception($
"Error creating devices from Excel file: {ex.Message}", ex);