namespace KPlan.RefActions { using Eplan.EplApi.ApplicationFramework; using Eplan.EplApi.Base; using Eplan.EplApi.DataModel; using Eplan.EplApi.HEServices; using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Xml; using System.Collections.Generic; using System.Text; using System.Diagnostics; using Microsoft.Office.Interop.Excel; using System.Data; using System.Diagnostics; public class EplimPublicMethod { private string eplan_temp_path; private List parts_list; private string PROJ_FULL_NAME; private string PROJ_NAME; private string xml_file_name; private string zwl_file_name; public EplimPublicMethod() { this.parts_list = new List(); } public bool Execute() { Boolean isOk = false; SelectionSet set; Eplan.EplApi.DataModel.Project project; string str; this.eplan_temp_path = Environment.GetEnvironmentVariable("TEMP").ToString() + @"\"; try { set = new SelectionSet(); project = set.GetCurrentProject(true); if (project == null) { MessageBox.Show("项目不存在。"); return false; } // flag2 = 0; // goto Label_00A9; // Label_0052: str = this.TcPath(project.ProjectFullName); if (str == null) { MessageBox.Show("项目名称为空。"); return false; } isOk = this.ZipProject(project, str); this.WriteEplanMsg(); } catch { MessageBox.Show("Error!"); return false; } return isOk; } public List get_parts_list() { List list; list = this.parts_list; return list; } public string get_proj_full_name() { string str; str = this.PROJ_FULL_NAME; return str; } public string get_proj_name() { string str; str = this.PROJ_NAME; return str; } private string TcPath(string projectName) { string str; string str2; string str3; string str4; string str5; string str6; string str7; string str8; string[] strArray; bool flag; str = null; str2 = @"Eplan\Electric P8\EplLog.txt"; str3 = System.Windows.Forms.Application.CommonAppDataPath; str4 = System.Windows.Forms.Application.CompanyName; str5 = System.Windows.Forms.Application.ProductName; str6 = System.Windows.Forms.Application.ProductVersion; str7 = str4 + @"\" + str5 + @"\" + str6; if ((str3.Contains(str7))) { goto Label_007A; } str3 = str3.Substring(0, str3.IndexOf(str7)); Label_007A: str8 = this.eplan_temp_path; return str8; } public bool WriteEplanMsg() { bool flag; string str; FileStream stream; StreamWriter writer; Exception exception; try { stream = new FileStream(this.eplan_temp_path + "eplan_msg.txt", FileMode.OpenOrCreate, FileAccess.Write); stream.Seek(0L, 0); stream.SetLength(0L); writer = new StreamWriter(stream); writer.WriteLine(this.PROJ_NAME); writer.WriteLine(this.zwl_file_name); writer.WriteLine(this.xml_file_name); writer.Close(); stream.Close(); } catch (Exception exception1) { exception = exception1; MessageBox.Show(exception.Message); } return true; } //--------------------------------将EXCEL转换成xml------------------------------------------------- /// /// 将xml转换成XML /// /// /// /// private List xlsToXml(String excelFilePath) { Stopwatch wath = new Stopwatch(); Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Sheets sheets = null; Microsoft.Office.Interop.Excel.Workbook workbook = null; object oMissiong = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Range range = null; List dt = new List(); wath.Start(); try { if (app == null) { return null; } workbook = app.Workbooks.Open(excelFilePath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong); //将数据读入到DataTable中——Start sheets = workbook.Worksheets; // UI.GetUI().NXMessageBox.Show("MSG",NXMessageBox.DialogType.Information,"111"); Microsoft.Office.Interop.Excel.Worksheet worksheet = sheets.get_Item(1) as Microsoft.Office.Interop.Excel.Worksheet;//读取第一张表 // UI.GetUI().NXMessageBox.Show("MSG", NXMessageBox.DialogType.Information, "22"); if (worksheet == null) return null; string cellContent = null; int iRowCount = worksheet.UsedRange.Rows.Count; int iColCount = worksheet.UsedRange.Columns.Count; int partNo = -1; int partCount = -1; int partErp = -1; int xmh = -1; //负责列头Start for (int iCol = 1; iCol <= iColCount; iCol++) { range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, iCol]; cellContent = (range.Value2 == null) ? "" : range.Text.ToString(); switch (cellContent) { case "物料编码": partNo = iCol; break; case "数量": partCount = iCol; break; case "ERP编码": partErp = iCol; break; case "项目号": xmh = iCol; break; default: break; } } for (int iRow = 2; iRow <= iRowCount; iRow++) { ElecPartInfo partInfo = new ElecPartInfo(); range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, partCount]; partInfo.P_ARTICLEREF_COUNT = (range.Value2 == null) ? "" : range.Text.ToString(); range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, partErp]; partInfo.P_ARTICLE_ERPNR = (range.Value2 == null) ? "" : range.Text.ToString(); range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, partNo]; partInfo.P_ARTICLEREF_PARTNO = (range.Value2 == null) ? "" : range.Text.ToString(); range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, xmh]; partInfo.P_ARTICLEREF_IDENTNAME = (range.Value2 == null) ? "" : range.Text.ToString(); Microsoft.Office.Interop.Excel.Range rangetemp = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[iRow, 1]; String cellStr = (rangetemp.Value2 == null) ? "" : rangetemp.Text.ToString(); if (cellStr.Trim().Equals("")) { break; } if (partInfo.P_ARTICLEREF_COUNT != null && !partInfo.P_ARTICLEREF_COUNT.Equals("0")) dt.Add(partInfo); } wath.Stop(); TimeSpan ts = wath.Elapsed; return dt; } catch (Exception ex) { return dt; } finally { workbook.Close(false, oMissiong, oMissiong); System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook); workbook = null; app.Workbooks.Close(); app.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(app); app = null; GC.Collect(); GC.WaitForPendingFinalizers(); } return dt; } /// /// 写入XML文件 /// /// /// /// private bool writeXml(String xmlPath, List partList) { bool flag = true; StringBuilder xmlResult = new StringBuilder(""); if (partList != null && partList.Count > 0) { xmlResult.AppendFormat("", partList.Count); foreach (ElecPartInfo part in partList) { xmlResult.AppendFormat("", part.P_ARTICLEREF_IDENTNAME); xmlResult.AppendFormat("", part.P_ARTICLEREF_PARTNO, part.P_ARTICLE_ERPNR, part.P_ARTICLEREF_COUNT); xmlResult.Append(""); } xmlResult.Append(""); } //写入文件 try { //1.创建文件流 FileStream fileStream = new FileStream(xmlPath, FileMode.Create); //2.创建写入器 StreamWriter streamWriter = new StreamWriter(fileStream); //3.将内容写入文件 streamWriter.WriteLine(xmlResult); //4.关闭写入器 streamWriter.Close(); //5.关闭文件流 fileStream.Close(); } catch (Exception e) { } return flag; } //------------------------------------------------------------------------------------- private bool ZipProject(Eplan.EplApi.DataModel.Project currentProject, string tcPath) { bool flag; string str; ActionCallingContext context; string[] strArray; string str2; ActionCallingContext context2; ActionCallingContext context3; PartsService service; Progress progress; CommandLineInterpreter interpreter; Exception exception; try { this.PROJ_FULL_NAME = currentProject.ProjectFullName; str = this.PROJ_FULL_NAME + ".zw1"; this.PROJ_NAME = currentProject.ProjectName; this.zwl_file_name = ""; this.zwl_file_name = tcPath + this.PROJ_NAME + ".zw1"; context = new ActionCallingContext(); strArray = context.GetParameters(); context.AddParameter("TYPE", "PROJECT"); context.AddParameter("DESTINATIONPATH", tcPath); context.AddParameter("ARCHIVENAME", str); context.AddParameter("BACKUPMETHOD", "BACKUP"); context.AddParameter("BACKUPMEDIA", "DISK"); context.AddParameter("SPLITSIZE", "0.0"); context.AddParameter("BACKUPAMOUNT", "BACKUPAMOUNT_ALL"); context.AddParameter("SPLITSIZE", "0.0"); context.AddParameter("INCLEXTDOCS", "1"); context.AddParameter("INCLIMAGES", "1"); str2 = currentProject.ProjectDirectoryPath; context2 = new ActionCallingContext(); context2.AddParameter("TYPE", "MASTERDATA"); context2.AddParameter("ARCHIVENAME", "D.xls"); context2.AddParameter("PROJECTNAME", str2); context2.AddParameter("DESTINATIONPATH ", @"c:\"); context2.AddParameter("BACKUPMETHOD", "BACKUP"); context2.AddParameter("BACKUPMEDIA", "DISK"); context2.AddParameter("SOURCEPATH", ""); context2.AddParameter("SPLITSIZE", "0.0"); context2.AddParameter("BACKUPAMOUNT", "BAKAMOUNT_ALL"); context2.AddParameter("INCLEXTDOCS", "1"); context2.AddParameter("INCLIMAGES", "1"); context3 = new ActionCallingContext(); //context3.AddParameter("PROJECTNAME", "EXPORT"); context3.AddParameter("PROJECTNAME", str2); this.xml_file_name = tcPath + this.PROJ_NAME + ".xml"; String xls_file_name = tcPath + this.PROJ_NAME + ".xls"; context3.AddParameter("CONFIGSCHEME", "部件汇总表2(加ERP编码)"); context3.AddParameter("LANGUAGE", "zh_CN"); context3.AddParameter("DESTINATIONFILE", xls_file_name); //context3.AddParameter("MODE", "0"); //context3.AddParameter("ADDITIONAL_LANGUAGE", "1"); service = new PartsService(); progress = new Progress("SimpleProgress"); progress.BeginPart(100.0, ""); progress.ShowImmediately(); progress.SetAllowCancel(true); if (progress.Canceled()) { MessageBox.Show("操作终止"); progress.EndPart(true); return false; } interpreter = new CommandLineInterpreter(); if (!interpreter.Execute("backup", context)) { MessageBox.Show("项目不能备份"); progress.EndPart(true); return false; } if (!interpreter.Execute("label", context3)) { MessageBox.Show("导出XML失败"); progress.EndPart(true); return false; } List partList = xlsToXml(xls_file_name); writeXml(this.xml_file_name, partList); } catch (Exception exception1) { exception = exception1; MessageBox.Show(exception.Message); return false; } return true; } } /// /// 元器件解析单元 /// class ElecPartInfo { public String P_ARTICLE_ERPNR { get; set; } public String P_ARTICLEREF_PARTNO { get; set; } public String P_ARTICLEREF_COUNT { get; set; } public String P_ARTICLEREF_IDENTNAME { get; set; } } }