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; cell.CellStyle = styles.headerStyle;
} }
ICell noteCell = headerRow.CreateCell(sampleCount + 1);
noteCell.SetCellValue("根据样品个数进行选填");
noteCell.CellStyle = styles.headerStyle;
// 数据行 // 数据行
if (dataTable != null && dataTable.Rows.Count > 0) if (dataTable != null && dataTable.Rows.Count > 0)
{ {
@@ -408,6 +404,38 @@ namespace WindowsFormsApp6
nameCell.SetCellValue(rowName); nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle; nameCell.CellStyle = styles.dataStyle;
// 如果是平均时间行,只显示一个合并的单元格
if (rowName.Contains("平均"))
{
ICell avgCell = row.CreateCell(1);
if (dataTable.Columns.Contains("试样1") && dataRow["试样1"] != DBNull.Value)
{
if (double.TryParse(dataRow["试样1"].ToString(), out double value))
{
avgCell.SetCellValue(value);
}
else
{
avgCell.SetCellValue("");
}
}
else
{
avgCell.SetCellValue("");
}
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));
}
else
{
// 普通数据行,显示每个试样的数据
for (int i = 1; i <= sampleCount; i++) for (int i = 1; i <= sampleCount; i++)
{ {
ICell cell = row.CreateCell(i); ICell cell = row.CreateCell(i);
@@ -419,22 +447,15 @@ namespace WindowsFormsApp6
} }
else else
{ {
cell.SetCellValue("系统读数"); cell.SetCellValue("");
} }
} }
else else
{ {
cell.SetCellValue("系统读数"); cell.SetCellValue("");
} }
cell.CellStyle = styles.yellowStyle; cell.CellStyle = styles.yellowStyle;
} }
// 如果是平均时间行,添加"系统计算"
if (rowName.Contains("平均"))
{
ICell calcCell = row.CreateCell(sampleCount + 1);
calcCell.SetCellValue("系统计算");
calcCell.CellStyle = styles.yellowStyle;
} }
} }
} }
@@ -450,66 +471,26 @@ namespace WindowsFormsApp6
subtitleCell.SetCellValue("液体吸收量"); subtitleCell.SetCellValue("液体吸收量");
subtitleCell.CellStyle = styles.headerStyle; 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; 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); IRow headerRow = sheet.CreateRow(currentRow++);
headerRow1.Height = 400; headerRow.Height = 400;
ICell seqCell1 = headerRow1.CreateCell(0); ICell seqCell = headerRow.CreateCell(0);
seqCell1.SetCellValue("序号"); seqCell.SetCellValue("序号");
seqCell1.CellStyle = styles.headerStyle; seqCell.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;
for (int i = 1; i <= sampleCount; i++) for (int i = 1; i <= sampleCount; i++)
{ {
ICell cell = headerRow2.CreateCell(i); ICell cell = headerRow.CreateCell(i);
cell.SetCellValue($"试样{i}"); cell.SetCellValue($"试样{i}");
cell.CellStyle = styles.headerStyle; cell.CellStyle = styles.headerStyle;
} }
// 合并序号列(跨两行)
sheet.AddMergedRegion(new CellRangeAddress(currentRow - 2, currentRow - 1, 0, 0));
// 数据行 // 数据行
if (dataTable != null && dataTable.Rows.Count > 0) if (dataTable != null && dataTable.Rows.Count > 0)
{ {
@@ -525,72 +506,74 @@ namespace WindowsFormsApp6
nameCell.SetCellValue(rowName); nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle; nameCell.CellStyle = styles.dataStyle;
// 判断是否是需要合并的行(平均值、最大值、标准偏差)
bool isMergedRow = rowName.Contains("平均值") || rowName.Contains("最大值") || rowName.Contains("标准偏差");
if (isMergedRow)
{
// 合并为一列,只显示第一个试样的值
ICell valueCell = row.CreateCell(1);
if (dataTable.Columns.Contains("试样1") && dataRow["试样1"] != DBNull.Value)
{
object value = dataRow["试样1"];
if (value != null && double.TryParse(value.ToString(), out double numValue))
{
valueCell.SetCellValue(numValue);
}
else
{
valueCell.SetCellValue("");
}
}
else
{
valueCell.SetCellValue("");
}
valueCell.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));
}
else
{
// 普通数据行,显示每个试样的数据
for (int i = 1; i <= sampleCount; i++) for (int i = 1; i <= sampleCount; i++)
{ {
ICell cell = row.CreateCell(i); ICell cell = row.CreateCell(i);
if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value) if (dataTable.Columns.Contains($"试样{i}") && dataRow[$"试样{i}"] != DBNull.Value)
{ {
object value = dataRow[$"试样{i}"]; object value = dataRow[$"试样{i}"];
if (value != null && double.TryParse(value.ToString(), out double numValue)) if (value != null)
{
// 尝试转换为 double
if (double.TryParse(value.ToString(), out double numValue))
{ {
cell.SetCellValue(numValue); cell.SetCellValue(numValue);
} }
else else
{ {
cell.SetCellValue("系统读数"); // 如果不是数字,直接显示字符串值
cell.SetCellValue(value.ToString());
} }
} }
else else
{ {
cell.SetCellValue("系统读数"); cell.SetCellValue("");
}
}
else
{
cell.SetCellValue("");
} }
cell.CellStyle = styles.yellowStyle; 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)
{
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));
}
break;
}
}
// 查找最大值行
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string rowName = dataTable.Rows[i]["序号"]?.ToString() ?? "";
if (rowName.Contains("最大"))
{
int maxRowIndex = currentRow - (dataTable.Rows.Count - i);
IRow maxExcelRow = sheet.GetRow(maxRowIndex);
if (maxExcelRow != null)
{
ICell calcCell = maxExcelRow.CreateCell(sampleCount + 1);
calcCell.SetCellValue("系统计算");
calcCell.CellStyle = styles.yellowStyle;
}
break;
}
}
} }
} }
@@ -684,6 +667,49 @@ namespace WindowsFormsApp6
nameCell.SetCellValue(rowName); nameCell.SetCellValue(rowName);
nameCell.CellStyle = styles.dataStyle; nameCell.CellStyle = styles.dataStyle;
// 判断是否是标准偏差行(需要合并为一列)
bool isStdDeviationRow = rowName.Contains("标准偏差");
if (isStdDeviationRow)
{
// 标准偏差行:合并为一列,只显示第一个值
ICell valueCell = row.CreateCell(1);
string columnName = "试样1_1";
if (dataTable.Columns.Contains(columnName) && dataRow[columnName] != DBNull.Value)
{
if (double.TryParse(dataRow[columnName].ToString(), out double value))
{
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; colIndex = 1;
for (int i = 1; i <= sampleCount; i++) for (int i = 1; i <= sampleCount; i++)
{ {
@@ -723,6 +749,7 @@ namespace WindowsFormsApp6
} }
} }
} }
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{ {