|
@@ -212,9 +212,146 @@ namespace UAS_MES_Tools
|
|
|
BoxNo.Enabled = true;
|
|
BoxNo.Enabled = true;
|
|
|
BoxNo.Focus();
|
|
BoxNo.Focus();
|
|
|
BoxNo.SelectAll();
|
|
BoxNo.SelectAll();
|
|
|
-
|
|
|
|
|
//BoxNo.Text = "";
|
|
//BoxNo.Text = "";
|
|
|
//Datas.Rows.Clear();
|
|
//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);
|
|
QDquery_Click(null, null);
|