public void Init(BaseRegPLC R_I, string strName) { try { //初始化赋值 g_RegPLCBase = R_I; this.gpbReg.Header = strName; ShowPar_Invoke(); } catch (Exception ex) { Log.L_I.WriteError("LogicPLC", ex); } }
/// <summary> /// 导入excel /// </summary> /// <param name="path"></param> public virtual void SetRegFromExcel(object path) { try { BaseRegPLC baseRegPLC = ReadExcelReg.R_I.ReadExcel(path.ToString()); g_RegPLCBase.regStart = baseRegPLC.regStart; g_RegPLCBase.NumRegSet = baseRegPLC.NumRegSet; g_RegPLCBase.Reg_L.Clear(); g_RegPLCBase.Reg_L = baseRegPLC.Reg_L; ShowPar_Invoke(); } catch (Exception ex) { } }
/// <summary> /// 导入excel /// </summary> /// <param name="path"></param> public override void SetRegFromExcel(object path) { try { BaseRegPLC baseRegPLC = ReadExcelReg.R_I.ReadExcel(path.ToString()); g_RegPLCBase.regStart = baseRegPLC.regStart; g_RegPLCBase.NumRegSet = baseRegPLC.NumRegSet - 10 - ParCameraWork.P_I[ParCameraWork.NumCamera] * 4; g_RegPLCBase.Reg_L.Clear(); g_RegPLCBase.Reg_L = baseRegPLC.Reg_L; ShowPar_Invoke(); } catch (Exception ex) { } }
public BaseRegPLC ReadExcel(string path) { return(null); BaseRegPLC baseRegPLC = new BaseRegPLC(); //try //{ // CloseProcess("EXCEL"); // Excel.Application xlsApp = new Excel.Application(); //创建Application对象 // Excel.Workbook xlsBook; // object missing = Missing.Value; // xlsBook = xlsApp.Workbooks.Open(path, // missing, missing, missing, missing, missing, // missing, missing, missing, missing, missing, // missing, missing, missing, missing); // Excel.Worksheet xlsSheet = (Excel.Worksheet)xlsBook.ActiveSheet; // //起始寄存器 // Excel.Range range = (Excel.Range)xlsSheet.Cells[1, 2]; // baseRegPLC.regStart = range.Value2.ToString(); // range = (Excel.Range)xlsSheet.Cells[2, 2]; // baseRegPLC.NumRegSet = int.Parse(range.Value2.ToString()); // for (int i = 0; i < baseRegPLC.NumRegSet; i++) // { // RegPLC regPLC = new RegPLC(); // try // { // range = (Excel.Range)xlsSheet.Cells[i + 4, 1]; // string low = range.Value2.ToString(); // range = (Excel.Range)xlsSheet.Cells[i + 4, 2]; // string high = range.Value2.ToString(); // regPLC.NameReg = low + high; // try // { // range = (Excel.Range)xlsSheet.Cells[i + 4, 3]; // regPLC.PLCSend = range.Value2.ToString(); // } // catch (Exception ex) // { // } // try // { // range = (Excel.Range)xlsSheet.Cells[i + 4, 4]; // regPLC.PCSend = range.Value2.ToString(); // } // catch (Exception ex) // { // } // range = (Excel.Range)xlsSheet.Cells[i + 4, 5]; // regPLC.DblMin = double.Parse(range.Value2.ToString()); // range = (Excel.Range)xlsSheet.Cells[i + 4, 6]; // regPLC.DblMax = double.Parse(range.Value2.ToString()); // range = (Excel.Range)xlsSheet.Cells[i + 4, 7]; // regPLC.Co = double.Parse(range.Value2.ToString()); // try // { // range = (Excel.Range)xlsSheet.Cells[i + 4, 8]; // regPLC.Annotation = range.Value2.ToString(); // } // catch (Exception ex) // { // } // regPLC.NameCustomReg = ""; // regPLC.No = i; // } // catch (Exception) // { // MessageBox.Show("Excel寄存器配置有误"); // return baseRegPLC; // } // baseRegPLC.Reg_L.Add(regPLC); // } // try // { // xlsSheet = null; // xlsBook.Close(true, Type.Missing, Type.Missing); // xlsBook = null; // xlsApp.Quit(); // xlsApp = null; // } // catch (Exception ex) // { // } // return baseRegPLC; //} //catch (Exception ex) //{ // return null; //} //finally //{ // CloseProcess("EXCEL"); //} }
public BaseRegPLC ReadExcel(string path) { BaseRegPLC baseRegPLC = new BaseRegPLC(); try { CloseProcess("EXCEL"); Excel.Application xlsApp = new Excel.Application(); //创建Application对象 Excel.Workbook xlsBook; object missing = Missing.Value; xlsBook = xlsApp.Workbooks.Open(path, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); Excel.Worksheet xlsSheet = (Excel.Worksheet)xlsBook.ActiveSheet; //起始寄存器 Excel.Range range = (Excel.Range)xlsSheet.Cells[1, 2]; baseRegPLC.regStart = range.Value2.ToString(); range = (Excel.Range)xlsSheet.Cells[2, 2]; baseRegPLC.NumRegSet = int.Parse(range.Value2.ToString()); for (int i = 0; i < baseRegPLC.NumRegSet; i++) { RegPLC regPLC = new RegPLC(); try { range = (Excel.Range)xlsSheet.Cells[i + 4, 1]; string low = range.Value2.ToString(); range = (Excel.Range)xlsSheet.Cells[i + 4, 2]; string high = range.Value2.ToString(); regPLC.NameReg = low + high; try { range = (Excel.Range)xlsSheet.Cells[i + 4, 3]; regPLC.PLCSend = range.Value2.ToString(); } catch (Exception ex) { } try { range = (Excel.Range)xlsSheet.Cells[i + 4, 4]; regPLC.PCSend = range.Value2.ToString(); } catch (Exception ex) { } range = (Excel.Range)xlsSheet.Cells[i + 4, 5]; regPLC.DblMin = double.Parse(range.Value2.ToString()); range = (Excel.Range)xlsSheet.Cells[i + 4, 6]; regPLC.DblMax = double.Parse(range.Value2.ToString()); range = (Excel.Range)xlsSheet.Cells[i + 4, 7]; regPLC.Co = double.Parse(range.Value2.ToString()); try { range = (Excel.Range)xlsSheet.Cells[i + 4, 8]; regPLC.Annotation = range.Value2.ToString(); } catch (Exception ex) { } regPLC.NameCustomReg = ""; regPLC.No = i; } catch (Exception) { MessageBox.Show("Excel寄存器配置有误"); return(baseRegPLC); } baseRegPLC.Reg_L.Add(regPLC); } try { xlsSheet = null; xlsBook.Close(true, Type.Missing, Type.Missing); xlsBook = null; xlsApp.Quit(); xlsApp = null; } catch (Exception ex) { } return(baseRegPLC); } catch (Exception ex) { return(null); } finally { CloseProcess("EXCEL"); } }