public bool ImportXLS(string PayLoadFile) { this.CCFParameters.Clear(); Application xlApp = null; Workbook xlBook = null; Worksheet xlSheet = null; object missing = System.Reflection.Missing.Value; try { xlApp = new Application(); xlBook = xlApp.Workbooks.Add(PayLoadFile); xlSheet = xlBook.Sheets["Payload definition"]; Range xlRange = xlSheet.UsedRange; //Read Parameter CCFParameter CurrentParameter = new CCFParameter(); string LastParameterName = ""; int LastParameterNumber = 0; int LastEUCDBlockID = 0; for (int i = 3; i <= xlRange.Rows.Count; i++) { if (xlRange.Cells[i, PARAM_MDF_COLUMN].value != null) { int MDF = Convert.ToInt32(xlRange.Cells[i, PARAM_MDF_COLUMN].value, 16); if (MDF != CurrentParameter.MDF) //This is a new Parameter { //Save Old Parameter if (i != 3) CCFParameters.Add(CurrentParameter); //Create New Parameter CurrentParameter = new CCFParameter(); CurrentParameter.MDF = MDF; CurrentParameter.EUCDBlockID = Convert.ToInt32(xlRange.Cells[i, PARAM_EUCDBLOCKID_COLUMN].value, 16); if (CurrentParameter.EUCDBlockID == 0) CurrentParameter.EUCDBlockID = LastEUCDBlockID; else LastEUCDBlockID = CurrentParameter.EUCDBlockID; CurrentParameter.EUCDDescription = xlRange.Cells[i, PARAM_EUCDDESC_COLUMN].Text; CurrentParameter.T5Description = xlRange.Cells[i, PARAM_T5DESC_COLUMN].Text; CurrentParameter.ParameterName = xlRange.Cells[i, PARAM_PARAMETERNAME_COLUMN].Text; if (CurrentParameter.ParameterName == "") CurrentParameter.ParameterName = LastParameterName; else LastParameterName = CurrentParameter.ParameterName; CurrentParameter.ParameterNumber = Convert.ToInt32(xlRange.Cells[i, PARAM_PARAMETERNUMBER].value); if (CurrentParameter.ParameterNumber == 0) CurrentParameter.ParameterNumber = LastParameterNumber; else LastParameterNumber = CurrentParameter.ParameterNumber; CurrentParameter.StartByte = Convert.ToInt32(xlRange.Cells[i, PARAM_BYTE_COLUMN].value); CurrentParameter.StartBit = MDF & 0xF; CurrentParameter.ValueType = xlRange.Cells[i, PARAM_VALUETYPE_COLUMN].Text; CurrentParameter.ValueFormat = xlRange.Cells[i, PARAM_VALUEFORMAT_COLUMN].Text; //Read Bit Length string BitLengthRaw = xlRange.Cells[i, PARAM_BIT_COLUMN].Text; string[] BitRaw = BitLengthRaw.Trim().Split('-'); if (BitRaw.Length == 1) CurrentParameter.BitLength = 1; else CurrentParameter.BitLength = Convert.ToInt32(BitRaw[0]) - Convert.ToInt32(BitRaw[1]) + 1; //Set Parameter Level if (xlRange[i, PARAM_DESC2_COLUMN].Text != "" && xlRange[i, PARAM_DESC1_COLUMN].Text != "") { CurrentParameter.Level = 2; CurrentParameter.Description = xlRange.Cells[i, PARAM_DESC1_COLUMN].Text; } else if (xlRange[i, PARAM_DESC2_COLUMN].Text == "") CurrentParameter.Level = 0; else if (xlRange[i, PARAM_DESC1_COLUMN].Text == "") CurrentParameter.Level = 1; } //Create New Option //Generate Value CCFOption theCCFOption = new CCFOption(); if (CurrentParameter.ValueType == "Enumerated") { string strValue = xlRange.Cells[i, PARAM_VALUE_COLUMN].Text.Trim(); try { if (CurrentParameter.ValueFormat == "0x") theCCFOption.Value = Convert.ToInt32(strValue, 16); else if (CurrentParameter.ValueFormat == "0b") theCCFOption.Value = Convert.ToInt32(strValue.Replace("n", "").Replace(" ", "").Replace("X", "0"), 2); } catch { theCCFOption.Value = -1; } } //Generate Desc if (CurrentParameter.Level == 0) theCCFOption.Description = xlRange.Cells[i, PARAM_DESC1_COLUMN].Text; else theCCFOption.Description = xlRange.Cells[i, PARAM_DESC2_COLUMN].Text; //Add Option CurrentParameter.Options.Add(theCCFOption); } } //Save Last CCFParameters.Add(CurrentParameter); xlBook.Close(false); xlApp.Quit(); return true; } catch { //xlBook.Close(); //xlApp.Quit(); return false; } }
public bool ImportXML(string XMLFile) { CCFParameters.Clear(); try { XmlDocument Root = new XmlDocument(); Root.Load(XMLFile); XmlNodeList XMLCCFParameters = Root.GetElementsByTagName("CCFParameter"); foreach (XmlNode t in XMLCCFParameters) { CCFParameter theParameter = new CCFParameter(); theParameter.BitLength = Convert.ToInt32(t.Attributes["BitLength"].Value); theParameter.Description = t.Attributes["Description"].Value; theParameter.EUCDBlockID = Convert.ToInt32(t.Attributes["EUCDBlockID"].Value); theParameter.EUCDDescription = t.Attributes["Description"].Value; theParameter.Level = Convert.ToInt32(t.Attributes["Level"].Value); theParameter.MDF = Convert.ToInt32(t.Attributes["MDF"].Value); theParameter.ParameterName = t.Attributes["ParameterName"].Value; theParameter.ParameterNumber = Convert.ToInt32(t.Attributes["ParameterNumber"].Value); theParameter.StartBit = Convert.ToInt32(t.Attributes["StartBit"].Value); theParameter.StartByte = Convert.ToInt32(t.Attributes["StartByte"].Value); theParameter.T5Description = t.Attributes["T5Description"].Value; theParameter.ValueFormat = t.Attributes["ValueFormat"].Value; theParameter.ValueType = t.Attributes["ValueType"].Value; XmlNode XMLOptions = t.ChildNodes[0]; foreach (XmlNode p in XMLOptions) { CCFOption theOption = new CCFOption(); theOption.Description = p.Attributes["Description"].Value; theOption.Value = Convert.ToInt32(p.Attributes["Value"].Value); theParameter.Options.Add(theOption); } CCFParameters.Add(theParameter); } return true; } catch { return false; } }