diff --git a/WindowsFormsApp6/MainForm.cs b/WindowsFormsApp6/MainForm.cs index c31adac..b7342f7 100644 --- a/WindowsFormsApp6/MainForm.cs +++ b/WindowsFormsApp6/MainForm.cs @@ -1863,33 +1863,46 @@ namespace WindowsFormsApp6 if (isMergedRow) { - // 合并为一列,只显示第一个试样的值 - ICell valueCell = row.CreateCell(1); - if (dataTable.Columns.Contains("试样1") && dataRow["试样1"] != DBNull.Value) + // 按每5个试样一组显示 + int groupCount = (int)Math.Ceiling((double)sampleCount / 5.0); + + for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) { - object value = dataRow["试样1"]; - if (value != null && double.TryParse(value.ToString(), out double numValue)) + int startSample = groupIndex * 5 + 1; + int endSample = Math.Min(startSample + 4, sampleCount); + + // 查找该组的统计值(存储在该组第一个试样的列中) + ICell valueCell = row.CreateCell(startSample); + if (dataTable.Columns.Contains($"试样{startSample}") && dataRow[$"试样{startSample}"] != DBNull.Value) { - valueCell.SetCellValue(numValue); + object value = dataRow[$"试样{startSample}"]; + 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 = startSample + 1; i <= endSample; i++) + { + row.CreateCell(i).CellStyle = styles.yellowStyle; + } + + // 合并该组的单元格 + if (endSample > startSample) + { + sheet.AddMergedRegion(new CellRangeAddress(currentRow - 1, currentRow - 1, startSample, endSample)); + } } - 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 {