83 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Check.Main.Camera;
 | |
| using NPOI.SS.UserModel;
 | |
| using NPOI.XSSF.UserModel;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.IO;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.Threading.Tasks;
 | |
| using System.Windows.Forms;
 | |
| 
 | |
| namespace Check.Main.Common
 | |
| {
 | |
|     public static class StatisticsExporter
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// 将给定的统计数据导出到指定的Excel文件路径。
 | |
|         /// </summary>
 | |
|         /// <param name="data">要导出的统计数据对象。</param>
 | |
|         /// <param name="customFileName">自定义的文件名部分(如 "Reset" 或 "Shutdown")。</param>
 | |
|         public static void ExportToExcel(StatisticsData data, string customFileName)
 | |
|         {
 | |
|             if (data.TotalCount == 0) return; // 如果没有数据,则不导出
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 string directory = Path.Combine(Application.StartupPath, "Statistics");
 | |
|                 Directory.CreateDirectory(directory); // 确保文件夹存在
 | |
| 
 | |
|                 string fileName = $"Statistics_{customFileName}_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}.xlsx";
 | |
|                 string filePath = Path.Combine(directory, fileName);
 | |
| 
 | |
|                 // 创建工作簿和工作表
 | |
|                 IWorkbook workbook = new XSSFWorkbook();
 | |
|                 ISheet sheet = workbook.CreateSheet("生产统计");
 | |
| 
 | |
|                 // --- 创建样式 (可选,但能让表格更好看) ---
 | |
|                 IFont boldFont = workbook.CreateFont();
 | |
|                 boldFont.IsBold = true;
 | |
|                 ICellStyle headerStyle = workbook.CreateCellStyle();
 | |
|                 headerStyle.SetFont(boldFont);
 | |
| 
 | |
|                 // --- 创建表头 ---
 | |
|                 IRow headerRow = sheet.CreateRow(0);
 | |
|                 headerRow.CreateCell(0).SetCellValue("项目");
 | |
|                 headerRow.CreateCell(1).SetCellValue("数值");
 | |
|                 headerRow.GetCell(0).CellStyle = headerStyle;
 | |
|                 headerRow.GetCell(1).CellStyle = headerStyle;
 | |
| 
 | |
|                 // --- 填充数据 ---
 | |
|                 sheet.CreateRow(1).CreateCell(0).SetCellValue("良品数 (OK)");
 | |
|                 sheet.GetRow(1).CreateCell(1).SetCellValue(data.GoodCount);
 | |
| 
 | |
|                 sheet.CreateRow(2).CreateCell(0).SetCellValue("不良品数 (NG)");
 | |
|                 sheet.GetRow(2).CreateCell(1).SetCellValue(data.NgCount);
 | |
| 
 | |
|                 sheet.CreateRow(3).CreateCell(0).SetCellValue("产品总数");
 | |
|                 sheet.GetRow(3).CreateCell(1).SetCellValue(data.TotalCount);
 | |
| 
 | |
|                 sheet.CreateRow(4).CreateCell(0).SetCellValue("良率 (Yield)");
 | |
|                 sheet.GetRow(4).CreateCell(1).SetCellValue(data.YieldRate.ToString("P2"));
 | |
| 
 | |
|                 // 自动调整列宽
 | |
|                 sheet.AutoSizeColumn(0);
 | |
|                 sheet.AutoSizeColumn(1);
 | |
| 
 | |
|                 // --- 写入文件 ---
 | |
|                 using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
 | |
|                 {
 | |
|                     workbook.Write(fs);
 | |
|                 }
 | |
| 
 | |
|                 ThreadSafeLogger.Log($"统计数据已成功导出到: {filePath}");
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 ThreadSafeLogger.Log($"[ERROR] 导出统计数据失败: {ex.Message}");
 | |
|                 MessageBox.Show($"导出统计数据失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 |