using MembranePoreTester.Models; using System; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Xps.Packaging; namespace MembranePoreTester.Helpers { public static class ReportGenerator { public static void GenerateBubblePointReport(BubblePointRecord record) { FlowDocument doc = CreateBubblePointDocument(record); ShowPrintPreview(doc, "泡点法测试报告"); } public static void GeneratePoreDistributionReport(PoreDistributionRecord record) { FlowDocument doc = CreatePoreDistributionDocument(record); ShowPrintPreview(doc, "孔分布测试报告"); } private static FlowDocument CreateBubblePointDocument(BubblePointRecord record) { FlowDocument doc = new FlowDocument(); doc.PageWidth = 800; // 适应打印 // 标题 doc.Blocks.Add(new Paragraph(new Run("泡点法测试报告")) { FontSize = 24, FontWeight = FontWeights.Bold, TextAlignment = TextAlignment.Center }); // 日期/测试者 doc.Blocks.Add(new Paragraph(new Run($"测试日期: {record.TestDate:yyyy-MM-dd} 测试者: {record.Tester}")) { TextAlignment = TextAlignment.Right }); // 样品信息表格 Table table = new Table(); table.Columns.Add(new TableColumn { Width = new GridLength(150) }); table.Columns.Add(new TableColumn { Width = new GridLength(200) }); TableRowGroup group = new TableRowGroup(); AddRow(group, "膜类型", record.SampleType); AddRow(group, "规格", record.SampleSpec); AddRow(group, "室温(°C)", record.RoomTemperature.ToString("F1")); AddRow(group, "浸润时间(h)", record.SoakingTime.ToString("F1")); AddRow(group, "测试液体", record.Liquid?.Name); AddRow(group, "液体生产厂家", record.LiquidManufacturer); AddRow(group, "泡点压力", $"{record.BubblePointPressure} {record.PressureUnit}"); AddRow(group, "最大孔径(μm)", record.MaxPoreSize.ToString("F3")); table.RowGroups.Add(group); doc.Blocks.Add(table); doc.Blocks.Add(new Paragraph(new Run("本报告依据 GB/T 32361-2015 生成。")) { FontStyle = FontStyles.Italic, TextAlignment = TextAlignment.Center, Margin = new Thickness(0, 20, 0, 0) }); return doc; } private static FlowDocument CreatePoreDistributionDocument(PoreDistributionRecord record) { FlowDocument doc = new FlowDocument(); doc.PageWidth = 800; // 标题 doc.Blocks.Add(new Paragraph(new Run("孔分布测试报告")) { FontSize = 24, FontWeight = FontWeights.Bold, TextAlignment = TextAlignment.Center }); doc.Blocks.Add(new Paragraph(new Run($"测试日期: {record.TestDate:yyyy-MM-dd} 测试者: {record.Tester}")) { TextAlignment = TextAlignment.Right }); // 样品信息表格 Table infoTable = new Table(); infoTable.Columns.Add(new TableColumn { Width = new GridLength(150) }); infoTable.Columns.Add(new TableColumn { Width = new GridLength(200) }); TableRowGroup group = new TableRowGroup(); AddRow(group, "膜类型", record.SampleType); AddRow(group, "规格", record.SampleSpec); AddRow(group, "室温(°C)", record.RoomTemperature.ToString("F1")); AddRow(group, "浸润时间(h)", record.SoakingTime.ToString("F1")); AddRow(group, "测试液体", record.Liquid?.Name); AddRow(group, "液体生产厂家", record.LiquidManufacturer); infoTable.RowGroups.Add(group); doc.Blocks.Add(infoTable); // 计算结果 doc.Blocks.Add(new Paragraph(new Run("测试结果")) { FontSize = 18, FontWeight = FontWeights.Bold, Margin = new Thickness(0, 10, 0, 5) }); Table resultTable = new Table(); resultTable.Columns.Add(new TableColumn { Width = new GridLength(150) }); resultTable.Columns.Add(new TableColumn { Width = new GridLength(200) }); TableRowGroup resultGroup = new TableRowGroup(); AddRow(resultGroup, "泡点压力", $"{record.BubblePointPressure} {record.PressureUnit}"); AddRow(resultGroup, "平均孔径(μm)", record.AveragePoreSize.ToString("F3")); resultTable.RowGroups.Add(resultGroup); doc.Blocks.Add(resultTable); // 数据点列表 doc.Blocks.Add(new Paragraph(new Run("压力-流量数据")) { FontSize = 14, FontWeight = FontWeights.Bold, Margin = new Thickness(0, 10, 0, 5) }); Table dataTable = new Table(); dataTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); dataTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); dataTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); dataTable.CellSpacing = 2; TableRowGroup dataGroup = new TableRowGroup(); // 表头 TableRow headerRow = new TableRow(); headerRow.Cells.Add(new TableCell(new Paragraph(new Run("压力")))); headerRow.Cells.Add(new TableCell(new Paragraph(new Run("湿膜流量(L/min)")))); headerRow.Cells.Add(new TableCell(new Paragraph(new Run("干膜流量(L/min)")))); headerRow.FontWeight = FontWeights.Bold; dataGroup.Rows.Add(headerRow); foreach (var dp in record.DataPoints) { TableRow row = new TableRow(); row.Cells.Add(new TableCell(new Paragraph(new Run(dp.Pressure.ToString("F2"))))); row.Cells.Add(new TableCell(new Paragraph(new Run(dp.WetFlow.ToString("F3"))))); row.Cells.Add(new TableCell(new Paragraph(new Run(dp.DryFlow.ToString("F3"))))); dataGroup.Rows.Add(row); } dataTable.RowGroups.Add(dataGroup); doc.Blocks.Add(dataTable); // 孔分布结果 if (record.PoreDistributions != null && record.PoreDistributions.Any()) { doc.Blocks.Add(new Paragraph(new Run("孔分布")) { FontSize = 14, FontWeight = FontWeights.Bold, Margin = new Thickness(0, 10, 0, 5) }); Table distTable = new Table(); distTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); distTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); distTable.Columns.Add(new TableColumn { Width = new GridLength(80) }); TableRowGroup distGroup = new TableRowGroup(); TableRow distHeader = new TableRow(); distHeader.Cells.Add(new TableCell(new Paragraph(new Run("孔径下限(μm)")))); distHeader.Cells.Add(new TableCell(new Paragraph(new Run("孔径上限(μm)")))); distHeader.Cells.Add(new TableCell(new Paragraph(new Run("百分比(%)")))); distHeader.FontWeight = FontWeights.Bold; distGroup.Rows.Add(distHeader); foreach (var pd in record.PoreDistributions) { TableRow row = new TableRow(); row.Cells.Add(new TableCell(new Paragraph(new Run(pd.LowerPore.ToString("F3"))))); row.Cells.Add(new TableCell(new Paragraph(new Run(pd.UpperPore.ToString("F3"))))); row.Cells.Add(new TableCell(new Paragraph(new Run(pd.Percentage.ToString("F1"))))); distGroup.Rows.Add(row); } distTable.RowGroups.Add(distGroup); doc.Blocks.Add(distTable); } doc.Blocks.Add(new Paragraph(new Run("本报告依据 GB/T 32361-2015 生成。")) { FontStyle = FontStyles.Italic, TextAlignment = TextAlignment.Center, Margin = new Thickness(0, 20, 0, 0) }); return doc; } private static void AddRow(TableRowGroup group, string label, string value) { TableRow row = new TableRow(); row.Cells.Add(new TableCell(new Paragraph(new Run(label)))); row.Cells.Add(new TableCell(new Paragraph(new Run(value ?? "")))); group.Rows.Add(row); } private static void ShowPrintPreview(FlowDocument document, string title) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { // 调整文档页面大小以匹配打印机 document.PageHeight = printDialog.PrintableAreaHeight; document.PageWidth = printDialog.PrintableAreaWidth; document.PagePadding = new Thickness(50); IDocumentPaginatorSource dps = document; printDialog.PrintDocument(dps.DocumentPaginator, title); } } } }