diff --git a/Resource/ResultReport.xlsx b/Resource/ResultReport.xlsx new file mode 100644 index 0000000..e477197 Binary files /dev/null and b/Resource/ResultReport.xlsx differ diff --git a/SparkClient.csproj b/SparkClient.csproj index d323125..1319731 100644 --- a/SparkClient.csproj +++ b/SparkClient.csproj @@ -35,6 +35,7 @@ + @@ -44,7 +45,6 @@ - @@ -73,9 +73,6 @@ - - Always - Always @@ -224,6 +221,9 @@ + + Always + @@ -275,9 +275,16 @@ + + C:\Windows\assembly\GAC_MSIL\office\15.0.0.0__71e9bce111e9429c\OFFICE.DLL + bin\Debug\net8.0-windows\SparkDotNetCore.dll + + + + diff --git a/ViewModel/Grading/GradingResultVM.cs b/ViewModel/Grading/GradingResultVM.cs index afac558..5a907b2 100644 --- a/ViewModel/Grading/GradingResultVM.cs +++ b/ViewModel/Grading/GradingResultVM.cs @@ -29,11 +29,15 @@ using FontFamily = System.Drawing.FontFamily; using FontStyle = System.Drawing.FontStyle; using NPOI.SS.Formula.Functions; using System.Windows.Media; +using HandyControl.Tools.Extension; +using NPOI.SS.Util; using Brush = System.Drawing.Brush; using Brushes = System.Drawing.Brushes; using SparkClient.Model.Attributes; using SparkClient.Model.Common; using PaperSize = System.Drawing.Printing.PaperSize; +using SharpDX.Direct2D1.Effects; + namespace SparkClient.ViewModel.Grading; /// @@ -284,7 +288,10 @@ public class GradingResultVM : BaseViewModel using (var writer = new StreamWriter(filePath,true)) { Measurements info = algorithmResult.measurements; - string line = $"{FormatDouble_A(info.M1)}(mm)," + + string line = + $"{DiamondCode}," + + $"{Wight}(ct)," + + $"{FormatDouble_A(info.M1)}(mm)," + $"{FormatDouble_A(info.M2)}(mm)," + $"{FormatDouble_A(info.M3)}(mm)," + $"{FormatDouble_P(info.TABLE)}%," + @@ -302,6 +309,35 @@ public class GradingResultVM : BaseViewModel writer.WriteLine(line); } } + else + { + File.Create(filePath).Close(); + string header = "Diamond Code,Wight,Measurement1, Measurement2, Measurement3, Table, Crown Height,Pavilion Depth, Total Depth,Crown Angle, Pavilion Angle,Culet Size, Girdle Percent,Girdle Name, POL or Pol/ Sym,SYM,CUT - PROP"; + Measurements info = algorithmResult.measurements; + string line = + $"{DiamondCode}," + + $"{Wight}(ct)," + + $"{FormatDouble_A(info.M1)}(mm)," + + $"{FormatDouble_A(info.M2)}(mm)," + + $"{FormatDouble_A(info.M3)}(mm)," + + $"{FormatDouble_P(info.TABLE)}%," + + $"{FormatDouble_P(info.CROWN_HEIGHT)}%," + + $"{FormatDouble_P(info.PAV_DEPTH)}%," + + $"{FormatDouble_P(info.TOTAL_DEPTH)}%," + + $"{FormatDouble_A(info.CROWN_ANGLE)}°," + + $"{FormatDouble_A(info.PAV_ANGLE)}°," + + $"{FormatDouble_A(info.CULET_SIZE)}(mm)," + + $"{FormatDouble_P(info.GIRDLE)}%," + + $"{calGirdleName(info)}," + + $"," + + $"," + + $"{GetGradeEnName(GetGradeOrder(CutLevelTotal))}"; + using (var writer = new StreamWriter(filePath, true)) + { + writer.WriteLine(header); + writer.WriteLine(line); + } + } } private void saveTempDS(string selValue) @@ -1586,7 +1622,7 @@ public class GradingResultVM : BaseViewModel [Log] public void ChangeSym(object norm) { - decimal? order = this.DtResults.Select(x => x.SymLevel).Max(); + decimal? order = this.DtResults.Where(x => x.TestItemId != "TWIST").Select(x => x.SymLevel).Max(); if (order.HasValue) { totalSymGrade = order.Value; @@ -1939,90 +1975,139 @@ public class GradingResultVM : BaseViewModel // 执行热敏打印的方法 [Log] private void ExecuteThermalPrint(object parameter) - { - try + { + MessageBox messageBox = new MessageBox(); + MessageBoxResult boxResult = messageBox.ShowAsk(MultilingualHelper.getString("YesOrNo")); + if (boxResult != MessageBoxResult.OK) { - // 创建打印文档 - PrintDocument printDocument = new PrintDocument(); - //printDocument.PrintPage += PrintPageHandler; - // 订阅 PrintPage 事件 - printDocument.PrintPage += PreparePrintData; - - // 创建自定义打印预览窗体 - CustomPrintPreviewForm customPreviewForm = new CustomPrintPreviewForm(printDocument); - - // 显示自定义打印预览窗体 - - MessageBox messageBox = new MessageBox(); - MessageBoxResult boxResult = messageBox.ShowAsk(MultilingualHelper.getString("YesOrNo")); - if (boxResult == MessageBoxResult.OK) - { - // 打开串口 - // if (!_serialPort.IsOpen) - // { - // _serialPort.Open(); - // } - - // 将绘制内容转换为字节数组 - // MemoryStream memoryStream = new MemoryStream(); - // using (Bitmap bitmap = new Bitmap((int)printDocument.DefaultPageSettings.PrintableArea.Width, (int)printDocument.DefaultPageSettings.PrintableArea.Height)) - // { - // using (var graphics = Graphics.FromImage(bitmap)) - // { - // // 计算可打印区域的 Rectangle - // var margins = new Margins(0, 0, 0, 0); - // var printableArea = new System.Drawing.Rectangle(margins.Left, margins.Top, - // (int)printDocument.DefaultPageSettings.PrintableArea.Width - margins.Left - margins.Right, - // (int)printDocument.DefaultPageSettings.PrintableArea.Height - margins.Top - margins.Bottom); - // - // // 调用 PreparePrintData 方法绘制内容到 Graphics 对象 - // // 注意这里的第三个参数应该是 Rectangle 类型 - // PreparePrintData(printDocument, new PrintPageEventArgs(graphics, printableArea, printableArea, new PageSettings())); - // } - // // 将 Bitmap 保存到 MemoryStream 中 - // bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); - // } - // byte[] dataToSend = memoryStream.ToArray(); - string? printName = ConfigurationManager.AppSettings["PrintName"]; - string? printWidth = ConfigurationManager.AppSettings["PrintWidth"]; - string? printHeight = ConfigurationManager.AppSettings["PrintHeight"]; - - int pageWidth = 400; - int pageHeight = 300; - if (string.IsNullOrWhiteSpace(printName) || string.IsNullOrWhiteSpace(printWidth) || - string.IsNullOrWhiteSpace(printHeight) ) - { - Logger.Info("缺少打印配置。"); - throw new Exception("The printer parameter is invalid"); - } - int.TryParse(printWidth, out pageWidth); - int.TryParse(printHeight, out pageHeight); - - printDocument.PrinterSettings.PrinterName = printName; - printDocument.DefaultPageSettings.PaperSize = new PaperSize("mm", pageWidth, pageHeight); - printDocument.PrintController = new System.Drawing.Printing.StandardPrintController(); - - // DialogResult result = customPreviewForm.ShowDialog(); - printDocument.Print(); - // 发送数据到串口 - //_serialPort.Write(dataToSend, 0, dataToSend.Length); + return; + } - Logger.Info("数据已发送到热敏打印机。"); - } + string tempFilePath = string.Empty; + Microsoft.Office.Interop.Excel.Application excelApp = null; + try + { + // 步骤2:加载模板并填充数据 + tempFilePath = GenerateTempExcelFile(); + + // 步骤3:使用Excel Interop静默打印 + excelApp = new Microsoft.Office.Interop.Excel.Application(); + excelApp.Visible = false; // 不显示Excel窗口 + excelApp.DisplayAlerts = false; // 禁用警告提示 + + Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Open(tempFilePath); + + // 打印配置 + workbook.PrintOut( + Copies: 1, + Preview: false // 禁用打印预览 + ); + + // 关闭工作簿(不保存修改) + workbook.Close(SaveChanges: false); } catch (Exception ex) { - Logger.Error($"热敏打印发生错误: {ex.Message}"); - Growl.Error(MultilingualHelper.getString("DiamodResultPrintError")); + new MessageBox().Show($"打印失败: {ex.Message}"); } finally { - // 关闭串口 - // if (_serialPort.IsOpen) - // { - // _serialPort.Close(); - // } + // 步骤4:清理资源 + if (excelApp != null) + { + excelApp.Quit(); + System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp); + } + + // 删除临时文件 + if (File.Exists(tempFilePath)) + { + File.Delete(tempFilePath); + } } + // try + // { + // // 创建打印文档 + // PrintDocument printDocument = new PrintDocument(); + // //printDocument.PrintPage += PrintPageHandler; + // // 订阅 PrintPage 事件 + // printDocument.PrintPage += PreparePrintData; + // + // // 创建自定义打印预览窗体 + // CustomPrintPreviewForm customPreviewForm = new CustomPrintPreviewForm(printDocument); + // + // // 显示自定义打印预览窗体 + // + // MessageBox messageBox = new MessageBox(); + // MessageBoxResult boxResult = messageBox.ShowAsk(MultilingualHelper.getString("YesOrNo")); + // if (boxResult == MessageBoxResult.OK) + // { + // // 打开串口 + // // if (!_serialPort.IsOpen) + // // { + // // _serialPort.Open(); + // // } + // + // // 将绘制内容转换为字节数组 + // // MemoryStream memoryStream = new MemoryStream(); + // // using (Bitmap bitmap = new Bitmap((int)printDocument.DefaultPageSettings.PrintableArea.Width, (int)printDocument.DefaultPageSettings.PrintableArea.Height)) + // // { + // // using (var graphics = Graphics.FromImage(bitmap)) + // // { + // // // 计算可打印区域的 Rectangle + // // var margins = new Margins(0, 0, 0, 0); + // // var printableArea = new System.Drawing.Rectangle(margins.Left, margins.Top, + // // (int)printDocument.DefaultPageSettings.PrintableArea.Width - margins.Left - margins.Right, + // // (int)printDocument.DefaultPageSettings.PrintableArea.Height - margins.Top - margins.Bottom); + // // + // // // 调用 PreparePrintData 方法绘制内容到 Graphics 对象 + // // // 注意这里的第三个参数应该是 Rectangle 类型 + // // PreparePrintData(printDocument, new PrintPageEventArgs(graphics, printableArea, printableArea, new PageSettings())); + // // } + // // // 将 Bitmap 保存到 MemoryStream 中 + // // bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png); + // // } + // // byte[] dataToSend = memoryStream.ToArray(); + // string? printName = ConfigurationManager.AppSettings["PrintName"]; + // string? printWidth = ConfigurationManager.AppSettings["PrintWidth"]; + // string? printHeight = ConfigurationManager.AppSettings["PrintHeight"]; + // + // int pageWidth = 400; + // int pageHeight = 300; + // if (string.IsNullOrWhiteSpace(printName) || string.IsNullOrWhiteSpace(printWidth) || + // string.IsNullOrWhiteSpace(printHeight) ) + // { + // Logger.Info("缺少打印配置。"); + // throw new Exception("The printer parameter is invalid"); + // } + // int.TryParse(printWidth, out pageWidth); + // int.TryParse(printHeight, out pageHeight); + // + // printDocument.PrinterSettings.PrinterName = printName; + // printDocument.DefaultPageSettings.PaperSize = new PaperSize("mm", pageWidth, pageHeight); + // printDocument.PrintController = new System.Drawing.Printing.StandardPrintController(); + // + // // DialogResult result = customPreviewForm.ShowDialog(); + // printDocument.Print(); + // // 发送数据到串口 + // //_serialPort.Write(dataToSend, 0, dataToSend.Length); + // + // Logger.Info("数据已发送到热敏打印机。"); + // } + // } + // catch (Exception ex) + // { + // Logger.Error($"热敏打印发生错误: {ex.Message}"); + // Growl.Error(MultilingualHelper.getString("DiamodResultPrintError")); + // } + // finally + // { + // // 关闭串口 + // // if (_serialPort.IsOpen) + // // { + // // _serialPort.Close(); + // // } + // } } // 自定义打印预览窗体类 @@ -2238,6 +2323,99 @@ public class GradingResultVM : BaseViewModel } + private string GenerateTempExcelFile() + { + string TemplatePath = @"Resource\ResultReport.xlsx"; + using (FileStream templateStream = new FileStream(TemplatePath, FileMode.Open, FileAccess.Read)) + { + IWorkbook workbook = new XSSFWorkbook(templateStream); + ISheet sheet = workbook.GetSheetAt(0); + + //Row 1 + SetCellValue(sheet, "A1", $"{MultilingualHelper.getString("Organization")}:{Standard}"); + SetCellValue(sheet, "B1", $"{MultilingualHelper.getString("DiamondResultShape")}:{Shape}"); + SetCellValue(sheet, "C1", $"{MultilingualHelper.getString("Code")}:{DiamondCode}"); + SetCellValue(sheet, "E1", $"{MultilingualHelper.getString("Wight")}:{Wight}ct."); + //Row 2 + SetCellValue(sheet, "A2", $"{MultilingualHelper.getString("Parameters")}"); + SetCellValue(sheet, "B2", $"{MultilingualHelper.getString("DiamondResultGridAvgValue")}"); + SetCellValue(sheet, "C2", $"{MultilingualHelper.getString("Interval")}(Min~Max)"); + SetCellValue(sheet, "D2", $"{MultilingualHelper.getString("DiamondResultGridCutLevel")}"); + SetCellValue(sheet, "E2", $"{MultilingualHelper.getString("DiamondResultSymmetryGrade")}"); + //Row 3 + var row3 = getSelectData("DIAMETER"); + SetCellValue(sheet, "A3", $"{GetName("DIAMETER")}(mm)"); + SetCellValue(sheet, "B3", $"{row3.Avg}"); + SetCellValue(sheet, "C3", $"({row3.Min}~{row3.Max})"); + SetCellValue(sheet, "D3", "--"); + SetCellValue(sheet, "E3", $"{row3.SymLevel}"); + //Row 4 + var row4 = getSelectData("TOTAL_DEPTH"); + SetCellValue(sheet, "A4", $"{GetName("TOTAL_DEPTH")}(%)"); + SetCellValue(sheet, "B4", $"{row4.Avg}"); + SetCellValue(sheet, "C4", "--"); + SetCellValue(sheet, "D4", $"{row4.CutLevel}"); + SetCellValue(sheet, "E4", "--"); + // Row 5 + var row5 = getSelectData("TABLE"); + SetCellValue(sheet, "A5", $"{GetName("TABLE")}(%)"); + SetCellValue(sheet, "B5", $"{row5.Avg}"); + SetCellValue(sheet, "C5", $"({row5.Min}~{row5.Max})"); + SetCellValue(sheet, "D5", $"{row5.CutLevel}"); + SetCellValue(sheet, "E5", "--"); + var row6 = getSelectData("CROWN_ANGLE"); + SetCellValue(sheet, "A6", $"{GetName("CROWN_ANGLE")}(°)"); + SetCellValue(sheet, "B6", $"{row6.Avg}"); + SetCellValue(sheet, "C6", $"({row6.Min}~{row6.Max})"); + SetCellValue(sheet, "D6", $"{row6.CutLevel}"); + SetCellValue(sheet, "E6", $"{row6.SymLevel}"); + var row7 = getSelectData("CROWN_HEIGHT"); + SetCellValue(sheet, "A7", $"{GetName("CROWN_HEIGHT")}(%)"); + SetCellValue(sheet, "B7", $"{row7.Avg}"); + SetCellValue(sheet, "C7", $"({row7.Min}~{row7.Max})"); + SetCellValue(sheet, "D7", $"{row7.CutLevel}"); + SetCellValue(sheet, "E7", $"{row7.SymLevel}"); + var row8 = getSelectData("GIRDLE"); + SetCellValue(sheet, "A8", $"{GetName("GIRDLE")}(%)"); + SetCellValue(sheet, "B8", $"{row8.Avg}"); + SetCellValue(sheet, "C8", $"({row8.Min}~{row8.Max})"); + SetCellValue(sheet, "D8", $"{row8.CutLevel}"); + SetCellValue(sheet, "E8", $"{row8.SymLevel}"); + var row9 = getSelectData("PAV_DEPTH"); + SetCellValue(sheet, "A9", $"{GetName("PAV_DEPTH")}(%)"); + SetCellValue(sheet, "B9", $"{row9.Avg}"); + SetCellValue(sheet, "C9", $"({row9.Min}~{row9.Max})"); + SetCellValue(sheet, "D9", $"{row9.CutLevel}"); + SetCellValue(sheet, "E9", $"{row9.SymLevel}"); + //row 10 + //Row 11 + SetCellValue(sheet, "A11", $"{MultilingualHelper.getString("CuttingGrade")}"); + SetCellValue(sheet, "B11", $"{CutLevelTotal}"); + //Row 12 + SetCellValue(sheet, "A12", $"{MultilingualHelper.getString("SymmetryLevel")}"); + SetCellValue(sheet, "B12", $"{SymLevelTotal}"); + SetCellValue(sheet, "C12", $"{MultilingualHelper.getString("DateOfIssue")}: {DateTime.Now:yyyy/M/d}"); + // 生成临时文件路径 + string tempFile = Path.Combine( + Path.GetTempPath(), + $"DiamondReport_{DateTime.Now:yyyyMMddHHmmss}.xlsx"); + + using (FileStream fs = new FileStream(tempFile, FileMode.Create)) + { + workbook.Write(fs); + } + return tempFile; + } + } + + [Log] + private void SetCellValue(ISheet sheet, string cellAddress, object value) + { + var cellRef = new CellReference(cellAddress); + IRow row = sheet.GetRow(cellRef.Row) ?? sheet.CreateRow(cellRef.Row); + ICell cell = row.GetCell(cellRef.Col) ?? row.CreateCell(cellRef.Col); + cell.SetCellValue(value?.ToString() ?? ""); + } [Log] private void PrintPageHandler(object sender, PrintPageEventArgs e) { diff --git a/csv/histroy.csv b/csv/histroy.csv deleted file mode 100644 index e69de29..0000000