/// <summary> /// 处理列合并 /// </summary> /// <param name="columnMergedHeader">列合并表头</param> /// <param name="mergedList">合并信息集合</param> /// <param name="rowIndex">当前行下表(起始下标:0)</param> /// <param name="columnIndex">当前列下标(起始下标:0)</param> /// <param name="propertyInfo">字段属性</param> /// <param name="value">值</param> private void ProcessMergeColumn(IReadOnlyList <string[]> columnMergedHeader, ICollection <ExcelExportMergedRegionInfo> mergedList, int rowIndex, int columnIndex, PropertyInfo propertyInfo, object value) { //处理列合并 if (columnMergedHeader == null || !columnMergedHeader.Any(a => a.Contains(propertyInfo.Name))) { return; } //获取最后一个当前列的合并信息 ExcelExportMergedRegionInfo merge = mergedList.LastOrDefault(a => a.PropertyNames.Contains(propertyInfo.Name)); //值是否相等 bool isValueEqual = merge?.IsValueEqual(value) == true; //无该列合并信息、不是同一行、值不相等但可合并、值相等但不是相邻列 都要新建合并信息 if (merge == null || !merge.IsSameRow(rowIndex) || !isValueEqual && merge.IsCanMergedColumn() || isValueEqual && !merge.IsSiblingColumn(columnIndex)) { mergedList.Add(new ExcelExportMergedRegionInfo { PropertyNames = columnMergedHeader.First(a => a.Contains(propertyInfo.Name)), Value = value, FromRowIndex = rowIndex, ToRowIndex = rowIndex, FromColumnIndex = columnIndex, ToColumnIndex = columnIndex }); } else if (!isValueEqual) //值不相等,则替换掉 { merge.Value = value; merge.FromRowIndex = rowIndex; merge.ToRowIndex = rowIndex; merge.FromColumnIndex = columnIndex; merge.ToColumnIndex = columnIndex; } else //值相等,相邻列 ,则改变合并列下标 { if (merge.IsOutRangeColumnFrom(columnIndex)) { merge.FromColumnIndex -= 1; } else if (merge.IsOutRangeColumnTo(columnIndex)) { merge.ToColumnIndex += 1; } } }
/// <summary> /// 处理行合并 /// </summary> /// <param name="rowMergedHeader">行合并表头</param> /// <param name="mergedList">合并信息集合</param> /// <param name="rowIndex">当前行下标(起始下标:0)</param> /// <param name="columnIndex">当前列下标(起始下标:0)</param> /// <param name="propertyInfo">字段属性</param> /// <param name="value">值</param> private void ProcessMergeRow(IReadOnlyList <string> rowMergedHeader, ICollection <ExcelExportMergedRegionInfo> mergedList, int rowIndex, int columnIndex, PropertyInfo propertyInfo, object value) { if (rowMergedHeader.All(a => a != propertyInfo.Name)) { return; } //获取最后一个当前列的合并信息 ExcelExportMergedRegionInfo merge = mergedList.LastOrDefault(a => a.PropertyNames.Contains(propertyInfo.Name)); //值是否相等 bool isValueEqual = merge?.IsValueEqual(value) == true; //无该列合并信息、不是同一列,值不相等但可合并、值相等但不是相邻行 都要新建合并信息 if (merge == null || !merge.IsSameColumn(columnIndex) || !isValueEqual && merge.IsCanMergedRow() || isValueEqual && !merge.IsSiblingRow(rowIndex)) { mergedList.Add(new ExcelExportMergedRegionInfo { PropertyNames = new[] { propertyInfo.Name }, Value = value, FromRowIndex = rowIndex, ToRowIndex = rowIndex, FromColumnIndex = columnIndex, ToColumnIndex = columnIndex }); } else if (!isValueEqual) //不相等,则替换掉 { merge.Value = value; merge.FromRowIndex = rowIndex; merge.ToRowIndex = rowIndex; merge.FromColumnIndex = columnIndex; merge.ToColumnIndex = columnIndex; } else //值相等,相邻行 ,则改变合并行下标 { if (merge.IsOutRangeRowFrom(rowIndex)) { merge.FromRowIndex -= 1; } else if (merge.IsOutRangeRowTo(rowIndex)) { merge.ToRowIndex += 1; } } }