From a04d135700c8b7fd7816e89b368339f51806fbb9 Mon Sep 17 00:00:00 2001 From: wxt Date: Thu, 5 Feb 2026 17:48:28 +0800 Subject: [PATCH] --- 全自动水压检测仪/Report.cs | 264 ++++++++++++++++++++++++++++++++----- 1 file changed, 232 insertions(+), 32 deletions(-) diff --git a/全自动水压检测仪/Report.cs b/全自动水压检测仪/Report.cs index a995094..006c384 100644 --- a/全自动水压检测仪/Report.cs +++ b/全自动水压检测仪/Report.cs @@ -603,6 +603,129 @@ namespace 全自动水压检测仪 /// /// 导出为Excel文件(简单实现) /// + //private void ExportToExcel(string filePath) + //{ + // using (var package = new ExcelPackage()) + // { + // var worksheet = package.Workbook.Worksheets.Add("测试数据报表"); + + // // 设置默认样式 + // worksheet.Cells.Style.Font.Name = "微软雅黑"; + // worksheet.Cells.Style.Font.Size = 11; + + // int currentRow = 1; + + // // 主标题 + // worksheet.Cells[currentRow, 1, currentRow, 9].Merge = true; + // var titleCell = worksheet.Cells[currentRow, 1]; + // titleCell.Value = "测试数据报表"; + // titleCell.Style.Font.Size = 16; + // titleCell.Style.Font.Bold = true; + // titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + // titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; + + // currentRow++; + + // // 副标题 + // worksheet.Cells[currentRow, 1, currentRow, 9].Merge = true; + // var subTitleCell = worksheet.Cells[currentRow, 1]; + // subTitleCell.Value = $"生成时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss} | 记录数量:{CurrentReport.Count}条"; + // subTitleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + // subTitleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; + + // currentRow += 2; + + // // 表头 + // string[] headers = { "编号", "联络单号", "件号", "刻字号", "数量", "初始压力(PSI)", "开始时间", "结束时间", "结束压力(PSI)", "保压时间(h)", "压差(PSI)", "温度模式", "温度", "标准差值(PSI)", "测试结果", "时间日期" }; + + // for (int i = 0; i < headers.Length; i++) + // { + // var cell = worksheet.Cells[currentRow, i + 1]; + // cell.Value = headers[i]; + // cell.Style.Font.Bold = true; + // cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + // cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; + // cell.Style.Fill.PatternType = ExcelFillStyle.Solid; + // cell.Style.Fill.BackgroundColor.SetColor(Color.LightSkyBlue); + // cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.Black); + + // // 设置列宽 + // worksheet.Column(i + 1).Width = 15; + // } + + // // 设置表头行高 + // worksheet.Row(currentRow).Height = 35; + + // currentRow++; + + // // 数据行 + // foreach (var data in CurrentReport) + // { + // string tempMode = GetTemperatureModeDisplay(data.Type); + + // worksheet.Cells[currentRow, 1].Value = data.Id; + // worksheet.Cells[currentRow, 2].Value = data.lldh ?? ""; + // worksheet.Cells[currentRow, 3].Value = data.jh ?? ""; + + // worksheet.Cells[currentRow, 4].Value = data.kzh ?? ""; + // worksheet.Cells[currentRow, 5].Value = data.quantity; + // worksheet.Cells[currentRow, 6].Value = data.startpressure; + + + + + // worksheet.Cells[currentRow, 7].Value = data.starttime; + // worksheet.Cells[currentRow, 8].Value = data.endtime; + + // worksheet.Cells[currentRow, 9].Value = data.endpressure; + // worksheet.Cells[currentRow, 10].Value = data.dwelltime; + + // worksheet.Cells[currentRow, 11].Value = data.diffpressure; + // worksheet.Cells[currentRow, 12].Value = tempMode; + // worksheet.Cells[currentRow, 13].Value = data.temperature; + // worksheet.Cells[currentRow, 14].Value = data.standarderror; + // worksheet.Cells[currentRow, 15].Value = data.testresult; + + // worksheet.Cells[currentRow, 16].Value = data.CreateTime; + + + // // 设置数据行样式 + // for (int col = 1; col <= 9; col++) + // { + // var cell = worksheet.Cells[currentRow, col]; + // cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + // cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; + // cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.LightGray); + + // // 隔行变色 + // if (currentRow % 2 == 0) + // { + // cell.Style.Fill.PatternType = ExcelFillStyle.Solid; + // cell.Style.Fill.BackgroundColor.SetColor(Color.AliceBlue); + // } + // } + + // // 设置格式 + // worksheet.Cells[currentRow, 4].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss"; + // worksheet.Cells[currentRow, 5].Style.Numberformat.Format = "0.00"; + // worksheet.Cells[currentRow, 6].Style.Numberformat.Format = "0.00"; + // worksheet.Cells[currentRow, 7].Style.Numberformat.Format = "0.00"; + // worksheet.Cells[currentRow, 8].Style.Numberformat.Format = "0.0"; + + // // 设置行高 + // worksheet.Row(currentRow).Height = 25; + + // currentRow++; + // } + + // // 自动调整列宽 + // worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); + + // // 保存 + // package.SaveAs(new FileInfo(filePath)); + // } + //} + private void ExportToExcel(string filePath) { using (var package = new ExcelPackage()) @@ -611,50 +734,77 @@ namespace 全自动水压检测仪 // 设置默认样式 worksheet.Cells.Style.Font.Name = "微软雅黑"; - worksheet.Cells.Style.Font.Size = 11; + worksheet.Cells.Style.Font.Size = 10; + worksheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + worksheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center; int currentRow = 1; // 主标题 - worksheet.Cells[currentRow, 1, currentRow, 9].Merge = true; + worksheet.Cells[currentRow, 1, currentRow, 16].Merge = true; var titleCell = worksheet.Cells[currentRow, 1]; titleCell.Value = "测试数据报表"; titleCell.Style.Font.Size = 16; titleCell.Style.Font.Bold = true; titleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; titleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; + worksheet.Row(currentRow).Height = 30; currentRow++; // 副标题 - worksheet.Cells[currentRow, 1, currentRow, 9].Merge = true; + worksheet.Cells[currentRow, 1, currentRow, 16].Merge = true; var subTitleCell = worksheet.Cells[currentRow, 1]; subTitleCell.Value = $"生成时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss} | 记录数量:{CurrentReport.Count}条"; + subTitleCell.Style.Font.Size = 11; subTitleCell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; subTitleCell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; + worksheet.Row(currentRow).Height = 25; - currentRow += 2; + currentRow += 2; // 增加空行间距 // 表头 - string[] headers = { "编号", "联络单号", "件号", "刻字号", "数量", "初始压力(PSI)", "开始时间", "结束时间", "结束压力(PSI)", "保压时间(h)", "压差(PSI)", "温度模式", "温度", "标准差值(PSI)", "测试结果", "时间日期" }; + string[] headers = { "编号", "联络单号", "件号", "刻字号", "数量", "初始压力(PSI)", "开始时间", "结束时间", "结束压力(PSI)", "保压时间(h)", "压差(PSI)", "温度模式", "温度", "标准差值(PSI)", "测试结果", "创建时间" }; + // 设置表头 for (int i = 0; i < headers.Length; i++) { var cell = worksheet.Cells[currentRow, i + 1]; cell.Value = headers[i]; cell.Style.Font.Bold = true; + cell.Style.Font.Size = 11; cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; cell.Style.Fill.PatternType = ExcelFillStyle.Solid; - cell.Style.Fill.BackgroundColor.SetColor(Color.LightSkyBlue); - cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.Black); + cell.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(91, 155, 213)); // 更专业的蓝色 + cell.Style.Font.Color.SetColor(Color.White); + cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(217, 217, 217)); - // 设置列宽 - worksheet.Column(i + 1).Width = 15; + // 设置列宽(根据内容调整) + int columnWidth = 12; + switch (headers[i]) + { + case "联络单号": + case "开始时间": + case "结束时间": + case "创建时间": + columnWidth = 18; + break; + case "刻字号": + columnWidth = 15; + break; + case "测试结果": + columnWidth = 10; + break; + case "温度模式": + columnWidth = 12; + break; + } + worksheet.Column(i + 1).Width = columnWidth; } // 设置表头行高 - worksheet.Row(currentRow).Height = 35; + worksheet.Row(currentRow).Height = 30; currentRow++; @@ -663,63 +813,113 @@ namespace 全自动水压检测仪 { string tempMode = GetTemperatureModeDisplay(data.Type); + // 数据赋值 worksheet.Cells[currentRow, 1].Value = data.Id; worksheet.Cells[currentRow, 2].Value = data.lldh ?? ""; worksheet.Cells[currentRow, 3].Value = data.jh ?? ""; - worksheet.Cells[currentRow, 4].Value = data.kzh ?? ""; worksheet.Cells[currentRow, 5].Value = data.quantity; worksheet.Cells[currentRow, 6].Value = data.startpressure; + // 修复时间格式 - 使用正确的日期时间格式 + if (data.starttime != null) + { + worksheet.Cells[currentRow, 7].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss"; + worksheet.Cells[currentRow, 7].Value = data.starttime; + } - - - worksheet.Cells[currentRow, 7].Value = data.starttime; - worksheet.Cells[currentRow, 8].Value = data.endtime; + if (data.endtime != null) + { + worksheet.Cells[currentRow, 8].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss"; + worksheet.Cells[currentRow, 8].Value = data.endtime; + } worksheet.Cells[currentRow, 9].Value = data.endpressure; worksheet.Cells[currentRow, 10].Value = data.dwelltime; - worksheet.Cells[currentRow, 11].Value = data.diffpressure; worksheet.Cells[currentRow, 12].Value = tempMode; worksheet.Cells[currentRow, 13].Value = data.temperature; worksheet.Cells[currentRow, 14].Value = data.standarderror; worksheet.Cells[currentRow, 15].Value = data.testresult; - worksheet.Cells[currentRow, 16].Value = data.CreateTime; + // 创建时间格式 + if (data.CreateTime != null) + { + worksheet.Cells[currentRow, 16].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss"; + worksheet.Cells[currentRow, 16].Value = data.CreateTime; + } - - // 设置数据行样式 - for (int col = 1; col <= 9; col++) + // 设置整行样式 + for (int col = 1; col <= 16; col++) { var cell = worksheet.Cells[currentRow, col]; cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; - cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.LightGray); + cell.Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(217, 217, 217)); // 隔行变色 if (currentRow % 2 == 0) { cell.Style.Fill.PatternType = ExcelFillStyle.Solid; - cell.Style.Fill.BackgroundColor.SetColor(Color.AliceBlue); + cell.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(248, 250, 252)); // 更柔和的蓝色背景 + } + else + { + cell.Style.Fill.PatternType = ExcelFillStyle.Solid; + cell.Style.Fill.BackgroundColor.SetColor(Color.White); } } - // 设置格式 - worksheet.Cells[currentRow, 4].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss"; - worksheet.Cells[currentRow, 5].Style.Numberformat.Format = "0.00"; - worksheet.Cells[currentRow, 6].Style.Numberformat.Format = "0.00"; - worksheet.Cells[currentRow, 7].Style.Numberformat.Format = "0.00"; - worksheet.Cells[currentRow, 8].Style.Numberformat.Format = "0.0"; + // 设置数值格式(修正之前的格式设置错误) + if (data.quantity != null) + worksheet.Cells[currentRow, 5].Style.Numberformat.Format = "0"; - // 设置行高 - worksheet.Row(currentRow).Height = 25; + if (data.startpressure != null) + worksheet.Cells[currentRow, 6].Style.Numberformat.Format = "0.00"; + + if (data.endpressure != null) + worksheet.Cells[currentRow, 9].Style.Numberformat.Format = "0.00"; + + if (data.dwelltime != null) + worksheet.Cells[currentRow, 10].Style.Numberformat.Format = "0.0"; + + if (data.diffpressure != null) + worksheet.Cells[currentRow, 11].Style.Numberformat.Format = "0.00"; + + if (data.temperature != null) + worksheet.Cells[currentRow, 13].Style.Numberformat.Format = "0.0"; + + if (data.standarderror != null) + worksheet.Cells[currentRow, 14].Style.Numberformat.Format = "0.00"; + + // 设置数据行高 + worksheet.Row(currentRow).Height = 22; currentRow++; } - // 自动调整列宽 - worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); + // 添加底部空行 + currentRow++; + worksheet.Cells[currentRow, 1, currentRow, 16].Merge = true; + worksheet.Cells[currentRow, 1].Value = "--- 报告结束 ---"; + worksheet.Cells[currentRow, 1].Style.Font.Italic = true; + worksheet.Cells[currentRow, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; + worksheet.Cells[currentRow, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center; + worksheet.Row(currentRow).Height = 25; + + // 设置整体边框 + var dataRange = worksheet.Cells[3, 1, currentRow - 1, 16]; + dataRange.Style.Border.Top.Style = ExcelBorderStyle.Thin; + dataRange.Style.Border.Top.Color.SetColor(Color.FromArgb(217, 217, 217)); + dataRange.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; + dataRange.Style.Border.Bottom.Color.SetColor(Color.FromArgb(217, 217, 217)); + dataRange.Style.Border.Left.Style = ExcelBorderStyle.Thin; + dataRange.Style.Border.Left.Color.SetColor(Color.FromArgb(217, 217, 217)); + dataRange.Style.Border.Right.Style = ExcelBorderStyle.Thin; + dataRange.Style.Border.Right.Color.SetColor(Color.FromArgb(217, 217, 217)); + + // 自动调整列宽(在设置固定宽度后微调) + worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(5, 20); // 最小5,最大20 // 保存 package.SaveAs(new FileInfo(filePath));