This commit is contained in:
GukSang.Jin
2026-01-05 17:17:58 +08:00
parent 1aac1cb210
commit b1338a3f03
2 changed files with 428 additions and 110 deletions

View File

@@ -321,8 +321,14 @@ namespace WindowsFormsApp6
/// </summary>
private void RemoveAverageRows()
{
var avgRows = sampleDataTable.Select($"序号 = '{AVG_ROW_LABEL}'");
foreach (var row in avgRows)
var avgRows1 = sampleDataTable.Select($"序号 = '平均时间(s)试样1-5'");
var avgRows2 = sampleDataTable.Select($"序号 = '平均时间(s)试样6-10'");
foreach (var row in avgRows1)
{
sampleDataTable.Rows.Remove(row);
}
foreach (var row in avgRows2)
{
sampleDataTable.Rows.Remove(row);
}
@@ -415,38 +421,96 @@ namespace WindowsFormsApp6
/// </summary>
private void UpdateAverageRow(double[] averages)
{
var avgRows = sampleDataTable.Select($"序号 = '{AVG_ROW_LABEL}'");
// 移除所有平均值行
var avgRows1 = sampleDataTable.Select($"序号 = '平均时间(s)试样1-5'");
var avgRows2 = sampleDataTable.Select($"序号 = '平均时间(s)试样6-10'");
// 计算总平均值
double overallAvg = CalculateOverallAverage();
if (avgRows.Length == 0)
foreach (var row in avgRows1)
{
// 添加新的平均值行
DataRow avgRow = sampleDataTable.NewRow();
avgRow["序号"] = AVG_ROW_LABEL;
// 只在第一列显示总平均值
avgRow["试样1"] = overallAvg;
// 其他列设置为空
for (int i = 2; i <= currentSampleCount; i++)
{
avgRow[$"试样{i}"] = DBNull.Value;
}
sampleDataTable.Rows.Add(avgRow);
sampleDataTable.Rows.Remove(row);
}
else
foreach (var row in avgRows2)
{
// 更新现有平均值行
avgRows[0]["试样1"] = overallAvg;
sampleDataTable.Rows.Remove(row);
}
// 获取所有时间行数据
var timeRows = sampleDataTable.AsEnumerable()
.Where(r => r.Field<string>("序号") == TIME_ROW_LABEL);
if (!timeRows.Any())
return;
// 计算试样1-5的平均值
int group1Count = Math.Min(5, currentSampleCount);
if (group1Count > 0)
{
double sum1 = 0;
int count1 = 0;
foreach (var row in timeRows)
{
for (int i = 1; i <= group1Count; i++)
{
sum1 += row.Field<double>($"试样{i}");
count1++;
}
}
double avg1 = count1 > 0 ? sum1 / count1 : 0;
// 添加试样1-5平均值行
DataRow avgRow1 = sampleDataTable.NewRow();
avgRow1["序号"] = "平均时间(s)试样1-5";
avgRow1["试样1"] = avg1;
// 其他列设置为空
for (int i = 2; i <= currentSampleCount; i++)
{
avgRows[0][$"试样{i}"] = DBNull.Value;
avgRow1[$"试样{i}"] = DBNull.Value;
}
sampleDataTable.Rows.Add(avgRow1);
}
// 计算试样6-10的平均值仅当试样数量>5时
if (currentSampleCount > 5)
{
int group2Count = Math.Min(5, currentSampleCount - 5);
double sum2 = 0;
int count2 = 0;
foreach (var row in timeRows)
{
for (int i = 6; i <= 5 + group2Count; i++)
{
sum2 += row.Field<double>($"试样{i}");
count2++;
}
}
double avg2 = count2 > 0 ? sum2 / count2 : 0;
// 添加试样6-10平均值行
DataRow avgRow2 = sampleDataTable.NewRow();
avgRow2["序号"] = "平均时间(s)试样6-10";
// 前5列为空
for (int i = 1; i <= 5; i++)
{
avgRow2[$"试样{i}"] = DBNull.Value;
}
// 第6列显示平均值
avgRow2["试样6"] = avg2;
// 其他列为空
for (int i = 7; i <= currentSampleCount; i++)
{
avgRow2[$"试样{i}"] = DBNull.Value;
}
sampleDataTable.Rows.Add(avgRow2);
}
}
@@ -596,31 +660,82 @@ namespace WindowsFormsApp6
private void AddAverageRow(ISheet sheet, ICellStyle avgStyle)
{
var dataRows = sampleDataTable.AsEnumerable()
.Where(r => r.Field<string>("序号") != AVG_ROW_LABEL);
.Where(r => r.Field<string>("序号") != "平均时间(s)试样1-5" &&
r.Field<string>("序号") != "平均时间(s)试样6-10");
if (!dataRows.Any())
return;
int rowIndex = sheet.LastRowNum + 1;
IRow avgRow = sheet.CreateRow(rowIndex);
// 序号列
ICell labelCell = avgRow.CreateCell(0);
labelCell.SetCellValue(AVG_ROW_LABEL);
labelCell.CellStyle = avgStyle;
// 计算总平均值
double overallAvg = CalculateOverallAverage();
// 在第一个试样列显示总平均值
ICell avgCell = avgRow.CreateCell(1);
avgCell.SetCellValue(overallAvg);
avgCell.CellStyle = avgStyle;
// 合并所有试样列
if (currentSampleCount > 1)
// 计算试样1-5的平均值
int group1Count = Math.Min(5, currentSampleCount);
if (group1Count > 0)
{
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, currentSampleCount));
double sum1 = 0;
int count1 = 0;
foreach (var row in dataRows)
{
for (int i = 1; i <= group1Count; i++)
{
sum1 += row.Field<double>($"试样{i}");
count1++;
}
}
double avg1 = count1 > 0 ? sum1 / count1 : 0;
// 添加试样1-5平均值行
IRow avgRow1 = sheet.CreateRow(rowIndex++);
ICell labelCell1 = avgRow1.CreateCell(0);
labelCell1.SetCellValue("平均时间(s)试样1-5");
labelCell1.CellStyle = avgStyle;
ICell avgCell1 = avgRow1.CreateCell(1);
avgCell1.SetCellValue(avg1);
avgCell1.CellStyle = avgStyle;
// 合并试样1-5列
if (group1Count > 1)
{
sheet.AddMergedRegion(new CellRangeAddress(rowIndex - 1, rowIndex - 1, 1, group1Count));
}
}
// 计算试样6-10的平均值仅当试样数量>5时
if (currentSampleCount > 5)
{
int group2Count = Math.Min(5, currentSampleCount - 5);
double sum2 = 0;
int count2 = 0;
foreach (var row in dataRows)
{
for (int i = 6; i <= 5 + group2Count; i++)
{
sum2 += row.Field<double>($"试样{i}");
count2++;
}
}
double avg2 = count2 > 0 ? sum2 / count2 : 0;
// 添加试样6-10平均值行
IRow avgRow2 = sheet.CreateRow(rowIndex);
ICell labelCell2 = avgRow2.CreateCell(0);
labelCell2.SetCellValue("平均时间(s)试样6-10");
labelCell2.CellStyle = avgStyle;
ICell avgCell2 = avgRow2.CreateCell(6);
avgCell2.SetCellValue(avg2);
avgCell2.CellStyle = avgStyle;
// 合并试样6-10列
if (group2Count > 1)
{
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 6, 5 + group2Count));
}
}
}
@@ -795,22 +910,74 @@ namespace WindowsFormsApp6
}
}
// 平均时间行
IRow avgRow = sheet.CreateRow(rowIndex);
ICell avgLabelCell = avgRow.CreateCell(0);
avgLabelCell.SetCellValue(AVG_ROW_LABEL);
avgLabelCell.CellStyle = dataStyle;
// 计算并显示总平均值
double overallAvg = CalculateOverallAverage();
ICell avgCell = avgRow.CreateCell(1);
avgCell.SetCellValue(overallAvg);
avgCell.CellStyle = yellowStyle;
// 合并所有试样列
if (currentSampleCount > 1)
// 计算试样1-5的平均值
int group1Count = Math.Min(5, currentSampleCount);
if (group1Count > 0 && timeRows.Any())
{
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, currentSampleCount));
double sum1 = 0;
int count1 = 0;
foreach (var row in timeRows)
{
for (int i = 1; i <= group1Count; i++)
{
sum1 += row.Field<double>($"试样{i}");
count1++;
}
}
double avg1 = count1 > 0 ? sum1 / count1 : 0;
// 添加试样1-5平均值行
IRow avgRow1 = sheet.CreateRow(rowIndex++);
ICell avgLabelCell1 = avgRow1.CreateCell(0);
avgLabelCell1.SetCellValue("平均时间(s)试样1-5");
avgLabelCell1.CellStyle = dataStyle;
ICell avgCell1 = avgRow1.CreateCell(1);
avgCell1.SetCellValue(avg1);
avgCell1.CellStyle = yellowStyle;
// 合并试样1-5列
if (group1Count > 1)
{
sheet.AddMergedRegion(new CellRangeAddress(rowIndex - 1, rowIndex - 1, 1, group1Count));
}
}
// 计算试样6-10的平均值仅当试样数量>5时
if (currentSampleCount > 5 && timeRows.Any())
{
int group2Count = Math.Min(5, currentSampleCount - 5);
double sum2 = 0;
int count2 = 0;
foreach (var row in timeRows)
{
for (int i = 6; i <= 5 + group2Count; i++)
{
sum2 += row.Field<double>($"试样{i}");
count2++;
}
}
double avg2 = count2 > 0 ? sum2 / count2 : 0;
// 添加试样6-10平均值行
IRow avgRow2 = sheet.CreateRow(rowIndex);
ICell avgLabelCell2 = avgRow2.CreateCell(0);
avgLabelCell2.SetCellValue("平均时间(s)试样6-10");
avgLabelCell2.CellStyle = dataStyle;
ICell avgCell2 = avgRow2.CreateCell(6);
avgCell2.SetCellValue(avg2);
avgCell2.CellStyle = yellowStyle;
// 合并试样6-10列
if (group2Count > 1)
{
sheet.AddMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 6, 5 + group2Count));
}
}
}