From abb13c805dc6e19d76ce12fce0c58d3107e4a530 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 18 Dec 2020 13:51:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- U8AppMain/App.config | 6 +- U8AppMain/Bom/ComponentEnity.cs | 2 +- U8AppMain/Bom/ComponentOptEnity.cs | 1 + U8AppMain/Bom/Version.cs | 2 +- U8AppMain/ExcelHelper.cs | 90 ++++++++++++++++++++++ U8AppMain/FrmMain.Designer.cs | 58 +++++++++++--- U8AppMain/FrmMain.cs | 89 +++++++++++++++++++-- U8AppMain/HttpHelper.cs | 27 +++++++ U8AppMain/Program.cs | 2 +- U8AppMain/Properties/Resources.Designer.cs | 55 ++++++------- U8AppMain/Properties/Settings.Designer.cs | 29 ++++--- U8AppMain/U8AppMain.csproj | 24 +++++- U8AppMain/packages.config | 3 + 13 files changed, 316 insertions(+), 72 deletions(-) create mode 100644 U8AppMain/ExcelHelper.cs create mode 100644 U8AppMain/HttpHelper.cs diff --git a/U8AppMain/App.config b/U8AppMain/App.config index 8e15646..8227adb 100644 --- a/U8AppMain/App.config +++ b/U8AppMain/App.config @@ -1,6 +1,6 @@ - + - + - \ No newline at end of file + diff --git a/U8AppMain/Bom/ComponentEnity.cs b/U8AppMain/Bom/ComponentEnity.cs index 7625f25..566b4a3 100644 --- a/U8AppMain/Bom/ComponentEnity.cs +++ b/U8AppMain/Bom/ComponentEnity.cs @@ -13,7 +13,7 @@ namespace U8AppMain.Bom public int BomId { get; set; } - public int SortSeq { get; set; } + public string SortSeq { get; set; } public string OpSeq { get; set; } public string InvCode { get; set; } diff --git a/U8AppMain/Bom/ComponentOptEnity.cs b/U8AppMain/Bom/ComponentOptEnity.cs index 789f2df..a35b395 100644 --- a/U8AppMain/Bom/ComponentOptEnity.cs +++ b/U8AppMain/Bom/ComponentOptEnity.cs @@ -8,6 +8,7 @@ namespace U8AppMain.Bom { public class ComponentOptEnity { + public string WIPType { get; set; } public int OptionsId { get; set; } public int Offset { get; set; } diff --git a/U8AppMain/Bom/Version.cs b/U8AppMain/Bom/Version.cs index 0c12190..4356e2d 100644 --- a/U8AppMain/Bom/Version.cs +++ b/U8AppMain/Bom/Version.cs @@ -9,7 +9,7 @@ namespace U8AppMain.Bom public class VersionEnity { public int BomId { get; set; } = 0; - public int BomType { get; set; } + public string BomType { get; set; } public int Version { get; set; } public String VersionDesc { get; set; } public DateTime VersionEffDate { get; set; } = DateTime.Now; diff --git a/U8AppMain/ExcelHelper.cs b/U8AppMain/ExcelHelper.cs new file mode 100644 index 0000000..c698964 --- /dev/null +++ b/U8AppMain/ExcelHelper.cs @@ -0,0 +1,90 @@ +using NPOI.SS.UserModel; +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace U8AppMain +{ + public static class ExcelHelper + { + public static DataTable ExcelToDataTable(Stream steam, string sheetName = null, Dictionary fieldName = null, + bool isFirstRowColumn = true) + { + DataTable data = new DataTable(); + try + { + + var workbook = WorkbookFactory.Create(steam); + ISheet sheet; + if (sheetName != null) + { + sheet = workbook.GetSheet(sheetName); + if (sheet == null) sheet = workbook.GetSheetAt(0); + } + else + { + sheet = workbook.GetSheetAt(0); + } + if (sheet != null) + { + IRow firstRow = sheet.GetRow(0); + int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数 + + int startRow; + if (isFirstRowColumn) + { + for (int i = firstRow.FirstCellNum; i < cellCount; ++i) + { + ICell cell = firstRow.GetCell(i); + if (cell != null) + { + string cellValue = cell.StringCellValue.Trim(); + if (fieldName != null) + { + if (fieldName.ContainsKey(cellValue)) + { + cellValue = fieldName[cellValue]; + } + } + if (cellValue != null) + { + DataColumn column = new DataColumn(cellValue); + data.Columns.Add(column); + } + } + } + startRow = sheet.FirstRowNum + 1; + } + else + { + startRow = sheet.FirstRowNum; + } + //最后一列的标号 + int rowCount = sheet.LastRowNum; + for (int i = startRow; i <= rowCount; ++i) + { + IRow row = sheet.GetRow(i); + if (row == null) continue; //没有数据的行默认是null        + DataRow dataRow = data.NewRow(); + for (int j = row.FirstCellNum; j < cellCount; ++j) + { + if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null + dataRow[j] = row.GetCell(j).ToString().Trim(); + } + data.Rows.Add(dataRow); + } + } + return data; + } + catch (Exception ex) + { + + throw ex; + } + } + } +} diff --git a/U8AppMain/FrmMain.Designer.cs b/U8AppMain/FrmMain.Designer.cs index a00530d..f52d3d1 100644 --- a/U8AppMain/FrmMain.Designer.cs +++ b/U8AppMain/FrmMain.Designer.cs @@ -30,13 +30,18 @@ namespace U8AppMain private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); - this.panel2 = new System.Windows.Forms.Panel(); this.BtnBomImport = new System.Windows.Forms.Button(); + this.panel2 = new System.Windows.Forms.Panel(); + this.kryptonDataGridView1 = new ComponentFactory.Krypton.Toolkit.KryptonDataGridView(); + this.BtnImportExcel = new System.Windows.Forms.Button(); this.panel1.SuspendLayout(); + this.panel2.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.kryptonDataGridView1)).BeginInit(); this.SuspendLayout(); // // panel1 // + this.panel1.Controls.Add(this.BtnImportExcel); this.panel1.Controls.Add(this.BtnBomImport); this.panel1.Dock = System.Windows.Forms.DockStyle.Top; this.panel1.Location = new System.Drawing.Point(0, 0); @@ -44,17 +49,9 @@ namespace U8AppMain this.panel1.Size = new System.Drawing.Size(800, 42); this.panel1.TabIndex = 0; // - // panel2 - // - this.panel2.Dock = System.Windows.Forms.DockStyle.Fill; - this.panel2.Location = new System.Drawing.Point(0, 42); - this.panel2.Name = "panel2"; - this.panel2.Size = new System.Drawing.Size(800, 408); - this.panel2.TabIndex = 1; - // // BtnBomImport // - this.BtnBomImport.Location = new System.Drawing.Point(12, 12); + this.BtnBomImport.Location = new System.Drawing.Point(108, 12); this.BtnBomImport.Name = "BtnBomImport"; this.BtnBomImport.Size = new System.Drawing.Size(75, 23); this.BtnBomImport.TabIndex = 0; @@ -62,16 +59,51 @@ namespace U8AppMain this.BtnBomImport.UseVisualStyleBackColor = true; this.BtnBomImport.Click += new System.EventHandler(this.BtnBomImport_Click); // - // Form1 + // panel2 + // + this.panel2.Controls.Add(this.kryptonDataGridView1); + this.panel2.Dock = System.Windows.Forms.DockStyle.Fill; + this.panel2.Location = new System.Drawing.Point(0, 42); + this.panel2.Name = "panel2"; + this.panel2.Size = new System.Drawing.Size(800, 408); + this.panel2.TabIndex = 1; + // + // kryptonDataGridView1 + // + this.kryptonDataGridView1.AllowUserToAddRows = false; + this.kryptonDataGridView1.AllowUserToDeleteRows = false; + this.kryptonDataGridView1.AllowUserToResizeRows = false; + this.kryptonDataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.kryptonDataGridView1.Dock = System.Windows.Forms.DockStyle.Fill; + this.kryptonDataGridView1.Location = new System.Drawing.Point(0, 0); + this.kryptonDataGridView1.Name = "kryptonDataGridView1"; + this.kryptonDataGridView1.ReadOnly = true; + this.kryptonDataGridView1.RowTemplate.Height = 23; + this.kryptonDataGridView1.Size = new System.Drawing.Size(800, 408); + this.kryptonDataGridView1.TabIndex = 0; + // + // BtnImportExcel + // + this.BtnImportExcel.Location = new System.Drawing.Point(12, 12); + this.BtnImportExcel.Name = "BtnImportExcel"; + this.BtnImportExcel.Size = new System.Drawing.Size(75, 23); + this.BtnImportExcel.TabIndex = 1; + this.BtnImportExcel.Text = "读取Exel"; + this.BtnImportExcel.UseVisualStyleBackColor = true; + this.BtnImportExcel.Click += new System.EventHandler(this.BtnImportExcel_Click); + // + // FrmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.panel2); this.Controls.Add(this.panel1); - this.Name = "Form1"; + this.Name = "FrmMain"; this.Text = "Form1"; this.panel1.ResumeLayout(false); + this.panel2.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.kryptonDataGridView1)).EndInit(); this.ResumeLayout(false); } @@ -81,6 +113,8 @@ namespace U8AppMain private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Button BtnBomImport; private System.Windows.Forms.Panel panel2; + private ComponentFactory.Krypton.Toolkit.KryptonDataGridView kryptonDataGridView1; + private System.Windows.Forms.Button BtnImportExcel; } } diff --git a/U8AppMain/FrmMain.cs b/U8AppMain/FrmMain.cs index 165573c..d6169b0 100644 --- a/U8AppMain/FrmMain.cs +++ b/U8AppMain/FrmMain.cs @@ -4,10 +4,12 @@ using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Serialization; +using System.IO; namespace U8AppMain { public partial class FrmMain : Form @@ -17,13 +19,88 @@ namespace U8AppMain InitializeComponent(); } - private void BtnBomImport_Click(object sender, EventArgs e) + private DataTable dataTable; + private async void BtnBomImport_Click(object sender, EventArgs e) { - var bom = new Bom.ufinterface(); - bom.Bom.ComponentBodies.Add(new Bom.ComponentBody()); - bom.Bom.ComponentBodies.Add(new Bom.ComponentBody()); - - MessageBox.Show(XmlHelper.XmlSerialize(bom, Encoding.UTF8)); + // 判断是否是BOM导入的格式 + //母件编码 BOM类别 版本号 版本说明 版本生效日 替代标识 替代说明 母件损耗率 子件编码 行号 基本用量 - 分子 基础数量 - 分母 子件损耗率 供应类型 创建日期 创建人 + var colums = new Dictionary(); + colums.Add("母件编码", "InvCode"); + colums.Add("BOM类别", "BomType"); + colums.Add("版本号", "Version"); + colums.Add("版本说明", "VersionDesc"); + colums.Add("版本生效日", "版本号"); + colums.Add("替代标识", "版本号"); + colums.Add("替代说明", "版本号"); + colums.Add("母件损耗率", "ParentScrap"); + colums.Add("子件编码", "InvCode"); + colums.Add("行号", "SortSeq"); + colums.Add("基本用量分子", "BaseQtyN"); + colums.Add("基础数量分母", "BaseQtyD"); + colums.Add("子件损耗率", "CompScrap"); + colums.Add("供应类型", "WIPType"); + colums.Add("创建日期", "VersionEffDate"); + + + var bomLookup = new Dictionary(); + + + foreach (DataRow row in this.dataTable.Rows) + { + var bom = new Bom.BomEnity(); + var comp = new Bom.ComponentBody(); + + bom.Version.Version = Convert.ToInt32(row["版本号"]); + bom.Version.VersionDesc = row.Field("版本说明"); + bom.Parent.InvCode = row.Field("母件编码"); + bom.Version.VersionEffDate = DateTime.Parse(row.Field("版本生效日")); + bom.Version.BomType = row.Field("BOM类别"); + bom.Parent.ParentScrap = decimal.Parse(row["母件损耗率"].ToString()); + var strKey = $"{bom.Parent.InvCode}-{bom.Version.Version}"; + if (bomLookup.ContainsKey(strKey)) + { + bom = bomLookup[strKey]; + } + else + { + bomLookup.Add(strKey, bom); + } + comp.Component.InvCode = row.Field("子件编码"); + comp.Component.SortSeq = row.Field("行号"); + comp.Component.BaseQtyN = decimal.Parse(row["基本用量分子"].ToString()); + comp.Component.BaseQtyD = decimal.Parse(row["基础数量分母"].ToString()); + comp.Component.CompScrap = decimal.Parse(row["子件损耗率"].ToString()); + comp.ComponentOpt.WIPType = row["供应类型"].ToString(); + + bom.ComponentBodies.Add(comp); + } + + + + //colums.Add("创建人", "版本号"); + + + + //var bom = new Bom.ufinterface(); + //bom.Bom.ComponentBodies.Add(new Bom.ComponentBody()); + //bom.Bom.ComponentBodies.Add(new Bom.ComponentBody()); + var strbody = XmlHelper.XmlSerialize(bomLookup.Values.ToList(), Encoding.UTF8); + var response = await HttpHelper.Post("", strbody); + MessageBox.Show(response); + } + + private void BtnImportExcel_Click(object sender, EventArgs e) + { + var opentFileDlg = new OpenFileDialog(); + opentFileDlg.Filter = "EXCEL2003|*.xls|EXCLE2007|*.xlsx"; + + if (opentFileDlg.ShowDialog() == DialogResult.OK) + { + var file = new FileStream(opentFileDlg.FileName, FileMode.Open); + dataTable = ExcelHelper.ExcelToDataTable(file); + this.kryptonDataGridView1.DataSource = dataTable; + } + return; } } } diff --git a/U8AppMain/HttpHelper.cs b/U8AppMain/HttpHelper.cs new file mode 100644 index 0000000..a64de3b --- /dev/null +++ b/U8AppMain/HttpHelper.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace U8AppMain +{ + public static class HttpHelper + { + public static Task Post(string url, string strBody) + { + strBody = strBody ?? ""; + using (HttpClient client = new HttpClient()) + { + var httpContent = new StringContent(strBody, Encoding.UTF8); + httpContent.Headers.ContentType.MediaType = "application/xml"; + httpContent.Headers.ContentType.CharSet = "utf-8"; + HttpResponseMessage response = client.PostAsync(url, httpContent).Result; + if (httpContent != null) + httpContent.Dispose(); + return response.Content.ReadAsStringAsync(); + } + } + } +} diff --git a/U8AppMain/Program.cs b/U8AppMain/Program.cs index f670dbd..6c4d119 100644 --- a/U8AppMain/Program.cs +++ b/U8AppMain/Program.cs @@ -6,7 +6,7 @@ using System.Windows.Forms; namespace U8AppMain { - static class Program + static class Program { /// /// 应用程序的主入口点。 diff --git a/U8AppMain/Properties/Resources.Designer.cs b/U8AppMain/Properties/Resources.Designer.cs index 674f0e6..7f8d09e 100644 --- a/U8AppMain/Properties/Resources.Designer.cs +++ b/U8AppMain/Properties/Resources.Designer.cs @@ -1,68 +1,61 @@ //------------------------------------------------------------------------------ // // 此代码由工具生成。 -// 运行时版本: 4.0.30319.42000 +// 运行时版本:4.0.30319.42000 // -// 对此文件的更改可能导致不正确的行为,如果 -// 重新生成代码,则所做更改将丢失。 +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 // //------------------------------------------------------------------------------ - -namespace U8AppMain.Properties -{ +namespace U8AppMain.Properties { + using System; + + /// - /// 强类型资源类,用于查找本地化字符串等。 + /// 一个强类型的资源类,用于查找本地化的字符串等。 /// // 此类是由 StronglyTypedResourceBuilder // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 - // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen // (以 /str 作为命令选项),或重新生成 VS 项目。 - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources - { - + internal class Resources { + private static global::System.Resources.ResourceManager resourceMan; - + private static global::System.Globalization.CultureInfo resourceCulture; - + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() - { + internal Resources() { } - + /// - /// 返回此类使用的缓存 ResourceManager 实例。 + /// 返回此类使用的缓存的 ResourceManager 实例。 /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager - { - get - { - if ((resourceMan == null)) - { + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("U8AppMain.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } - + /// /// 重写当前线程的 CurrentUICulture 属性,对 /// 使用此强类型资源类的所有资源查找执行重写。 /// [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture - { - get - { + internal static global::System.Globalization.CultureInfo Culture { + get { return resourceCulture; } - set - { + set { resourceCulture = value; } } diff --git a/U8AppMain/Properties/Settings.Designer.cs b/U8AppMain/Properties/Settings.Designer.cs index bd7bf7a..f24a371 100644 --- a/U8AppMain/Properties/Settings.Designer.cs +++ b/U8AppMain/Properties/Settings.Designer.cs @@ -1,27 +1,24 @@ //------------------------------------------------------------------------------ // -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 +// 此代码由工具生成。 +// 运行时版本:4.0.30319.42000 // -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 // //------------------------------------------------------------------------------ - -namespace U8AppMain.Properties -{ +namespace U8AppMain.Properties { + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase - { - + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.8.1.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default - { - get - { + + public static Settings Default { + get { return defaultInstance; } } diff --git a/U8AppMain/U8AppMain.csproj b/U8AppMain/U8AppMain.csproj index 3475ae7..598b998 100644 --- a/U8AppMain/U8AppMain.csproj +++ b/U8AppMain/U8AppMain.csproj @@ -8,9 +8,10 @@ WinExe U8AppMain U8AppMain - v4.5 + v4.5.2 512 true + AnyCPU @@ -32,6 +33,9 @@ 4 + + ..\packages\Portable.BouncyCastle.1.8.6\lib\net40\BouncyCastle.Crypto.dll + ..\packages\Krypton.Components.Suite.4.5.9\lib\net20\ComponentFactory.Krypton.Design.dll @@ -50,6 +54,21 @@ ..\packages\Krypton.Components.Suite.4.5.9\lib\net20\ComponentFactory.Krypton.Workspace.dll + + ..\packages\SharpZipLib.1.2.0\lib\net45\ICSharpCode.SharpZipLib.dll + + + ..\packages\NPOI.2.5.2\lib\net45\NPOI.dll + + + ..\packages\NPOI.2.5.2\lib\net45\NPOI.OOXML.dll + + + ..\packages\NPOI.2.5.2\lib\net45\NPOI.OpenXml4Net.dll + + + ..\packages\NPOI.2.5.2\lib\net45\NPOI.OpenXmlFormats.dll + @@ -71,12 +90,14 @@ + Form FrmMain.cs + @@ -96,6 +117,7 @@ True Resources.resx + True diff --git a/U8AppMain/packages.config b/U8AppMain/packages.config index 78bbd27..ad70d18 100644 --- a/U8AppMain/packages.config +++ b/U8AppMain/packages.config @@ -1,4 +1,7 @@  + + + \ No newline at end of file