This commit is contained in:
GukSang.Jin
2026-01-03 14:04:08 +08:00
parent e16822b6bd
commit b42acd58b0

View File

@@ -389,10 +389,6 @@ namespace WindowsFormsApp6
cell.CellStyle = styles.headerStyle;
}
ICell noteCell = headerRow.CreateCell(sampleCount + 1);
noteCell.SetCellValue("根据样品个数进行选填");
noteCell.CellStyle = styles.headerStyle;
// 数据行
if (dataTable != null && dataTable.Rows.Count > 0)
{
@@ -408,33 +404,58 @@ namespace WindowsFormsApp6
nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle;
for (int i = 1; i <= sampleCount; i++)
// 如果是平均时间行,只显示一个合并的单元格
if (rowName.Contains("平均"))
{
ICell cell = row.CreateCell(i);
if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value)
ICell avgCell = row.CreateCell(1);
if (dataTable.Columns.Contains("试样1") && dataRow["试样1"] != DBNull.Value)
{
if (double.TryParse(dataRow[$"试样{i}"].ToString(), out double value))
if (double.TryParse(dataRow["试样1"].ToString(), out double value))
{
cell.SetCellValue(value);
avgCell.SetCellValue(value);
}
else
{
cell.SetCellValue("系统读数");
avgCell.SetCellValue("");
}
}
else
{
cell.SetCellValue("系统读数");
avgCell.SetCellValue("");
}
cell.CellStyle = styles.yellowStyle;
avgCell.CellStyle = styles.yellowStyle;
// 为合并区域的其他单元格设置样式
for (int i = 2; i <= sampleCount; i++)
{
row.CreateCell(i).CellStyle = styles.yellowStyle;
}
// 合并平均时间单元格
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, 1, sampleCount));
}
// 如果是平均时间行,添加"系统计算"
if (rowName.Contains("平均"))
else
{
ICell calcCell = row.CreateCell(sampleCount + 1);
calcCell.SetCellValue("系统计算");
calcCell.CellStyle = styles.yellowStyle;
// 普通数据行,显示每个试样的数据
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = row.CreateCell(i);
if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value)
{
if (double.TryParse(dataRow[$"试样{i}"].ToString(), out double value))
{
cell.SetCellValue(value);
}
else
{
cell.SetCellValue("");
}
}
else
{
cell.SetCellValue("");
}
cell.CellStyle = styles.yellowStyle;
}
}
}
}
@@ -450,65 +471,25 @@ namespace WindowsFormsApp6
subtitleCell.SetCellValue("液体吸收量");
subtitleCell.CellStyle = styles.headerStyle;
// 为合并区域的所有单元格设置样式
for (int i = 1; i <= sampleCount + 3; i++)
for (int i = 1; i <= sampleCount; i++)
{
subtitleRow.CreateCell(i).CellStyle = styles.headerStyle;
}
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, 0, sampleCount + 3));
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, 0, sampleCount));
// 第一行表头(浸润时间、系统读数等)
IRow headerRow1 = sheet.CreateRow(currentRow);
headerRow1.Height = 400;
ICell seqCell1 = headerRow1.CreateCell(0);
seqCell1.SetCellValue("序号");
seqCell1.CellStyle = styles.headerStyle;
ICell timeCell = headerRow1.CreateCell(1);
timeCell.SetCellValue("浸润时间");
timeCell.CellStyle = styles.headerStyle;
ICell readCell = headerRow1.CreateCell(2);
readCell.SetCellValue("系统读数");
readCell.CellStyle = styles.headerStyle;
ICell hangCell = headerRow1.CreateCell(3);
hangCell.SetCellValue("悬挂时间");
hangCell.CellStyle = styles.headerStyle;
ICell readCell2 = headerRow1.CreateCell(4);
readCell2.SetCellValue("系统读数");
readCell2.CellStyle = styles.headerStyle;
ICell speedCell = headerRow1.CreateCell(5);
speedCell.SetCellValue("运行速度");
speedCell.CellStyle = styles.headerStyle;
ICell readCell3 = headerRow1.CreateCell(6);
readCell3.SetCellValue("系统读数");
readCell3.CellStyle = styles.headerStyle;
ICell noteCell1 = headerRow1.CreateCell(sampleCount + 1);
noteCell1.SetCellValue("根据样品个数进行选填");
noteCell1.CellStyle = styles.headerStyle;
currentRow++;
// 第二行表头试样1-5
IRow headerRow2 = sheet.CreateRow(currentRow++);
headerRow2.Height = 400;
// 序号列在第二行也需要创建并设置样式(用于合并)
headerRow2.CreateCell(0).CellStyle = styles.headerStyle;
// 表头
IRow headerRow = sheet.CreateRow(currentRow++);
headerRow.Height = 400;
ICell seqCell = headerRow.CreateCell(0);
seqCell.SetCellValue("序号");
seqCell.CellStyle = styles.headerStyle;
for (int i = 1; i <= sampleCount; i++)
{
ICell cell = headerRow2.CreateCell(i);
ICell cell = headerRow.CreateCell(i);
cell.SetCellValue($"试样{i}");
cell.CellStyle = styles.headerStyle;
}
// 合并序号列(跨两行)
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 2, currentRow - 1, 0, 0));
// 数据行
if (dataTable != null && dataTable.Rows.Count > 0)
@@ -525,70 +506,72 @@ namespace WindowsFormsApp6
nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle;
for (int i = 1; i <= sampleCount; i++)
// 判断是否是需要合并的行(平均值、最大值、标准偏差)
bool isMergedRow = rowName.Contains("平均值") || rowName.Contains("最大值") || rowName.Contains("标准偏差");
if (isMergedRow)
{
ICell cell = row.CreateCell(i);
if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value)
// 合并为一列,只显示第一个试样的值
ICell valueCell = row.CreateCell(1);
if (dataTable.Columns.Contains("试样1") && dataRow["试样1"] != DBNull.Value)
{
object value = dataRow[$"试样{i}"];
object value = dataRow["试样1"];
if (value != null && double.TryParse(value.ToString(), out double numValue))
{
cell.SetCellValue(numValue);
valueCell.SetCellValue(numValue);
}
else
{
cell.SetCellValue("系统读数");
valueCell.SetCellValue("");
}
}
else
{
cell.SetCellValue("系统读数");
valueCell.SetCellValue("");
}
cell.CellStyle = styles.yellowStyle;
}
}
}
// 添加平均值和最大值行的说明
if (dataTable != null && dataTable.Rows.Count > 0)
{
// 查找平均值行
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string rowName = dataTable.Rows[i]["序号"]?.ToString() ?? "";
if (rowName.Contains("平均"))
{
int avgRowIndex = currentRow - (dataTable.Rows.Count - i);
IRow avgExcelRow = sheet.GetRow(avgRowIndex);
if (avgExcelRow != null)
valueCell.CellStyle = styles.yellowStyle;
// 为合并区域的其他单元格设置样式
for (int i = 2; i <= sampleCount; i++)
{
ICell calcCell = avgExcelRow.CreateCell(sampleCount + 1);
calcCell.SetCellValue("系统根据多少组试样,计算多少组的标准偏差");
calcCell.CellStyle = styles.yellowStyle;
// 为合并区域的所有单元格设置样式
avgExcelRow.CreateCell(sampleCount + 2).CellStyle = styles.yellowStyle;
avgExcelRow.CreateCell(sampleCount + 3).CellStyle = styles.yellowStyle;
sheet.AddMergedRegion(new CellRangeAddress(avgRowIndex, avgRowIndex, sampleCount + 1, sampleCount + 3));
row.CreateCell(i).CellStyle = styles.yellowStyle;
}
break;
// 合并单元格
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, 1, sampleCount));
}
}
// 查找最大值行
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string rowName = dataTable.Rows[i]["序号"]?.ToString() ?? "";
if (rowName.Contains("最大"))
else
{
int maxRowIndex = currentRow - (dataTable.Rows.Count - i);
IRow maxExcelRow = sheet.GetRow(maxRowIndex);
if (maxExcelRow != null)
// 普通数据行,显示每个试样的数据
for (int i = 1; i <= sampleCount; i++)
{
ICell calcCell = maxExcelRow.CreateCell(sampleCount + 1);
calcCell.SetCellValue("系统计算");
calcCell.CellStyle = styles.yellowStyle;
ICell cell = row.CreateCell(i);
if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value)
{
object value = dataRow[$"试样{i}"];
if (value != null)
{
// 尝试转换为 double
if (double.TryParse(value.ToString(), out double numValue))
{
cell.SetCellValue(numValue);
}
else
{
// 如果不是数字,直接显示字符串值
cell.SetCellValue(value.ToString());
}
}
else
{
cell.SetCellValue("");
}
}
else
{
cell.SetCellValue("");
}
cell.CellStyle = styles.yellowStyle;
}
break;
}
}
}
@@ -684,21 +667,70 @@ namespace WindowsFormsApp6
nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle;
colIndex = 1;
for (int i = 1; i <= sampleCount; i++)
// 判断是否是标准偏差行(需要合并为一列)
bool isStdDeviationRow = rowName.Contains("标准偏差");
if (isStdDeviationRow)
{
for (int j = 1; j <= 3; j++)
// 标准偏差行:合并为一列,只显示第一个值
ICell valueCell = row.CreateCell(1);
string columnName = "试样1_1";
if (dataTable.Columns.Contains(columnName) && dataRow[columnName] != DBNull.Value)
{
ICell cell = row.CreateCell(colIndex++);
string columnName = $"试样{i}_{j}";
if (dataTable.Columns.Contains(columnName) && dataRow[columnName] != DBNull.Value)
if (double.TryParse(dataRow[columnName].ToString(), out double value))
{
if (double.TryParse(dataRow[columnName].ToString(), out double value))
if (Math.Abs(value) >= 0.001)
{
if (Math.Abs(value) >= 0.001)
valueCell.SetCellValue(value);
}
else
{
valueCell.SetCellValue("");
}
}
else
{
valueCell.SetCellValue("");
}
}
else
{
valueCell.SetCellValue("");
}
valueCell.CellStyle = styles.yellowStyle;
// 为合并区域的其他单元格设置样式
for (int i = 2; i <= sampleCount * 3; i++)
{
row.CreateCell(i).CellStyle = styles.yellowStyle;
}
// 合并单元格
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, 1, sampleCount * 3));
}
else
{
// 普通数据行:显示每个试样的每次测试数据
colIndex = 1;
for (int i = 1; i <= sampleCount; i++)
{
for (int j = 1; j <= 3; j++)
{
ICell cell = row.CreateCell(colIndex++);
string columnName = $"试样{i}_{j}";
if (dataTable.Columns.Contains(columnName) && dataRow[columnName] != DBNull.Value)
{
if (double.TryParse(dataRow[columnName].ToString(), out double value))
{
cell.SetCellValue(value);
if (Math.Abs(value) >= 0.001)
{
cell.SetCellValue(value);
}
else
{
// 所有空数据都显示为空白
cell.SetCellValue("");
}
}
else
{
@@ -711,13 +743,8 @@ namespace WindowsFormsApp6
// 所有空数据都显示为空白
cell.SetCellValue("");
}
cell.CellStyle = styles.yellowStyle;
}
else
{
// 所有空数据都显示为空白
cell.SetCellValue("");
}
cell.CellStyle = styles.yellowStyle;
}
}
}