311 lines
14 KiB
C#
311 lines
14 KiB
C#
using OfficeOpenXml;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using TabletTester2025.Models;
|
|
|
|
namespace TabletTester2025.Services
|
|
{
|
|
public class ExcelExportService
|
|
{
|
|
|
|
public void ExportToExcel(IEnumerable<TestBatch> batches, string filePath)
|
|
{
|
|
using var package = new ExcelPackage(new FileInfo(filePath));
|
|
|
|
// 按测试类型分组
|
|
var hardnessData = batches.Where(b => b.TestType == "硬度").ToList();
|
|
var friabilityData = batches.Where(b => b.TestType == "脆碎度").ToList();
|
|
var disintegrationData = batches.Where(b => b.TestType == "崩解").ToList();
|
|
var dissolutionData = batches.Where(b => b.TestType == "溶出").ToList();
|
|
|
|
// 硬度表
|
|
if (hardnessData.Any())
|
|
{
|
|
var sheet = package.Workbook.Worksheets.Add("硬度报表");
|
|
// 表头
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "平均值(N)";
|
|
sheet.Cells[1, 4].Value = "RSD(%)";
|
|
sheet.Cells[1, 5].Value = "最大值(N)";
|
|
sheet.Cells[1, 6].Value = "最小值(N)";
|
|
sheet.Cells[1, 7].Value = "测试次数";
|
|
sheet.Cells[1, 8].Value = "内控下限(N)";
|
|
sheet.Cells[1, 9].Value = "内控上限(N)";
|
|
int row = 2;
|
|
foreach (var b in hardnessData)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.HardnessAvg;
|
|
sheet.Cells[row, 4].Value = b.HardnessRSD;
|
|
sheet.Cells[row, 5].Value = b.HardnessMax;
|
|
sheet.Cells[row, 6].Value = b.HardnessMin;
|
|
sheet.Cells[row, 7].Value = b.HardnessTestCount;
|
|
sheet.Cells[row, 8].Value = b.HardnessInternalMin;
|
|
sheet.Cells[row, 9].Value = b.HardnessInternalMax;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
}
|
|
else
|
|
{
|
|
// 如果没有数据也创建空表(可选),根据需求决定
|
|
var sheet = package.Workbook.Worksheets.Add("硬度报表");
|
|
sheet.Cells[1, 1].Value = "无硬度测试数据";
|
|
}
|
|
|
|
// 脆碎度表
|
|
if (friabilityData.Any())
|
|
{
|
|
var sheet = package.Workbook.Worksheets.Add("脆碎度报表");
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "失重率(%)";
|
|
sheet.Cells[1, 4].Value = "设定转速(r/min)";
|
|
sheet.Cells[1, 5].Value = "试验转数";
|
|
sheet.Cells[1, 6].Value = "前重(g)";
|
|
sheet.Cells[1, 7].Value = "后重(g)";
|
|
int row = 2;
|
|
foreach (var b in friabilityData)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.FriabilityLoss;
|
|
sheet.Cells[row, 4].Value = b.FriabilityTargetRpm;
|
|
sheet.Cells[row, 5].Value = b.FriabilityTargetRounds;
|
|
sheet.Cells[row, 6].Value = b.WeightBefore;
|
|
sheet.Cells[row, 7].Value = b.WeightAfter;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
}
|
|
else
|
|
{
|
|
var sheet = package.Workbook.Worksheets.Add("脆碎度报表");
|
|
sheet.Cells[1, 1].Value = "无脆碎度测试数据";
|
|
}
|
|
|
|
// 崩解表
|
|
if (disintegrationData.Any())
|
|
{
|
|
var sheet = package.Workbook.Worksheets.Add("崩解报表");
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "剂型规则";
|
|
sheet.Cells[1, 4].Value = "时限(秒)";
|
|
sheet.Cells[1, 5].Value = "崩解时间(秒)";
|
|
sheet.Cells[1, 6].Value = "剩余未崩解管";
|
|
sheet.Cells[1, 7].Value = "水浴温度(℃)";
|
|
int row = 2;
|
|
foreach (var b in disintegrationData)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.DisintegrationDosageForm;
|
|
sheet.Cells[row, 4].Value = b.DisintegrationLimitSeconds;
|
|
sheet.Cells[row, 5].Value = b.DisintegrationTimeSec;
|
|
sheet.Cells[row, 6].Value = b.RemainingTubesAtEnd;
|
|
sheet.Cells[row, 7].Value = b.DisintegrationTemp;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
}
|
|
else
|
|
{
|
|
var sheet = package.Workbook.Worksheets.Add("崩解报表");
|
|
sheet.Cells[1, 1].Value = "无崩解测试数据";
|
|
}
|
|
|
|
// 溶出表
|
|
if (dissolutionData.Any())
|
|
{
|
|
var sheet = package.Workbook.Worksheets.Add("溶出报表");
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "通道";
|
|
sheet.Cells[1, 4].Value = "30min溶出度(%)";
|
|
sheet.Cells[1, 5].Value = "R²";
|
|
sheet.Cells[1, 6].Value = "取样明细";
|
|
int row = 2;
|
|
foreach (var b in dissolutionData)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.DissolutionChannel;
|
|
sheet.Cells[row, 4].Value = b.DissolutionRate30Min;
|
|
sheet.Cells[row, 5].Value = b.DissolutionRSquared;
|
|
sheet.Cells[row, 6].Value = b.DissolutionSampleSummary;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
AddDissolutionSamplesSheet(package, dissolutionData);
|
|
}
|
|
else
|
|
{
|
|
var sheet = package.Workbook.Worksheets.Add("溶出报表");
|
|
sheet.Cells[1, 1].Value = "无溶出测试数据";
|
|
}
|
|
|
|
package.Save();
|
|
}
|
|
|
|
public void ExportHardnessToExcel(IEnumerable<TestBatch> batches, string filePath)
|
|
{
|
|
using var package = new ExcelPackage(new FileInfo(filePath));
|
|
var sheet = package.Workbook.Worksheets.Add("硬度报表");
|
|
// 只导出硬度相关列
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "平均值(N)";
|
|
sheet.Cells[1, 4].Value = "RSD(%)";
|
|
sheet.Cells[1, 5].Value = "最大值(N)";
|
|
sheet.Cells[1, 6].Value = "最小值(N)";
|
|
sheet.Cells[1, 7].Value = "测试次数";
|
|
sheet.Cells[1, 8].Value = "内控下限(N)";
|
|
sheet.Cells[1, 9].Value = "内控上限(N)";
|
|
|
|
int row = 2;
|
|
foreach (var b in batches)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.HardnessAvg;
|
|
sheet.Cells[row, 4].Value = b.HardnessRSD;
|
|
sheet.Cells[row, 5].Value = b.HardnessMax;
|
|
sheet.Cells[row, 6].Value = b.HardnessMin;
|
|
sheet.Cells[row, 7].Value = b.HardnessTestCount;
|
|
sheet.Cells[row, 8].Value = b.HardnessInternalMin;
|
|
sheet.Cells[row, 9].Value = b.HardnessInternalMax;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
package.Save();
|
|
}
|
|
|
|
public void ExportFriabilityToExcel(IEnumerable<TestBatch> batches, string filePath)
|
|
{
|
|
using var package = new ExcelPackage(new FileInfo(filePath));
|
|
var sheet = package.Workbook.Worksheets.Add("脆碎度报表");
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "失重率(%)";
|
|
sheet.Cells[1, 4].Value = "设定转速(r/min)";
|
|
sheet.Cells[1, 5].Value = "试验转数";
|
|
sheet.Cells[1, 6].Value = "前重(g)";
|
|
sheet.Cells[1, 7].Value = "后重(g)";
|
|
|
|
int row = 2;
|
|
foreach (var b in batches)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.FriabilityLoss;
|
|
sheet.Cells[row, 4].Value = b.FriabilityTargetRpm;
|
|
sheet.Cells[row, 5].Value = b.FriabilityTargetRounds;
|
|
sheet.Cells[row, 6].Value = b.WeightBefore;
|
|
sheet.Cells[row, 7].Value = b.WeightAfter;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
package.Save();
|
|
}
|
|
|
|
public void ExportDisintegrationToExcel(IEnumerable<TestBatch> batches, string filePath)
|
|
{
|
|
using var package = new ExcelPackage(new FileInfo(filePath));
|
|
var sheet = package.Workbook.Worksheets.Add("崩解报表");
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "剂型规则";
|
|
sheet.Cells[1, 4].Value = "时限(秒)";
|
|
sheet.Cells[1, 5].Value = "崩解时间(秒)";
|
|
sheet.Cells[1, 6].Value = "剩余未崩解管";
|
|
sheet.Cells[1, 7].Value = "水浴温度(℃)";
|
|
|
|
int row = 2;
|
|
foreach (var b in batches)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.DisintegrationDosageForm;
|
|
sheet.Cells[row, 4].Value = b.DisintegrationLimitSeconds;
|
|
sheet.Cells[row, 5].Value = b.DisintegrationTimeSec;
|
|
sheet.Cells[row, 6].Value = b.RemainingTubesAtEnd;
|
|
sheet.Cells[row, 7].Value = b.DisintegrationTemp;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
package.Save();
|
|
}
|
|
|
|
public void ExportDissolutionToExcel(IEnumerable<TestBatch> batches, string filePath)
|
|
{
|
|
using var package = new ExcelPackage(new FileInfo(filePath));
|
|
var sheet = package.Workbook.Worksheets.Add("溶出报表");
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "通道";
|
|
sheet.Cells[1, 4].Value = "30min溶出度(%)";
|
|
sheet.Cells[1, 5].Value = "R²";
|
|
sheet.Cells[1, 6].Value = "取样明细";
|
|
|
|
int row = 2;
|
|
foreach (var b in batches)
|
|
{
|
|
sheet.Cells[row, 1].Value = b.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = b.SampleName;
|
|
sheet.Cells[row, 3].Value = b.DissolutionChannel;
|
|
sheet.Cells[row, 4].Value = b.DissolutionRate30Min;
|
|
sheet.Cells[row, 5].Value = b.DissolutionRSquared;
|
|
sheet.Cells[row, 6].Value = b.DissolutionSampleSummary;
|
|
row++;
|
|
}
|
|
sheet.Cells.AutoFitColumns();
|
|
AddDissolutionSamplesSheet(package, batches);
|
|
package.Save();
|
|
}
|
|
|
|
private static void AddDissolutionSamplesSheet(ExcelPackage package, IEnumerable<TestBatch> batches)
|
|
{
|
|
var samples = batches
|
|
.SelectMany(batch => (batch.DissolutionSamples ?? Enumerable.Empty<DissolutionSamplePoint>())
|
|
.Select(sample => new { Batch = batch, Sample = sample }))
|
|
.OrderBy(x => x.Batch.TestTime)
|
|
.ThenBy(x => x.Sample.Channel)
|
|
.ThenBy(x => x.Sample.ScheduledTimeMin)
|
|
.ToList();
|
|
|
|
if (samples.Count == 0)
|
|
return;
|
|
|
|
var sheet = package.Workbook.Worksheets.Add("溶出取样明细");
|
|
sheet.Cells[1, 1].Value = "检测时间";
|
|
sheet.Cells[1, 2].Value = "样品名称";
|
|
sheet.Cells[1, 3].Value = "通道";
|
|
sheet.Cells[1, 4].Value = "计划时间(min)";
|
|
sheet.Cells[1, 5].Value = "实际时间(min)";
|
|
sheet.Cells[1, 6].Value = "溶出度(%)";
|
|
sheet.Cells[1, 7].Value = "记录时间";
|
|
|
|
int row = 2;
|
|
foreach (var item in samples)
|
|
{
|
|
sheet.Cells[row, 1].Value = item.Batch.TestTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
sheet.Cells[row, 2].Value = item.Batch.SampleName;
|
|
sheet.Cells[row, 3].Value = item.Sample.ChannelName;
|
|
sheet.Cells[row, 4].Value = item.Sample.ScheduledTimeMin;
|
|
sheet.Cells[row, 5].Value = item.Sample.ActualTimeMin;
|
|
sheet.Cells[row, 6].Value = item.Sample.Percent;
|
|
sheet.Cells[row, 7].Value = item.Sample.RecordedAt?.ToString("yyyy-MM-dd HH:mm:ss");
|
|
row++;
|
|
}
|
|
|
|
sheet.Cells.AutoFitColumns();
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|