//------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #region Specialied Excel Import /// <summary> /// Loads from excel. /// </summary> /// <param name="strInputFilename">The string input filename.</param> private void LoadFromExcel(string strInputFilename) { if (strInputFilename.IsEmpty()) { throw new ArgumentException("The input filename can not be empty!", nameof(strInputFilename)); } Excel.Application excel = new Excel.Application(); try { Excel.Workbook wb = excel.Workbooks.Open(strInputFilename); // We guess, the RF Device Data is on sheet #1, starting @ row 1 ... Excel.Worksheet maindatasheet = wb.Sheets[1] as Excel.Worksheet; Excel.Range range = maindatasheet.UsedRange; int iColumnCount = range.Columns.Count; if (iColumnCount < 17) { throw new Exception( $"The Current Excel File Can Not Be Imported Because There Are Only {iColumnCount} Columns!\nWe Need At Least 17 Columns For A Good Import."); } int iRowCount = range.Rows.Count; if (iRowCount < 2) { throw new Exception( $"The Current Excel File Can Not Be Imported Because There Are Only {iRowCount} Rows!\nWe Need At Least 2 Rows For A Good Import."); } RFDeviceList dlImportedDevices = new RFDeviceList(); for (int iRow = 2; iRow < iRowCount + 1; iRow++) { RFDevice device = new RFDevice { DeviceSource = DeviceSource.DataImport, }; for (int iColumn = 1; iColumn < 19 + 1; iColumn++) { object value = (range.Cells[iRow, iColumn] as Excel.Range).Value2; if (value == null) { continue; } // Hier ist zu überlegen wie wir das ganze generisch machen können falls sich das Model nochmal ändert ... switch (iColumn) { case 1: device.StartTime = Convert.ToDouble(value); break; case 2: device.Id = Convert.ToInt32(value); break; case 3: device.Latitude = Convert.ToDouble(value); break; case 4: device.Longitude = Convert.ToDouble(value); break; case 5: device.Altitude = Convert.ToInt32(value); break; case 6: device.Roll = Convert.ToDouble(value); break; case 7: device.Pitch = Convert.ToDouble(value); break; case 8: device.Yaw = Convert.ToDouble(value); break; case 9: device.RxTxType = RxTxTypes.FromInt(device.Id, Convert.ToInt32(value)); break; case 10: device.AntennaType = (AntennaType)Convert.ToInt32(value); break; case 11: device.Gain_dB = Convert.ToDouble(value); break; case 12: device.CenterFrequency_Hz = Convert.ToDouble(value); break; case 13: device.Bandwidth_Hz = Convert.ToDouble(value); break; case 14: device.SignalToNoiseRatio_dB = Convert.ToDouble(value); break; case 15: device.XPos = Convert.ToInt32(value); break; case 16: device.YPos = Convert.ToInt32(value); break; case 17: device.ZPos = Convert.ToInt32(value); break; case 18: device.Remark = Convert.ToString(value); break; case 19: device.TechnicalParameters = Convert.ToString(value); break; } } dlImportedDevices.Add(device); } AddRFDevices(dlImportedDevices); } catch (Exception ex) { MB.Error(ex); } excel.Quit(); excel = null; GC.WaitForPendingFinalizers(); GC.Collect(); }
/// <summary> /// Gets the property. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="eParent">The e parent.</param> /// <param name="strElementName">Name of the string element.</param> /// <param name="tDefault">The t default.</param> /// <returns></returns> /// <exception cref="NotSupportedException"></exception> public static T GetProperty <T>(this XElement eParent, string strElementName, T tDefault) { Type t = typeof(T); if (t.IsEnum) { return(GetEnumFromNode(eParent, strElementName, tDefault)); } //----------------------------------------------------------------- // Standard DataTypes if (t == typeof(string)) { string xDefault = (string)((object)tDefault); return((T)((object)GetStringFromNode(eParent, strElementName) ?? xDefault)); } if (t == typeof(bool)) { bool xDefault = (bool)((object)tDefault); return((T)((object)GetBoolFromNode(eParent, strElementName) ?? xDefault)); } if (t == typeof(double)) { double xDefault = (double)((object)tDefault); return((T)((object)GetDoubleFromNodePoint(eParent, strElementName) ?? xDefault)); } if (t == typeof(uint)) { uint xDefault = (uint)((object)tDefault); return((T)((object)GetUInt32FromNode(eParent, strElementName) ?? xDefault)); } if (t == typeof(int)) { int xDefault = (int)((object)tDefault); return((T)((object)GetInt32FromNode(eParent, strElementName) ?? xDefault)); } if (t == typeof(byte)) { byte xDefault = (byte)((object)tDefault); return((T)((object)((byte)(GetUInt32FromNode(eParent, strElementName) ?? xDefault)))); } if (t == typeof(ulong)) { ulong xDefault = (ulong)((object)tDefault); return((T)((object)(GetUInt32FromNode(eParent, strElementName) ?? xDefault))); } //----------------------------------------------------------------- // Standard Classes && Structs ... if (t == typeof(Guid)) { Guid xDefault = (Guid)((object)tDefault); return((T)((object)(GetGuidFromNode(eParent, strElementName) ?? xDefault))); } if (t == typeof(DateTime)) { DateTime xDefault = (DateTime)((object)tDefault); return((T)((object)(GetDateTimeFromNodeUTC(eParent, strElementName) ?? xDefault))); //return tDefault; ; } if (t == typeof(Color)) { Color xDefault = (Color)((object)tDefault); return((T)((object)GetColorFromNode(eParent, strElementName, xDefault))); } if (t == typeof(FileInfo)) { FileInfo xDefault = (FileInfo)((object)tDefault); return((T)((object)GetFileInfoFromNode(eParent, strElementName, xDefault))); } if (t == typeof(DirectoryInfo)) { DirectoryInfo xDefault = (DirectoryInfo)((object)tDefault); return((T)((object)GetDirectoryInfoFromNode(eParent, strElementName, xDefault))); } //----------------------------------------------------------------- //// ESRI DataTypes ... //if (t == typeof(MapPoint)) //{ // MapPoint xDefault = (MapPoint)((object)tDefault); // return (T)((object)((MapPoint)(GetMapPointFromNode(eParent, strElementName) ?? xDefault))); //} //if (t == typeof(Viewpoint)) //{ // Viewpoint xDefault = (Viewpoint)((object)tDefault); // return (T)((object)((Viewpoint)(GetViewpointFromNode(eParent, strElementName) ?? xDefault))); //} if (t == typeof(BitmapSource)) { BitmapSource xDefault = (BitmapSource)((object)tDefault); return((T)((object)(GetBitmapSourceFromNode(eParent, strElementName) ?? xDefault))); } //----------------------------------------------------------------- if (t == typeof(Latitude)) { Latitude xDefault = (Latitude)((object)tDefault); return((T)((object)new Latitude(GetDoubleFromNodePoint(eParent, strElementName) ?? xDefault))); } if (t == typeof(Longitude)) { Longitude xDefault = (Longitude)((object)tDefault); return((T)((object)new Longitude(GetDoubleFromNodePoint(eParent, strElementName) ?? xDefault))); } if (t == typeof(Altitude)) { Altitude xDefault = (Altitude)((object)tDefault); return((T)((object)new Altitude(GetInt32FromNode(eParent, strElementName) ?? xDefault))); } //----------------------------------------------------------------- if (t == typeof(RxTxType)) { RxTxType xDefault = (RxTxType)((object)tDefault); return((T)((object)RxTxTypes.FromString(GetStringFromNode(eParent, strElementName)))); } if (t == typeof(Bandwidth)) { Bandwidth xDefault = (Bandwidth)((object)tDefault); return((T)((object)new Bandwidth(GetDoubleFromNodePoint(eParent, strElementName) ?? xDefault))); } if (t == typeof(Frequency)) { Frequency xDefault = (Frequency)((object)tDefault); return((T)((object)new Frequency(GetDoubleFromNodePoint(eParent, strElementName) ?? xDefault))); } if (t == typeof(Gain)) { Gain xDefault = (Gain)((object)tDefault); return((T)((object)new Gain(GetDoubleFromNodePoint(eParent, strElementName) ?? xDefault))); } if (t == typeof(SignalToNoiseRatio)) { SignalToNoiseRatio xDefault = (SignalToNoiseRatio)((object)tDefault); return((T)((object)new SignalToNoiseRatio(GetDoubleFromNodePoint(eParent, strElementName) ?? xDefault))); } //----------------------------------------------------------------- throw new NotSupportedException(); }