Przeglądaj źródła

添加整箱导出

yhluo 1 dzień temu
rodzic
commit
a6109b00bc

+ 138 - 1
UAS_Tools_HY/Main.cs

@@ -212,9 +212,146 @@ namespace UAS_MES_Tools
                         BoxNo.Enabled = true;
                         BoxNo.Focus();
                         BoxNo.SelectAll();
-
                         //BoxNo.Text = "";
                         //Datas.Rows.Clear();
+
+                        Application excelApp = null;
+                        Workbook workbook = null;
+                        Worksheet worksheet = null;
+                        Range range = null;
+                        string tempTemplatePath = null;
+                        try
+                        {
+                            string checkExportModel = Path.Combine(settingRightInput2.Text, BoxNo.Text.Trim() + ".xlsx");
+
+                            if (string.IsNullOrEmpty(settingRightInput2.Text) || !Directory.Exists(settingRightInput2.Text))
+                            {
+                                MessageBox.Show($"输出目录不存在: {settingRightInput2.Text}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                                return;
+                            }
+
+                            byte[] templateBytes = Resources.checkExportModel;
+                            if (templateBytes == null || templateBytes.Length == 0)
+                            {
+                                MessageBox.Show("未能加载模板文件", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                                return;
+                            }
+                            tempTemplatePath = Path.GetTempFileName().Replace(".tmp", ".xlsx");
+                            File.WriteAllBytes(tempTemplatePath, templateBytes);
+
+                            excelApp = new Application();
+                            excelApp.Visible = false;
+                            excelApp.DisplayAlerts = false;
+
+                            workbook = excelApp.Workbooks.Open(tempTemplatePath);
+                            object sheetIndex = 1;
+                            worksheet = (Worksheet)workbook.Worksheets[sheetIndex];
+
+                            dt = ConnectDB.ExecuteSelect($@"select * from g_packing_sncheck where outbox_no = '{BoxNo.Text.Trim()}'");
+                            if (dt.Rows.Count > 0)
+                            {
+                                worksheet.Cells[1, 3] = DateTime.Now.ToString("yyyy-MM-dd");
+                                worksheet.Cells[1, 7] = BoxNo.Text.Trim();
+
+                                int startRow = 3;
+                                int startColumn = 2;
+                                int currInd = 0;
+                                int dataRowsCount = dt.Rows.Count;
+                                foreach (DataRow item in dt.Rows)
+                                {
+                                    worksheet.Cells[startRow + currInd, startColumn - 1] = currInd + 1;
+                                    worksheet.Cells[startRow + currInd, startColumn] = item["SN"].ToString();
+                                    if (currInd > 23)
+                                    {
+                                        currInd = 0;
+                                        startColumn += 2;
+                                    }
+                                    else
+                                    {
+                                        currInd += 1;
+                                    }
+                                }
+
+                                int endRow = startRow + 25;
+                                range = worksheet.Range[worksheet.Cells[startRow, 1], worksheet.Cells[endRow, startColumn]];
+
+                                range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
+                                range.VerticalAlignment = XlVAlign.xlVAlignCenter;
+                            }
+
+                            worksheet.Columns.AutoFit();
+                            workbook.SaveAs(
+                                Filename: checkExportModel,
+                                FileFormat: XlFileFormat.xlOpenXMLWorkbook,
+                                Password: Type.Missing,
+                                WriteResPassword: Type.Missing,
+                                ReadOnlyRecommended: Type.Missing,
+                                CreateBackup: Type.Missing,
+                                AccessMode: XlSaveAsAccessMode.xlExclusive,
+                                ConflictResolution: Type.Missing,
+                                AddToMru: false,
+                                TextCodepage: Type.Missing,
+                                TextVisualLayout: Type.Missing,
+                                Local: Type.Missing
+                            );
+
+                            MessageBox.Show($"Excel文件已成功导出到: {checkExportModel}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
+
+                        }
+                        catch (Exception ex)
+                        {
+                            MessageBox.Show($"生成 Excel 文件时出错: {ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                        }
+                        finally
+                        {
+                            // 正确释放COM对象
+                            if (worksheet != null)
+                            {
+                                try
+                                {
+                                    Marshal.ReleaseComObject(worksheet);
+                                }
+                                catch { }
+                                worksheet = null;
+                            }
+
+                            if (workbook != null)
+                            {
+                                try
+                                {
+                                    workbook.Close(false);
+                                    Marshal.ReleaseComObject(workbook);
+                                }
+                                catch { }
+                                workbook = null;
+                            }
+
+                            if (excelApp != null)
+                            {
+                                try
+                                {
+                                    excelApp.Quit();
+                                    Marshal.ReleaseComObject(excelApp);
+                                }
+                                catch { }
+                                excelApp = null;
+                            }
+
+                            if (!string.IsNullOrEmpty(tempTemplatePath) && File.Exists(tempTemplatePath))
+                            {
+                                try
+                                {
+                                    File.Delete(tempTemplatePath);
+                                }
+                                catch (Exception ex)
+                                {
+
+                                }
+                            }
+
+                            GC.Collect();
+                            GC.WaitForPendingFinalizers();
+                        }
                     }
 
                     QDquery_Click(null, null);

+ 11 - 1
UAS_Tools_HY/Properties/Resources.Designer.cs

@@ -19,7 +19,7 @@ namespace UAS_Tools_HY.Properties {
     // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
     // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
     // (以 /str 作为命令选项),或重新生成 VS 项目。
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "18.0.0.0")]
     [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
     internal class Resources {
@@ -60,6 +60,16 @@ namespace UAS_Tools_HY.Properties {
             }
         }
         
+        /// <summary>
+        ///   查找 System.Byte[] 类型的本地化资源。
+        /// </summary>
+        internal static byte[] checkExportModel {
+            get {
+                object obj = ResourceManager.GetObject("checkExportModel", resourceCulture);
+                return ((byte[])(obj));
+            }
+        }
+        
         /// <summary>
         ///   查找 System.Byte[] 类型的本地化资源。
         /// </summary>

+ 3 - 0
UAS_Tools_HY/Properties/Resources.resx

@@ -118,6 +118,9 @@
     <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </resheader>
   <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+  <data name="checkExportModel" type="System.Resources.ResXFileRef, System.Windows.Forms">
+    <value>..\Resources\checkExportModel.xlsx;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </data>
   <data name="exportModel" type="System.Resources.ResXFileRef, System.Windows.Forms">
     <value>..\Resources\Audio\导出模板.xlsx;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
   </data>

BIN
UAS_Tools_HY/Resources/checkExportModel.xlsx