using Eplan.EplApi.Base; using Eplan.EplApi.DataModel; using Microsoft.Office.Interop.Excel; using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; using KPlan.Util; using KPlan.RefActions; namespace KPlan.RefActions { public partial class CheckForm : Form { public String projectCode = ""; public Project currentProject = null; public string xmlFilePath = ""; public CheckForm() { InitializeComponent(); } private void CheckForm_Load(object sender, EventArgs e) { TCTool.ReadConfigs(); int titleNum = TCTool.BomTitleName.Count; this.ComListView.Columns.Add("检查结果", 80, HorizontalAlignment.Left); //一步添加表头信息 foreach (string str in TCTool.BomTitleName) { this.ComListView.Columns.Add(str, 120, HorizontalAlignment.Left); //一步添加表头信息 } Progress progress = new Progress("EnhancedProgress"); progress.SetActionText("正在加载表单..."); progress.SetTitle("加载表单"); progress.BeginPart(100.0, "进度"); progress.ShowImmediately(); progress.SetAllowCancel(true); if (!ReadXMLFile()) { btnGenerateList.Enabled = false; } else { btnGenerateList.Enabled = true; } progress.EndPart(true); } /** * * 功能:解析partslist XML文件, * 过滤掉不需要的部件, * 得到需要的部件列表的信息 * * 入参:partslist XML 文件地址 * */ public bool ReadXMLFile() { XmlTextReader xtr = new XmlTextReader(xmlFilePath); //MessageBox.Show("xtr---------------------------" + xmlFilePath); string name_f = "@@"; bool is_dos = false; bool is_device = false; string device_name = ""; bool hasError = false; int record_code_idx = -1; int record_num_idx = -1; int PropertyNoum = TCTool.BomPropertyKey.Count; for (int step = 0; step < PropertyNoum; step++) { //MessageBox.Show("TCTool.BomPropertyKey[step]---------------------------" + TCTool.BomPropertyKey[step]); if (TCTool.BomPropertyKey[step].Equals("P_ARTICLE_ERPNR")) { record_code_idx = step; } if (TCTool.BomPropertyKey[step].Equals("P_ARTICLEREF_COUNT")) { record_num_idx = step; } } try { this.ComListView.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度 while (xtr.Read()) { bool is_other = true; if (is_device) { if (xtr.NodeType == XmlNodeType.EndElement) { is_device = false; } else { continue; } } if (xtr.NodeType != XmlNodeType.Element) { continue; } if (!xtr.HasAttributes) { continue; } string element_name = xtr.Name; bool is_P_ARTICLE_DISCONTINUED = false; bool is_wire = false; string wire_length = ""; string record_code = ""; string record_num = ""; string record_type = ""; string[] propertyValue = new string[PropertyNoum]; while (xtr.MoveToNextAttribute()) { if (xtr.Name == "P_ARTICLEREF_IDENTNAME") { device_name = xtr.Value; } //if (xtr.Name == "P_ARTICLEREF_IDENTNAME" && xtr.Value.Equals("=+")) //{ // xtr.MoveToElement(); // is_device = true; // break; //} if (xtr.Name == "P_ARTICLE_DISCONTINUED") //停用 { if (!xtr.Value.Equals("0")) { is_P_ARTICLE_DISCONTINUED = true; } } if (xtr.Name == "P_ARTICLE_PARTIAL_LENGTH_VALUE") //此处标识出连接线的部件 { is_wire = true; wire_length = xtr.Value; } else if (xtr.Name == "P_ARTICLE_ERPNR") { is_other = false; record_code = xtr.Value; } else if (xtr.Name == "P_ARTICLEREF_COUNT") { record_num = xtr.Value; } else if (xtr.Name == "P_ARTICLE_TYPENR") { record_type = xtr.Value; } for (int step = 0; step < PropertyNoum; step++) { if (xtr.Name == TCTool.BomPropertyKey[step].ToString()) { //if (xtr.Name.Equals("P_ARTICLE_ERPNR") && record_code_idx == -1) //{ // record_code_idx = step; //} //if (xtr.Name.Equals("P_ARTICLEREF_COUNT") && record_num_idx == -1) //{ // record_num_idx = step; //} is_other = false; if (xtr.Value.Contains("@") && xtr.Value.Contains(";")) { string[] arrStr = xtr.Value.Split(';'); int arrLen = arrStr.Length; for (int cnt = 0; cnt < arrLen; cnt++) { if (arrStr[cnt].StartsWith("zh_CN") || arrStr[cnt].StartsWith("??")) { propertyValue[step] = arrStr[cnt].Split('@')[1]; break; } } } else { propertyValue[step] = xtr.Value; } break; } } } if (element_name == "device") //此处过滤掉一些组合设备的部件 { if (device_name.IndexOf(name_f) != -1) { is_dos = true; } else { is_dos = false; } } else if (element_name == "part") { if (is_dos) { continue; } if (TCTool.lineCount != "TRUE") { if (is_wire) { continue; } } else { if (is_wire) { if (!string.IsNullOrEmpty(wire_length) && !string.IsNullOrEmpty(record_num)) { double dwire_length = Convert.ToDouble(wire_length); if (dwire_length != 0.0) { double drecord_num = Convert.ToDouble(record_num); drecord_num = dwire_length * drecord_num; record_num = drecord_num.ToString(); propertyValue[record_num_idx] = record_num; } } } } if (record_num.Equals("0")) { continue; } if (is_other) { name_f = device_name; } ListViewItem lvi = new ListViewItem(); lvi.UseItemStyleForSubItems = false; if (record_code.Equals("")) { for (int i = 0; i < TCTool.P_ARTICLE_TYPENRs.Count; i++) { if (record_type.Equals(TCTool.P_ARTICLE_TYPENRs[i].ToString(), StringComparison.OrdinalIgnoreCase)) { record_code = TCTool.P_ARTICLE_ERPNRs[i].ToString(); propertyValue[record_code_idx] = record_code; break; } } } if (is_P_ARTICLE_DISCONTINUED) { lvi.ForeColor = Color.Red; hasError = true; lvi.Text = "停产部件"; } else { if (record_code.Equals("")) { lvi.ForeColor = Color.Red; hasError = true; lvi.Text = "TC不存在2"; } else { int findItem = TCTool.checkItem(record_code); if (findItem == 0) { lvi.ForeColor = Color.Red; hasError = true; lvi.Text = "TC不存在3"; } else { lvi.Text = "TC存在"; } } } for (int step = 0; step < PropertyNoum; step++) { if (propertyValue[step] == null) { lvi.SubItems.Add(""); } else { lvi.SubItems.Add(propertyValue[step]).ForeColor=Color.Black; } } this.ComListView.Items.Add(lvi); } } xtr.Close(); this.ComListView.EndUpdate(); //结束数据处理,UI界面一次性绘制。 } catch (XmlException ex) { MessageBox.Show(ex.Message); } return hasError; } private void generateListButton_Click(object sender, EventArgs e) { Progress progress = new Progress("EnhancedProgress"); progress.SetActionText("正在生成excel..."); progress.SetTitle("生成申请清单"); progress.BeginPart(100.0, "进度"); progress.ShowImmediately(); progress.SetAllowCancel(true); Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); string nowtime = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss"); string fileName = Environment.GetEnvironmentVariable("temp") + "\\申请清单" + nowtime + ".xlsx"; if (File.Exists(fileName)) { File.Delete(fileName); } excel.Visible = false; excel.Application.Workbooks.Add(true); Workbook wb = (Workbook)excel.ActiveWorkbook; Worksheet st = (Worksheet)wb.ActiveSheet; int col = 1; foreach (string str in TCTool.BomTitleName) { st.Cells[1, col] = str; col++; } int row = 2; for (int i = 0; i < ComListView.Items.Count; i++) { if (ComListView.Items[i].Text == "TC存在") { continue; } for (int j = 0; j < col - 1; j++) { st.Cells[row, j + 1] = ComListView.Items[i].SubItems[j + 1].Text; } row++; } //保存excel文件 wb.SaveCopyAs(fileName); //关闭文件 wb.Close(false, Type.Missing, Type.Missing); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); System.GC.Collect(); progress.EndPart(true); GeneralListForm generalListForm = new GeneralListForm(); generalListForm.labelName = "申请清单生成完成,请查看!"; generalListForm.xlsPath = fileName; generalListForm.ShowDialog(); } private void ComListView_ColumnClick(object sender, ColumnClickEventArgs e) { ComListView.ListViewItemSorter = new ListViewItemComparer(e.Column); ComListView.Sort(); } } public class ListViewItemComparer : IComparer { private int col; public ListViewItemComparer(int col) { this.col = col; } public int Compare(object x, object y) { int returnVal = -1; returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text); return returnVal; } } }